-2

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 $  
  • Where do you set a value for `number_Of_Positive` and `number_Of_Negative`? – NathanOliver Aug 24 '18 at 16:43
  • `if ( userInput >= 0) //is even` What? Forgot to fix the comment? – tkausl Aug 24 '18 at 16:43
  • Yup, putting = 0 at the end of a list of int variables only sets the last variable to 0, rather than all of them, instead you must do something like int a = 0, b= 0 etc.. – Phill Aug 24 '18 at 16:43
  • 1
    Some people recommend to define one variable per line. – Slava Aug 24 '18 at 17:01
  • "`int number_Of_Positive, number_Of_Negative, areZero, userInput, sum, avg = 0;`" - that *only* initialises `avg` to `0`. The other variables are uninitialized. – Jesper Juhl Aug 24 '18 at 17:54

2 Answers2

3

You are accessing uninitialized variables. For example, with sum += userInput, you access variable sum, but sum has not been assigned an initial value. That yields undefined behaviour, and so the output of your program is not predictable.

Note that int number_Of_Positive, number_Of_Negative, areZero, userInput, sum, avg = 0 only initializes variable age but leaves all others un-initialized.

You should write int number_Of_Positive=0, number_Of_Negative=0, areZero=0, userInput=0, sum=0, avg = 0;

Maybe there are other issues in terms of program logic, but initializing the variables is the very first thing before proceeding.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

It is recommended to have one declaration per line and do it right before variable is needed. Also using sgn() function from Is there a standard sign function (signum, sgn) in C/C++? you can make your code much simpler:

int counters[3] = {};
auto sum = 0;
for(int i = 0; i < 10; i ++) {
    auto userInput = 0;
    cin >> userInput; 
    sum += userInput;
    auto sgn = (userInput > 0) - (userInput < 0); // -1 for negative, 0 for 0 and +1 for positive
    ++counters[sgn + 1];
}

now counters[0] will have negative number count, counters[1] zeros, and counters[2] bigger than zero. Note counters[2] will have count for >0 but if you need output for positives you can simple add: counters[1] + counters[2]

Slava
  • 43,454
  • 1
  • 47
  • 90