Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested for loops, need more assignment help....

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have to write a program that displays the averages of four different sets of grades using a nested for loop. I wasn't quite positive whether or not I was doing it right, but I gave it a shot. Heres the code and the errors I keep getting...



Any help as to how I might improve this, or at least get it so that it runs would be most appreciated.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I think you need to do a bit of reading on what a for loop is and how you write one. A typical nested for loop can be seen in the code snippet below. Compile this code and run it - do you understand what is going on here?

Where are the scores you are averaging coming from? Are they input by the user?
Michael
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what a nested for loop is, I'm just not too sure how to apply it to the problem. Its basically a for loop within a for loop. The scores are already given, so I have to somehow set them as doubles somewhere within the program.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (gr1=23.2; gr2=31.5; gr3=16.9; gr4=27.5; gr5=25.4; gr6=28.6)
Not sure what you are trying to accomplish here, but it's definitely not a nested "if". Look again at Michael's code, and think about the problem that you are trying to solve.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a for-loop has a very specific structure. inside the parens there is an initialization, a test condition, and a 'what to do at the end of each loop'. each is separated by a semicolon - therefore, you should only have 2 semicolons. in your example, the compiler is confused because you have something like 5... it can't figure out what you are doing.
a for loop is often used to look at every element of an array...
so, maybe you could use a single for-loop to add all the values of one set of grades, if they were in an array. then after the loop, you could calculate the total and average.
but why do they say a nested loop? well, you actually have a set of (set of grades). this sounds like a 2D arrray. one loop (outer) would iterate over the set of sets, and the inner loop would iterate over the set of grades.
I'd try and just write the inner loop, over one set of grades, and the calucation first. once i was pretty sure i had that part working, and understood it, try and wrap that whole thing in another loop. this might then also require some tweaking of the inner loop, since the data would now be in a 2d instead of a 1d array.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love this place.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William: Now, now, now, Be Nice!
Brandi:
Fred is right in his description of the problem. You are attempting to write a for loop to calculate each set of grades. Instead, write one foor loop to calculate one set and then place that loop inside another which iterates over the individual sets.
It is possible to have more than one initializer in a for loop, but it is not recommended that you do so. Instead, put your grades into an array (or a List if you prefer) and then iterate over the array.
 
Yan Lee
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brandi:
One way you can use nested loops to solve the problem at hand is:
1. create a 2-D array of 4 rows where each row holds a set of 4 grades
2. for each row, you can sum the contents of the elements of the row
3. so you need 2 loops, one for traversing the rows, and other for travesing the columns
public class MyClass
{
//1. declare 2-d array variable and initialise it with 4 values in each row
//int [][]myArray={....}
int sum=0;
public MyClass()
{
//2. here comes the ensted llop
for(int i=0; i<myArray.length;i++)
{
for(int j=0;j<4;j++)
{//sum the contents here
}
System.out.println("Sum of the"+ sum);
System.out.println("Average of set"+(sum/4));
System.out.println("----------------------------------");
}
}
public static void main(String []args)
{ MyClass mc=new MyClass();}

}
Hope that this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic