I'm more interested in understanding why my output is incorrect than a different solution. Please see code comments. It seems that the sum calculation makes an error depending on what is commented out, but as you can see in the output my areZero variable is initialized to 0 and produces a random number, same for number_Of_Positive. Also, I entered -1 for a value and the number_of_Negative prints 0. Really not sure what's going on here.
/*
Use a loop, accept 10 integers and do:
Count and print out how many of the entered numbers are positive.
Count and print out how many of the entered numbers are negative.
Count and print out how many of the entered numbers are zeroes.
Calculate and print the sum of all the numbers entered.
Calculate and print the average value of all the numbers entered.
*/
int number_Of_Positive, number_Of_Negative, areZero, userInput, sum, avg = 0;
cout << "Enter 10 integers" << endl;
for(int i = 0; i < 10; i ++) {
cin >> userInput;
sum += userInput;
if (userInput == 0)
areZero++;
if ( userInput >= 0) //is even
number_Of_Positive++;
else //is odd
number_Of_Negative++;
}
avg = sum / 10;
cout << "The sum is " << sum << endl;
cout << "the average is " << avg << endl;
cout << "Positive " << number_Of_Positive << " Negative " <<
number_Of_Negative << endl;
cout << "Zero " << areZero << endl;
}
**//The OUTPUT**
ec2-user:~/environment/CPPBasics1 $ g++ ten_integers.cpp -o ten_integers
ec2-user:~/environment/CPPBasics1 $ ./ten_integers
Enter 10 integers
-1
0
1
2
3
4
5
6
7
8
The sum is 35
the average is 3
Positive -554687472 Negative 0
Zero 4196384
ec2-user:~/environment/CPPBasics1 $