This is an assignment that I am doing for university and I have not had many problems with assignments up until this point. I would expect my program to be working properly as I would expect it to but when going through the console output, I am getting a weird calculation. This program takes input from a text file and calculates the grade for three students who took four tests. Please let me know what I have done wrong. Thank you.
My code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
//global variables
const int STUDENT = 3;
const int TEST = 4;
//function initializers
void getTests(double[STUDENT][TEST]);
double totalTests(double[STUDENT][TEST]);
double averageTests(double[STUDENT][TEST]);
void averageEachTest(double[STUDENT][TEST]);
void averageEachStudent(double[STUDENT][TEST]);
double getHighest(double[STUDENT][TEST]);
void getHighestOfEachTest(double[STUDENT][TEST]);
void getLowestOfEachTest(double[STUDENT][TEST]);
void report(double[STUDENT][TEST]);
//main
int main()
{
double testScores[STUDENT][TEST]{};
getTests(testScores);
report(testScores);
return 0;
}
//**************************************
void getTests(double testScores[STUDENT][TEST])
{
fstream inFile;
inFile.open("test.txt");
if (!inFile)
{
exit(1);
}
for (int student = 0; student < STUDENT; student )
{
for (int test = 0; test < TEST; test )
{
inFile >> testScores[student][test];
}
}
}
//*****************************************
double totalTests(double testScores[STUDENT][TEST])
{
double total = 0;
for (int s = 0; s < STUDENT; s )
{
for (int t = 0; t < TEST; t )
{
total = testScores[s][t];
}
}
return total;
}
//**************************************
double averageTests(double testScores[STUDENT][TEST])
{
double average = 0.0, total = 0.0;
for (int s = 0; s < STUDENT; s )
{
for (int t = 0; t < TEST; t )
{
total = testScores[s][t];
}
}
average = total / (static_cast <double> (STUDENT) * TEST);
return average;
}
//*************************************
void averageEachTest(double testScores[STUDENT][TEST])
{
double average = 0.0, total = 0.0;
cout << "Average for each test:\n";
for (int t = 0; t < TEST; t )
{
total = 0;
for (int s = 0; s < STUDENT; s )
{
total = testScores[s][t];
average = total / STUDENT;
}
cout << "Average score of test " << t 1
<< ": " << average << endl;
}
}
//******************************************
void averageEachStudent(double testScores[STUDENT][TEST])
{
double average = 0.0, total = 0.0;
cout << "Average for each student:\n";
for (int s = 0; s < TEST; s )
{
total = 0;
for (int t = 0; t < STUDENT; t )
{
total = testScores[s][t];
average = total / STUDENT;
}
cout << "Average score of student " << s 1
<< ": " << average << endl;
}
}
//*************************************
double getHighest(double testScores[STUDENT][TEST])
{
double highest = testScores[0][0];
for (int s = 0; s < STUDENT; s )
{
for (int t = 0; t < TEST; t )
{
if (testScores[s][t] > highest)
{
highest = testScores[s][t];
}
}
}
return highest;
}
//************************************
void getHighestOfEachTest(double testScores[STUDENT][TEST])
{
cout << "Highest for each test:\n";
for (int t = 0; t < TEST; t )
{
double highest = testScores[0][t];
for (int s = 0; s < STUDENT; s )
{
if (testScores[s][t] > highest)
{
highest = testScores[s][t];
}
}
cout << "Highest score for test " << t 1
<< ": " << highest << endl;
}
}
//**************************************
void getLowestOfEachTest(double testScores[STUDENT][TEST])
{
cout << "Lowest for each test:\n";
for (int t = 0; t < TEST; t )
{
double lowest = testScores[0][t];
for (int s = 0; s < STUDENT; s )
{
if (testScores[s][t] < lowest)
{
lowest = testScores[s][t];
}
}
cout << "Lowest score for test " << t 1
<< ": " << lowest << endl;
}
}
//*****************************************
void report(double testScores[STUDENT][TEST])
{
cout << fixed << showpoint << setprecision(2);
double totalOfAllTests = totalTests(testScores);
cout << "Total of all tests: " << totalOfAllTests << endl;
cout << "Press enter to continue.\n";
cin.get();
double avgOfAllTests = averageTests(testScores);
cout << "Average of all tests: " << avgOfAllTests << endl;
cout << "Press enter to continue.\n";
cin.get();
averageEachTest(testScores);
cout << "Press enter to continue.\n";
cin.get();
averageEachStudent(testScores);
cout << "Press enter to continue.\n";
cin.get();
double highest = getHighest(testScores);
cout << "Highest of all tests: " << highest << endl;
cout << "Press enter to continue.\n";
cin.get();
getHighestOfEachTest(testScores);
cout << "Press enter to continue.\n";
cin.get();
getLowestOfEachTest(testScores);
cout << "Press enter to continue.\n";
cin.get();
}
//*******************************************
the text file contents:
70
80
90
75
85
90
60
80
80
90
80
75
The console output:
Total of all tests: 955.00
Press enter to continue.
Average of all tests: 79.58
Press enter to continue.
Average for each test:
Average score of test 1: 78.33
Average score of test 2: 86.67
Average score of test 3: 76.67
Average score of test 4: 76.67
Press enter to continue.
Average for each student:
Average score of student 1: 80.00
Average score of student 2: 78.33
Average score of student 3: 83.33
Average score of student 4: -92559631349317830736831783200707727132248687965119994463780864.00
Press enter to continue.
Highest of all tests: 90.00
Press enter to continue.
Highest for each test:
Highest score for test 1: 85.00
Highest score for test 2: 90.00
Highest score for test 3: 90.00
Highest score for test 4: 80.00
Press enter to continue.
Lowest for each test:
Lowest score for test 1: 70.00
Lowest score for test 2: 80.00
Lowest score for test 3: 60.00
Lowest score for test 4: 75.00
Press enter to continue.
C:\Users\antmc\source\repos\REALAssignment20_StudentTestScores_functions arrays\x64\Debug\REALAssignment20_StudentTestScores_functions arrays.exe (process 34320) exited with code 0.
Press any key to close this window . . .
CodePudding user response:
You are seeing an uninitialized value.
cout << "Average score of student " << s 1
<< ": " << average << endl;
If you look at the for loop defining s
then you'll see it is tested against TEST
. You have got the upper condition for you two loops backwards.