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.
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?
//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++;
}
elseif (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.
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++;
}
elseif (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.
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.