I'm trying to call the getAverage function in the last cout on printResults, but when I call it, it just give me the same average for all the cases. For example, if I put that first students grades are 50 50 and 50, the average would come 50 for the second and third even if they have different scores. I tried a loop with the first and second index, but it sums all of the points of each row. I want it to sum only the points of one row and give me the average. Then when I call it again sum the second row and give me the average. All of this without declaring another variable. (The code is an assigment and I have to do it this way)
#include <iostream>
using namespace std;
const int EXAMS = 3;
void getScores(int[][EXAMS], const int);
int getAverage(int[], const int); // this method requires an array and its length as parameters
char getGrade(int);
void printResults(int[][EXAMS], const int);
int main() {
const int STUDENTS = 5;
int scores[STUDENTS][EXAMS] = {0};
printResults(scores, STUDENTS);
cout << endl;
system("pause"); // for Visual Studio only
return 0;
}
void getScores(int scores[][EXAMS], const int students)
{
for (int i = 0; i < students; i++)
{
for (int j = 0; j < EXAMS; j++)
{
cout << "Enter the score for student #" << i + 1 << ", test #" << j + 1 << ": ";
cin >> scores[i][j];
}
cout << endl;
}
}
// The getAverage method receives an array and its length as parameters, and
// returns an integer value. It computes the average by adding the values in
// the array, which are stored in the integer variable sum, and then dividing
// by the array's size. Use a for iteration control structure to add all the values.
int getAverage(int scores[][EXAMS], const int students)
{
int sum = 0;
for (int i = 0; i < EXAMS; i++)
{
sum += scores[0][i];
}
return sum / EXAMS;
}
// DO NOT DECLARE any variables in this method.
char getGrade(int finalAverage)
{
if (finalAverage < 59)
return 'F';
else if (finalAverage < 69)
return 'D';
else if (finalAverage < 79)
return 'C';
else if (finalAverage < 89)
return 'B';
else
return 'A';
}
// DO NOT DECLARE any variables in this method.
void printResults(int scores[][EXAMS], const int students)
{
getScores(scores, students);
for(int j = 0; j < students; j++)
{
cout << "The student with test scores ";
for (int i = 0; i < EXAMS; i++)
{
if (i == EXAMS - 1)
cout << "and " << scores[j][i] << ", ";
else
cout << scores[j][i] << ", ";
}
cout << "scored a final average of " << getAverage(scores, students) << " and earned a(n) " << getGrade(getAverage(scores, students)) << endl;
}
}
I would solve it this way, without the function. But the assignment reequires me to do it with the function and without declaring any variables in the printResults method.
void printResults(int scores[][EXAMS], const int students)
{
getScores(scores, students);
for(int j = 0; j < students; j++)
{
int sum = 0;
cout << "The student with test scores ";
for (int i = 0; i < EXAMS; i++)
{
if (i == EXAMS - 1)
cout << "and " << scores[j][i] << ", ";
else
cout << scores[j][i] << ", ";
sum += scores[j][i];
}
cout << "scored a final average of " << sum / EXAMS << " and earned a(n) " << getGrade(sum / EXAMS) << endl;
}
}