Thread: Help with finding the highest average grade

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Help with finding the highest average grade

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const int numberOfstudents = 8;
    	const int numberOfgrades = 3;
    	double total;
    	double average;
    
    	string name[numberOfstudents] = {"Isabel", "Steve", "Michael",
    	"James", "Jennifer", "Billy",
    	"Brenda", "Jesus"};
    	double grades[numberOfstudents][numberOfgrades] = {{92,95,94},
    	                                                   {99,76,68},
    	                                                   {89,70,85},
    	                                                   {80,75,71},
    	                                                   {78,77,93},
    	                                                   {93,91,89},
    	                                                   {82,95,71},
    	                                                  {98,82,84}};
    
    
    	cout << "Student Name      Grade\n";
        cout << "------------" << setw(14) << "-------\n";
    	
    	for (int student = 0; student < numberOfgrades; student ++)
    	{
    		total = 0;
    
    	for (int col = 0; col < numberOfgrades; col++)
    		total += grades[student][col];
    	    average = total / numberOfgrades;
    	 cout << setw(9) << left << name[student];
    	 cout << fixed << showpoint << setprecision(2);
    	 cout << setw(14) << right << average << endl;
    	}
    
    	double row;
    	double highestAverage = 0;
    	double topStudent;
    
    
            for( row = 1; row < numberOfstudents ; row++)
    		{
                    if(grades[row] > highestAverage)
    				{
                            highestAverage = grades[row];
                            topStudent = row;
                    }
            }
    	{
    	cout <<"\nThe student with the highest average grade is "
    	<< name[topStudent] <<" and the average is " << grades[topStudent] << endl;
    	}
    return 0;
    }
    This is the code i have so far but i keep getting errors. I need help finding the highest average grade.
    This is the error i get error C2108: subscript is not of integral type.
    Sorry for not posting it earlier.
    Last edited by Rockie; 11-08-2011 at 10:43 AM. Reason: added error

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
        double grades[numberOfstudents][numberOfgrades] = {{92,95,94},
                    if(grades[row] > highestAverage)
    You have defined grades as a two dimensional array in the first line, but you try to access this array as a single dimensional array in the second line. You are doing this in several places.

    Also in future when you have compiler errors, post the complete error messages exactly as they appear in your development environment, along with the code that generated these errors.

    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The type of i in "Array[i]" must be of integer type (long and char will work; double/float will not work).

    Code:
    double row;
    Tim S.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    i have fix a little bit but now it gives me the highest grade instead of the highest average

    Code:
    	double highestAverage = grades[numberOfstudents][numberOfgrades];
    	for(int i = 0; i < numberOfstudents; i++)
    	{
    		for(int j = 0; j < numberOfgrades; j++) 
    		{
    			if(grades[i][j] > highestAverage)
    			{
    				highestAverage = grades[i][j];
    			}
    		} 
    	}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you need to be searching an array of averages, not an array of grades.

    Or you calculate an average from one row of the array of grades, then find the highest of those.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding highest average
    By Vontrapp in forum C Programming
    Replies: 6
    Last Post: 06-07-2010, 06:38 AM
  2. Grade program average problem
    By Zaz in forum C Programming
    Replies: 1
    Last Post: 11-20-2009, 07:11 PM
  3. Replies: 3
    Last Post: 02-22-2009, 02:54 PM
  4. Student Roster & Grade Average
    By Surge in forum C Programming
    Replies: 10
    Last Post: 12-12-2006, 07:32 AM
  5. Drop Lowest Grade then Average
    By cmut in forum C Programming
    Replies: 4
    Last Post: 09-27-2002, 10:19 PM