// This program calculates an average test score given individual test scores.
// Input: Interactive - Student Test Scores
// Output: Number of Students taking the test and the test score average
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
// Declare variables
int testScore;
int numStudents;
int stuCount;
double testTotal;
double average;
// This is the work done in the housekeeping() function
// Get user input to control loop
cout << "Enter number of students: ";
cin >> numStudents;
// Initialize accumulator variable to 0
testTotal = 0;
// This is the work done in the detailLoop() function
// Loop for each student
// Calculate average test score
average = testTotal / stuCount;
// Output number of students and average test score
cout << "Number of Students: " << stuCount << endl;
cout << "Average Test Score: " << average << endl;
return 0;
}
Since you are trying to calculate the average of all test scores of a class, you may need to use arrays. I see that you declare int testScore and int stuCount, but does every student have the same test score?
This is something you may want to consider
1 2
//Create an array testScore that corresponds to how many students there are in the classroom
int testScore[stuCount];
Now, your testTotal must be all of the testScore variables added together. The adding is done inside the loop.
Also, what's the difference between your int numStudents and int stuCount? It seems somewhat redundant to me... Judging by the names, are they meant to have the same value? If so, you only need one of them.
That one has an issue: the code/memory allocation for static array is decided already during compilation of the binary, but the number of students is revealed only when program is run.
Therefore, dynamic allocation of the array is required. While one can do that manually, it is better to use the std::vector container from C++ Standard Library.
The computation of average does not require any array.
Average is (sum of scores) / (count of scores)
One does need the actual scores only for computing the sum.