Problem with highest average grade

Use the following lists for student names and their grades.

Isabel (92, 95, 94), Steve (99, 76, 68), Michael (89, 70, 85), James (80, 75, 71), Jennifer (78, 77, 93), Billy (93, 91, 89), Brenda (82, 95, 71), Jesus (98, 82, 84)

The output should appear as shown below:

Student Name Grade
Isabel (display average here)
Steve
Michael
James
Jennifer
Billy
Brenda
Jesus

The student with the highest average grade is ____ and the average is ____. (This is the part where i need help).

This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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 < numberOfstudents; student ++)
	{
		total = 0;

	for (int col = 0; col < numberOfgrades; col++)
		total += grades[student][col];
	    average = total / numberOfgrades;
	 cout << setw(9) << left << name[student];
	 cout << setw(14) << right << average << endl;
	}
	
	double highest = grades[numberOfstudents][numberOfgrades];
	for(int i = 0; i < numberOfstudents; i++)
	{
		for(int j = 0; j < numberOfgrades; j++) 
		{
			if(grades[i][j] > highest)
			{
				highest = grades[i][j];
			}
		} 
	}
	
       cout << "The student with the highest average is" << (need help getting the name here) 
		<< "and the average is" << highest << endl;
	return 0;
}


I'm supposed to find the student with the highest average grade. I worked on it a bit more but it gives me the highest grade instead of the highest average.
Last edited on
You can create a separate array to store averaged grades and than you can look for the highest averaged grade.

You have wrong initialization in line 41. Both indexes in grades exceed allowed limits (in array numbering starts from 0 and ends at NumberOfElements - 1. I recommend use double highest = grades[0][0];

To output name of student with the highest grade you can use something like it:
1
2
3
4
5
6
7
8
9
10
int iBestStudent = 0;
...
			if(grades[i][j] > highest)
			{
				highest = grades[i][j];
                                iBestStudent = i;
			}
...
cout << "The student with the highest average is" <<  name[iBestStudent] 
		<< "and the average is" << highest << endl;
why not create a couple variables to store highest average and student with highest average right at the getgo, that way as you do your main loop and calculate averages, you can just check to see if whatever average you just calculated is higher than the highest known average and save it if it is. This will save you the trouble of doing a for loop within a for loop twice, you will only need to do it once this way.

I would highly reccomend learning about classes and making a student class for this. For example:
header file
1
2
3
4
5
6
7
8
9
10
11
12
class Student
{
   private:
   string m_strName;
   int m_lAverage;

   public:
   bool SetName( string strName );
   void SetAverage( int lAverage );
   string GetName( );
   int GetAverage( );
};

cpp (implementation) file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Student::Student( ) : m_strName( ), m_lAverage( 0 )
{
}

bool Student::SetName( string strName )
{
   m_strName = strName;
}

void Student::SetAverage( int lAverage )
{
   m_lAverage = lAverage;
}

string Student::GetName( )
{
   return m_strName;
}

int Student::GetAverage( )
{
   return m_lAverage;
}


This is a very very basic class and similar functionality could easily be achieved using structs. It really doesn't take advantage of the important and powerful aspects of c++ classes. However, this is a really good thing to get used to!!
Last edited on
@tfityo thanks that help like alot now i got it :)

@ceruleus I still haven't gone over that chapter about classes but I'll try to do it your way too
Topic archived. No new replies allowed.