5 Replies - 1044 Views - Last Post: 10 November 2011 - 02:33 PM Rate Topic: -----

#1 Rackie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Finding the student with the highest average grade

Posted 10 November 2011 - 08:59 AM

Basically my program is supposed list the student names with their average grade. Also find the student with the highest average grade. The problem that I have is that instead of getting the highest average grade it displays the highest grade instead.



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 Average 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 in).

This is my code so far:

#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;

	string name[numberOfstudents] = {"Isabel", "Steve", "Michael",
									"James", "Jennifer", "Billy",
									"Brenda", "Jesus"};
	int 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      Average Grade\n";
    cout << "------------" << setw(14) << "-------\n";
	
	double total;
	double average;
	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;
	}
	
	
	int 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];
			}
		} 
	}
	
	cout << "\nThe student with the highest average grade is " 
		<< name << " and the average is " << highestAverage << endl;
	
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the student with the highest average grade

#2 eypros  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-October 11

Re: Finding the student with the highest average grade

Posted 10 November 2011 - 09:51 AM

You use
int highestAverage = grades[numberOfstudents][numberOfgrades];

which is
int highestAverage = grades[8][3];

which is out of bound of your array.

Also since
string name[numberOfstudents]

name
is just the address of the array. You sure don't want that.
Was This Post Helpful? 0
  • +
  • -

#3 Rackie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Re: Finding the student with the highest average grade

Posted 10 November 2011 - 10:15 AM

[quote name='eypros' date='10 November 2011 - 09:51 AM' timestamp='1320943873' post='1483373']
You use
int highestAverage = grades[numberOfstudents][numberOfgrades];

which is
int highestAverage = grades[8][3];

which is out of bound of your array.

So what i did is wrong is that you mean?
Was This Post Helpful? 0
  • +
  • -

#4 WabiSabi  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 31-December 10

Re: Finding the student with the highest average grade

Posted 10 November 2011 - 10:27 AM

View PostRackie, on 10 November 2011 - 11:15 AM, said:

So what i did is wrong is that you mean?


Yes, what you did is wrong (or it would work, yes?)

You want to access an index of your array, correct? But there is no grades[8][3] (grades[numberOfstudents][numberOfgrades]). There are 8 index positions total for the [8] part, and 3 total for the [3] part,
but remember they start at 0. So you could have grades[7][2]. That would be your 8th and 3rd index position (the very last position).

Anything greater than that will be out of bounds of your array.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1770
  • View blog
  • Posts: 3,429
  • Joined: 30-May 10

Re: Finding the student with the highest average grade

Posted 10 November 2011 - 11:05 AM

What's the average distance between two forums?

Something else to read, while you wait
Was This Post Helpful? 0
  • +
  • -

#6 Rackie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Re: Finding the student with the highest average grade

Posted 10 November 2011 - 02:33 PM

View PostWabiSabi, on 10 November 2011 - 10:27 AM, said:

View PostRackie, on 10 November 2011 - 11:15 AM, said:

So what i did is wrong is that you mean?


Yes, what you did is wrong (or it would work, yes?)

You want to access an index of your array, correct? But there is no grades[8][3] (grades[numberOfstudents][numberOfgrades]). There are 8 index positions total for the [8] part, and 3 total for the [3] part,
but remember they start at 0. So you could have grades[7][2]. That would be your 8th and 3rd index position (the very last position).

Anything greater than that will be out of bounds of your array.


ok thanks for your help i get it now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1