Thread: for loops

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10

    for loops

    I'm having trouble incorporating this for loop:

    Code:
    	double sum; 
    	for (int row= 0; row < i; row ++)
    	{
    		cout << Names[row] << "    ";
    		int col=0;
    		for (col= 0; col < num; col ++)
    			cout << Scores[row][col] << "   ";
    		cout << "\n";
    	}
    Into this one:

    Code:
    cout << fixed << showpoint << setprecision (2);
    	for (int row =0; row < i; row++) //For each student, calculate the average score. Average for each row.
    	{
    		sum= 0;
    		for (int col =0; col < num; col++)
    			sum= sum + Scores[row][col];
    
    		AvgStudentScore[row]= (float) sum/num;
    	
    		if (AvgStudentScore [row] > 90)
    			Grade[row]= 'A';
    
    		else if (AvgStudentScore [row] > 80)
    			Grade[row]= 'B';
    
    		else if (AvgStudentScore [row] > 67.5) 
    			Grade[row]= 'C';
    
    		else if (AvgStudentScore [row] > 55)
    			Grade[row]= 'D';
    
    		else 
    			Grade[row]= 'F';
    
    		cout << "* " << AvgStudentScore[row] << "   " << Grade[row] << "\n";
    	}
    I tried this:
    Code:
    double sum; 
    	cout << fixed << showpoint << setprecision (2);
    	for (int row= 0; row < i; row ++)
    	{
    		cout << Names[row] << "    ";
    		sum=0;
    		int col=0;
    		for (col= 0; col < num; col ++)
    			cout << Scores[row][col] << "   ";
    		sum= sum + Scores[row][col];
    		cout << "\n";
    		AvgStudentScore[row]= (float) sum/num;
    
    		if (AvgStudentScore [row] > 90)
    			Grade[row]= 'A';
    
    		else if (AvgStudentScore [row] > 80)
    			Grade[row]= 'B';
    
    		else if (AvgStudentScore [row] > 67.5) 
    			Grade[row]= 'C';
    
    		else if (AvgStudentScore [row] > 55)
    			Grade[row]= 'D';
    
    		else 
    			Grade[row]= 'F';
    
    		cout << "* " << AvgStudentScore[row] << "   " << Grade[row] << "\n";
    	}
    But the final code does not give me the results I am looking for. The average given by the last code is very very strange. Note, that when I have both for loops (first two loops) I do get the correct results (average), however, the results do not show up where they are supposed to be, which is, next to other data that I have in my program.

    Any ideas anyone?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How in the world do you cut and paste and lose your braces? Edit: I guess you didn't lose your braces so much as delete one of your for loops (note originally you had four, now you have two, and you merged one -- the row one -- fine).
    Last edited by tabstop; 08-05-2008 at 09:30 PM.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10
    Are you saying I still need to use all for loops even after merging?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Consider the line
    Code:
    sum= sum + Scores[row][col];
    Before: in a for loop. After: not in a for loop. There's nothing wrong with merging (in fact I would recommend that you do so), but you can't change things.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10
    Great! Thanks for pointing that out. I was in fact missing the sum formula inside the for loop and needed curly braces for that as well.

Popular pages Recent additions subscribe to a feed