### Test average

I have had trouble with loops and don't know what to fill in

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` // 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 #include using namespace 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; } ``````
Last edited on
Have you looked at the examples in http://www.cplusplus.com/doc/tutorial/control/
I'll check that out, thank you.
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
 ``12`` ``````//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.
 `int testScore[stuCount];`

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.
Topic archived. No new replies allowed.