Ok... so I've got a new assignment and this was the question:
"Write a programs that stores 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, check that the number is within the range and store it in the array. After storing all the values, display the average of the 20 values."
You've got an if statement trying to act like a for loop. It doesn't work. You have to actually iterate through the array, for (int i = 0; i < 20; ++i) {
add all of the values to a central value, sum += array[i];
and then average them and output them. std::cout << "Average: " << sum/20 << std::endl;
EDIT:
Read my code, understand it, and then apply it to your program. If you just copy and paste it, you're going to have more problems.
Sum is never assigned a value, thus its starting value is kept (and altered through sum += ..). The starting value can be anything (often the largest negative number, for some reason).
To get proper results, set sum and average to 0 at the beginning.
That's exactly why I did it that way, I told you to understand what I was doing, and then add code in that was similar to what I put in, and not to just copy and paste my code, which you obviously did.
#include <iostream>
usingnamespace std;
int main()
{
constint N = 20;
int array[N];
constint MIN = 10;
constint MAX = 100;
cout << "Enter " << N << " numbers which are between " << MIN << " and " << MAX << endl;
int i = 0;
while( i < N )
{
cin >> array[i];
if ( array[i] < MIN || array[i] > MAX )
{
cout << "Number entered is wrong. Please try again.\n";
continue;
}
i++;
}
int sum = 0;
for ( i = 0; i < N; i++ ) sum += array[i];
cout << "average is:" << sum / N << endl;
return 0;
}
In fact sum could be calculated inside the while loop and in this case the array is redundant