Thanks for that.
Whatever I said before obviously was not clear.
Array subscripts start from 0. That's why both max and min are set to the first array element a[0];
But it also means that when reading in the data from the user, you should do this, starting from zero:
1 2 3 4 5 6 7
|
cout << "How many students you want to enter ? [" << students << ":MAXIMUM] : ";
cin >> n;
for (int i=0; i<n; i++)
{
cout << "Score #" << i+1 << " : ";
cin >> a[i];
}
|
Note the
i<=n
is changed to
i<n
as well. It does mean that in order to display in human-friendly form, the value
i+1
is displayed in the cout statement.
Incidentally, I changed the 200 to students in the information prompt at the start.
how to i avoid declaring uninitialised variables ? |
Don't declare a variable until you are ready to assign a value to it. Rather than declaring max, min and i somewhere near the start of the code, don't declare until point of use.
1 2
|
int max=a[0];
int min=a[0];
|
The same applies to
average
. Rather than declare it at the start, wait till you actually need to use it.
At line 24:
You again need to change
i<=n
to
i<n
It would be ok to start the loop from i=0, but since you already stored those as the current max and min, in this case it is ok to start counting from the second element.
Notice also I declared the variable i within the for loop. It's a good idea to limit the scope of variables as narrowly as possible, in order to avoid unexpected side-effects elsewhere.
Only
n
and
sum
need to be declared before being used (which you have done correctly).
I hope this is a bit clearer (maybe I should just post the complete code, but it seemed to me these were just a few small changes here and there).