Help - Search - Members - Calendar
Full Version: Java Help! Arrays / Sorting / Average
BleepingComputer.com > Software > Programming
   
KCabral06
CODE
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.
groovicus
Works fine for me.

KCabral06
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.
groovicus
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. smile.gif
KCabral06
haha thanks alot!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.