5 Replies - 215 Views - Last Post: 05 November 2017 - 12:41 AM Rate Topic: -----

#1 CooolGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 17

Simple fix but I have no idea what I'm doing wrong

Posted 04 November 2017 - 07:46 PM

This program is supposed to let the user enter 5 students test scores for three classes and calculate the average for each student. I just can't figure out what I'm doing wrong. Thank you!

#include<iostream>
using namespace std;

int main()
{
	const int NUM_STUDENTS = 5; 
	const int NUM_SCORES = 3; 
	double grade[NUM_STUDENTS][NUM_SCORES]; 
	double total = 0;
	double average = 0;
	int row, col;
	

	for (row = 0; row < NUM_STUDENTS; row++)
	{
		for (col = 0; col < NUM_SCORES; col++)
		{
			cout << "Enter the score for Student " << row + 1 << " Class " << col + 1 << ": ";
			cin >> grade[row][col];
		}
		cout << endl;
	}

	for (row = 0; row <= NUM_STUDENTS; row++)
	{
		for (col = 0; col < NUM_SCORES; col++)
		{
			total += grade[row][col];
			average = total / NUM_SCORES;
			cout << "Score average for student " << row + 1 << " is: " << average << endl;
		}
	}
	return 0;
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Simple fix but I have no idea what I'm doing wrong

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Simple fix but I have no idea what I'm doing wrong

Posted 04 November 2017 - 08:05 PM

This block of code

 for (row = 0; row <= NUM_STUDENTS; row++)

    {

        for (col = 0; col < NUM_SCORES; col++)

        {

            total += grade[row][col];

            average = total / NUM_SCORES;

            cout << "Score average for student " << row + 1 << " is: " << average << endl;

        }

    }




the average should only be calculated once the three scores have been added to total
the cout statement again should only be active when you have a valid average

and lastly total should be reset to 0 outside of the inner loop this is because you only want to calculate the total for one student not all of them.
Was This Post Helpful? 0
  • +
  • -

#3 CooolGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 17

Re: Simple fix but I have no idea what I'm doing wrong

Posted 04 November 2017 - 08:09 PM

**************Edit*************
****excessive quote removed****
Makes sense, how would you go about re-writing it. I'm not quite sure.

This post has been edited by snoopy11: 04 November 2017 - 09:13 PM
Reason for edit:: excessive quote from snoopy11 removed please dont place large quotes in posts !

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Simple fix but I have no idea what I'm doing wrong

Posted 04 November 2017 - 09:16 PM

You have now been given enough information to solve your problem,

please make an effort,

and post updated code along with specific questions...
Was This Post Helpful? 0
  • +
  • -

#5 CooolGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 17

Re: Simple fix but I have no idea what I'm doing wrong

Posted 04 November 2017 - 11:36 PM

Here's the finished product! Thank you so much for your help. I finished it before I read your last reply. I shouldn't have asked what you would've written. Won't learn anything that way lol. Btw, I added the rest of the code for the assignment. I had to display the average for each student as well as each entry. Thank you again!

int main()
{
	const int NUM_STUDENTS = 5; 
	const int NUM_SCORES = 3; 
	double grade[NUM_STUDENTS][NUM_SCORES]; 
	double total = 0;
	double average = 0;
	int row, col;
	

	for (row = 0; row < NUM_STUDENTS; row++)
	{
		for (col = 0; col < NUM_SCORES; col++)
		{
			cout << "Enter the score for Student " << row + 1 << " Class " << col + 1 << ": ";
			cin >> grade[row][col];
		}
		cout << endl;
	}

	cout << "*********************************************" << endl;

	for (row = 0; row < NUM_STUDENTS; row++)
	{
		total = 0;
		for (col = 0; col < NUM_SCORES; col++)
		{
			total += grade[row][col];
			average = total / NUM_SCORES;
		}
		cout << fixed << setprecision(2);
		cout << "Score average for student " << row + 1 << " is: " << average << endl;
		cout << endl;
	}

	for (row = 0; row < NUM_STUDENTS; row++)
	{
		for (col = 0; col < NUM_SCORES; col++)
		{
			cout << "The score for student " << row + 1 << " in class " << col + 1 << " was: " << grade[row][col] << endl;
		}
		cout << endl;
	}

	cout << "*********************************************" << endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Simple fix but I have no idea what I'm doing wrong

Posted 05 November 2017 - 12:41 AM

You're Welcome,

Glad you got it finished.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1