Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Spyware and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, 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.

 
Reply to this topicStart new topic
> Java Help! Arrays / Sorting / Average
KCabral06
post Feb 19 2008, 09:52 PM
Post #1


Member
**

Group: Members
Posts: 18
Joined: 18-September 07
From: Dartmouth, MA
Member No.: 157,542



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.
Go to the top of the page
 
+Quote Post
groovicus
post Feb 20 2008, 07:55 AM
Post #2


Hail Groovicus!
******

Group: Site Admin
Posts: 6,454
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



Works fine for me.



--------------------
Go to the top of the page
 
+Quote Post
KCabral06
post Feb 20 2008, 08:52 AM
Post #3


Member
**

Group: Members
Posts: 18
Joined: 18-September 07
From: Dartmouth, MA
Member No.: 157,542



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.
Go to the top of the page
 
+Quote Post
groovicus
post Feb 20 2008, 09:58 AM
Post #4


Hail Groovicus!
******

Group: Site Admin
Posts: 6,454
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



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


--------------------
Go to the top of the page
 
+Quote Post
KCabral06
post Feb 20 2008, 07:44 PM
Post #5


Member
**

Group: Members
Posts: 18
Joined: 18-September 07
From: Dartmouth, MA
Member No.: 157,542



haha thanks alot!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 9th January 2009 - 07:37 AM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.