Array help

I'm new to C/C++ and I'm trying to grasp arrays. Here is where I'm at so far with this program. Essentially I want it to be able to print out the entire array of numbers inputed by the user and then calculate the average based on those numbers and how many were input (max 10.) The issue I'm running into is that I want the user to be able to use a negative number if they only want to input say 4 grades, they could type "-1" and it will calculate the average and display those grades.

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
#include <iostream>
using namespace std;

int main()
{
	const int max = 10;
	int number[max];
    
	int sum = 0;
    
	cout << "Please input 10 grades.\n";
    
	for( int i = 0; i < max; i++ )
	{
		cout << "Grade " << i + 1 << ": ";
		cin >> number[i];
        if (number[i]<=-1)
        {
            i=max;
        }
		sum += number[i];
        
	}
     
   
    
	cout << "\n\nThe sum of these numbers is " << sum << "\n\n";
    
    
    for (int i=0; i<=max; i++)
    {
        cout << "grade inputed #" << i << " is:" << number[i] << endl;
    }
	return 0;
}
If you are restricted to arrays, then you should know that you either have to copy the entire array into a new larger one when you run out of space, or just keep a high maximum amount of values.

Other than that, just keep track of how many value the user entered with some count variable.
If this isn't a school project and I say this because they have criteria you are restricted to using, I would use a vector for this instead since it will give you a count and grows as you need it only being as large as you need it. Vectors are fairly easy to use.
Topic archived. No new replies allowed.