BleepingComputer.com: Java Help! Arrays / Sorting / Average

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Java Help! Arrays / Sorting / Average

#1 User is offline   KCabral06 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 21
  • Joined: 18-September 07
  • Gender:Male
  • Location:Dartmouth, MA

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.

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,604
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 20 February 2008 - 07:55 AM

Works fine for me.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   KCabral06 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 21
  • Joined: 18-September 07
  • Gender:Male
  • Location:Dartmouth, MA

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 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,604
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

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:
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   KCabral06 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 21
  • Joined: 18-September 07
  • Gender:Male
  • Location:Dartmouth, MA

Posted 20 February 2008 - 07:44 PM

haha thanks alot!

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users