Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Java Help! Arrays / Sorting / Average


  • Please log in to reply
4 replies to this topic

#1 KCabral06

KCabral06

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dartmouth, MA
  • Local time:04:44 AM

Posted 19 February 2008 - 09:52 PM

import java.util.*;import java.util.Scanner;public class Lab1 {	/**	 * @param args	 */	public static void main(String[] args) {		// TODO Auto-generated method stub		int ngrades;		int midTerm,finalGrade;		String studentFName,studentLName;		Scanner sc = new Scanner(System.in);		System.out.print("Please Enter Your First Name: ");		studentFName = sc.next();		System.out.print("Please Enter Your Last Name: ");		studentLName = sc.next();		System.out.println();		System.out.println("Hello " + studentFName + " " + studentLName + "!");		System.out.println();		System.out.print("Please Enter Your MidTerm Grade: ");		midTerm = sc.nextInt();		System.out.print("Please Enter Your Final Grade: ");		finalGrade = sc.nextInt();		int [] grades;		grades = new int[12];		int number = 1;		for (int i = 0; i < grades.length; i++){			System.out.print("Please Enter Grade # " + number + ": ");			grades[i] = sc.nextInt();			number++;		}		System.out.println();		System.out.println("Your Individual Lab Grades Not In Order Are: ");		System.out.println();		number = 1;		for (int num = 0; num < grades.length; num++){		System.out.println("Lab # "+number + " = " +grades[num]);		number ++;		}		Arrays.sort(grades);			int random;			System.out.println();			System.out.println("Your Top 10 Individual Lab Grades IN Order Are: ");			System.out.println();			number = 10;			for (int num = 11; num > 1; num--){			System.out.println("Lab # "+number + " = " +grades[num]);			number--;			}	}}

what i need to do is grab those 10 TOP GRADES from the array that i sorted and average them i've tried EVERYTHING! and i can't seem to do it : - (... can someone please help me out.

BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:44 AM

Posted 20 February 2008 - 07:55 AM

Works fine for me.

#3 KCabral06

KCabral06
  • Topic Starter

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dartmouth, MA
  • Local time:04:44 AM

Posted 20 February 2008 - 08:52 AM

yea that code i put up there works, but what i wanted to do is grab those top 10 grades that is displayed as the output and declare a new variable called average. And then average those top 10 grades whatever they may be. I wouldn't know how to go about doing that.

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:44 AM

Posted 20 February 2008 - 09:58 AM

I'm sorry, I didn't know what " Java Help! Arrays / Sorting / Average" was supposed to mean, and since you didn't clarify and posted some code, I could only assume that there was a problem with the code that you needed help with.

So you declare a new variable (you declared a variable called rand that you never used, so I am not sure why it is there). Then you get the the last number of the array, and set your new variable equal to that;
int myVariable=0;
myVariable = myVariable + array[12];
myVariable = myVariable + array[11];
...
...
...

Do that ten times, and you will have the sum of the top 10 grades. I am sure that you can figure out how to average it from there.

A couple of other 'critiques' that will help you down the road......

You are declaring a variable named number that is presumably a label which grade is being entered, and which grade is being displayed. Declaring a variable named number is not a good idea, because it tells you nothing about what is actual being held in that variable. A better name would be something like numLabel, or gradeNumLabel. Better yet, do not use a counter like that at all. In your for loop, you have a counter that keeps track of the iteration through the loop. Use i instead. The first time you are in the loop, you want the label to be "Please Enter Grade #1" right? But i==0 (since it is the first time in the loop). So what plus zero equals one?

Instead of this:
System.out.print("Please Enter Grade # " + number + ": ");
Do this:
System.out.print("Please Enter Grade # " +(i+1) + ": ");

That knocks half a dozen lines out of your code, and you don't have to worry about creating a good name for a variable.

In your last 'for' loop, you construct your loop as follows:
for (int num = 11; num > 1; num--){
//some other stuff here
}

What happens if you do something like this? (add it into your code and see)


for (int i = 1; i<11; i++){
System.out.println("--->" + grades[grades.length-i]);
System.out.println("--->" + (10 + (-i)));
}

Make sure to remember all of your indentations. And I'll quit picking on you now. :thumbsup:

#5 KCabral06

KCabral06
  • Topic Starter

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dartmouth, MA
  • Local time:04:44 AM

Posted 20 February 2008 - 07:44 PM

haha thanks alot!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users