Test Average

Can someone explain to me why my array size doesn't let me run the program

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  
#include <iostream>
#include <string>
using namespace std; 
int main()
{
   // Declare variables
	int SIZE;
   int testScores[SIZE];
   int numStudents; 
   int stuCount;
   int testCount;
   double testTotal;
   double average;
   int sum;
   
				
   // This is the work done in the housekeeping() function
   // Get user input to control loop
   cout << "Enter number of students: ";
   cin >> numStudents;	

   SIZE = numStudents;
		
   // Initialize accumulator variable to 0
   testTotal = 0; 
  

   // This is the work done in the detailLoop() function
   // Loop for each student
	stuCount = 0;
	
	cout << "How many tests will each student be taking?";
	cin >> testCount;
	
	
	//User enters test scores of amount of students taking the test
	while (stuCount <= numStudents)
	{
		cout << "Enter the test score for " numStudents " students";
		cin >> testScores[]; 
		
		stuCount++;
	}
	
	//Gets the sum of the array
	for (int a=0; a=<numStudents; a++)
	{
		testTotal+=testScore[a]; 
	}
 
   // Calculate average test score
   average = testTotal / stuCount;
   // Output number of students and average test score
   cout << "Number of Students: " << numStudents << endl;
   cout << "Average Test Score: " << average << endl;
		
   return 0;
}
Hello ddaniel10,

Because "SIZE" needs to be a constant as in constexpr int SIZE{ 10 }; or const int SIZE{ 10 };. The first is a newer form. The capital letters let you know that it is defined as a constant and can not be changed. This means line 23 will not work.

When you define an array the size of the array needs to be a constant value. This could be a variable defined as a constant or a number like 10. At compile time the compiler needs to know how much space to set aside for the array. That is why it needs to be a fixed number. If you need an array based on a variable then you will need to create a dynamic array.

I will have to check into the rest of the program in a bit.

Hope that helps,

Andy
Hello ddaniel10,

You should compile the program before you post the code. This way if there are any errors that you do not understand you can add them to the message.

Once I made some changes I found more problems.

The while loop is not doing what it should. It could be made to work, but a for loop or loops would work better. As it is it is not working to collect the test scores from all the students.

Variables like "testTotal" and "stuCount" lines 26 and 31. These variables can be initialized when they are defined unless there is a need to do it later.

You need to re think what you need to do and make the program do it.

Hope that helps,

Andy
Topic archived. No new replies allowed.