Test average

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  // 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>
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
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.
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.