# Nested loops

• 02-10-2011, 11:24 PM
Aestuv
Nested loops
Im attempting to print---

Enter score 1 for student 1: 75
Enter score 2 for student 1: 82
Enter score 3 for student 1: 67

Average for student 1 is 74.67

Enter score 1 for student 2: 100
Enter score 2 for student 2: 66
Enter score 3 for student 2: 85

Average for student 2 is 83.67

Overall class average is 79.1
The lowest score was a 66 from student 2 on exam 2
The highest score was a 100 from student 2 on exam 1
(random scores obviously)
----
Im having trouble calculating the overall class average, and not sure where
to start for the lowest/highest score. Any input would be appreciated, this is my current code:

Code:

```System.out.print("Enter number of students:");                 int stud = Integer.parseInt(s.nextLine());                 System.out.print("Enter number of exams:");                 int exam = Integer.parseInt(s.nextLine());         double max = 0;         double classum = 0;         for (int i = 1; i <= stud; i++){                         double sum = 0;                         for (int j = 1; j <= exam; j++){                                 System.out.print("Enter Score " + j + " for student " +i+":");                                 double score = Double.parseDouble(s.nextLine());                                 sum = sum+score;                                 classum += sum;                                }                         System.out.println("Average for student "+ (sum/exam) );                 }                 System.out.println("Overall class average is: " +classum/(exam*stud) );                 System.out.println("The lowest score was a");                 System.out.println("The highest score was a");                 } }```
------

Thanks for any help!
• 02-11-2011, 12:09 AM
sunde887
calculate classsum outside the inner loop, directly after, this way it will find one students test scores, then add it to the class sum and do this for every student, then when you do classsum/(student * exam) it will work correctly.

it will be easier to find min/max grades if you store the grades in an arraylist. This way, you can set the first item in the list to max, and loop through each item, if some item is greater then max, set it to the new max, at the end you will be left with the correct max value. Its similar for min. Here is an examples with an array and some numbers

Code:

```int[] intArr = {5, 2, 45, 98, 24 }; int max = intArr[0]; for(int i = 1; i < intArr.length; i++){   if(intArr[i] > max){     max = intArr[i];   } } System.out.println(max);```
This may be giving you a bit too much to be honest.
• 02-11-2011, 12:10 AM
Junky
So you don't have a nested loop problem at all.

For class sum take a close look at what your code is doing. Pretend you are the computer executing the code.

Enter score one > user enters 1 > sum = 0 + 1 > sum = 1 > classum = 0 + 1 > classum = 1
Enter score two > user enters 4 > sum = 1 + 4 > sum = 5 > classum = 1 + 5 > classum = 6

Hang on! The two scores are 1 and 4 so classum should be 5 but it is 6. <scratches head>

Enter score three > user enters 5 > sum = 5 + 5 > sum = 10 > classum = 6 + 10 > classum = 16

Wow, now classum is 16 but it should be 10. Hmmmm!
• 02-11-2011, 12:40 AM
Aestuv
Thanks guys! You helped a lot

Im still working on the minimum/maximum grades. We aren't suppose to use arrays yet.