Implementing a for loop.

I'm honestly at a loss as to what to do here. I know it's wrong but I do not know what to do differently. Any advice?
The question i'm trying to solve is:
Write a program that reads in ten whole numbers and outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative or zero. The user enters the ten numbers one at a time and the user can enter them in any order. The program should not ask to enter the positive and negative numbers separately. The program should also display the numbers used.

1) Modify the above program so that it outputs the sum of all positive numbers, the average of all positive numbers, the sum of all nonpositive numbers, the average of all nonpositive numbers, the sum of all numbers and the total average.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     int i = 1; int numbers;
    int sumofpositive, avgofpositive, sumofnegative, avgofnegative,  sum;
    
    for (i= 1; i < 11; i++)
    {
        cout << "Enter a number: ";
        cin >> numbers;
    if(numbers > 0)
    {
        sumofpositive = (sumofpositive + numbers);
        avgofpositive = (sumofpositive) / 10;
    }
    else 
    {
        sumofnegative = (sumofnegative + numbers);
        avgofnegative = (sumofnegative) / 10;
    }   
    }
     cout << "Sum of positive numbers: " << sumofpositive;
     cout << "Average of positive numbers: " << avgofpositive;
     cout << "Sum of negative numbers: " << sumofnegative;
     cout << "Average of negative numbers: " << avgofnegative;
     cout << "Sum of all numbers: " << (sumofpositive + sumofnegative); 
What do you see to be wrong?
* Does not compile?
* Does crash?
* Output differs from expected?
You should calculate the average after the for loop has finished.
The average is not output correctly. The average calculates for the positive numbers but does not for the rest. Will it calculate differently outside of the loop?
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
    //Problem 1
    int i = 1; int numbers; int x, y;
    int sumofpositive, sumofnegative, sum;
    
    for (i= 1; i < 11; i++)
    {
        cout << "Enter a number: ";
        cin >> numbers;
    if(numbers > 0)
    {
        sumofpositive = (sumofpositive + numbers);
        x = x++;
    }
    else if (numbers < 0)
    {
        sumofnegative = (sumofnegative + numbers);
        y = y++;
    }   
    }
     cout << "Sum of positive numbers: " << sumofpositive;
     cout << "Average of positive numbers: " << (sumofpositive / x);
     cout << "Sum of negative numbers: " << sumofnegative;
     cout << "Average of negative numbers: " << (sumofnegative / y);
     cout << "Sum of all numbers: " << (sumofpositive + sumofnegative); 
     cout << "Average of all numbers: " << (sumofpositive + sumofnegative) / 
             (x + y);

I have tried to fix it again. But now it doesn't display any of the couts at the end.
I figured I have no way of predicting how many positive or negative inputs the user will input so I added the x++ and y++ to try to have that to divide by for the average. I feel like I'm closer but it doesn't work as it stands. It asks me for my inputs and that's it.
Initialize your variables to some reasonable value. Currently, they contain junk. And when you add junk to junk, you get more junk.
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
    int i = 1; int numbers; int x = 0, y = 0;
    int sumofpositive, sumofnegative, sum;
    
    for (i= 1; i < 11; i++)
    {
        cout << "Enter a number: ";
        cin >> numbers;
    if(numbers > 0)
    {
        sumofpositive = (sumofpositive + numbers);
        x++;
    }
    else if (numbers < 0)
    {
        sumofnegative = (sumofnegative + numbers);
        y++;
    }   
    }
     cout << "Sum of positive numbers: " << sumofpositive;
     cout << "Average of positive numbers: " << (sumofpositive / x);
     cout << "Sum of negative numbers: " << sumofnegative;
     cout << "Average of negative numbers: " << (sumofnegative / y);
     cout << "Sum of all numbers: " << (sumofpositive + sumofnegative); 
     cout << "Average of all numbers: " << (sumofpositive + sumofnegative) / 
             (x + y);

I now get the correct calculation for negative numbers for sum and average.
The rest - are wrong.
I don't understand... haha.
I thought I made sure to use similar formatting between negative and positive numbers so I am not sure why only the negative numbers come out correct.
I would again say initialize your variables to some reasonable value. You've initialized some of them. Initialize the rest.
hmm... okay now I have something to play around with and test it.
Thank you so much, I'll try and mess around with that for 20 minutes haha
I fixed all of the variables in line 2 and it works properly now!
Thank you so much.
That was very quick to fix once I knew what was wrong
If I (the user) type only positive numbers, then y remains 0 and you divide by 0 to get average of negative numbers, which there were none. Same if no positive values are in the input.

You do need conditionals in the "show the results" part.
Do you think I should form another if/else statement in my show the results part to mitigate that?
Topic archived. No new replies allowed.