### using a loop to get sum

I'm suppose to have them average out the sum_greater_than_zero/ (number of positive numbers inputted)
and

sum_less_than_zero / (number of negative numbers inputted)

But I can't figure out how to do so

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main() { double sum_pos = 0, sum_neg = 0,num[10],numneg[10],numpos[10],mean = 0; int i; for(i=0;i<10;i++){ std::cout << "Enter a number:"; std::cin >> num[i]; if (num[i] > 0) sum_pos = sum_pos + num[i]; if (num[i] < 0) sum_neg = sum_neg + num[i]; } cout << "The sum of the positive numbers is: " << sum_pos << endl; cout << "The average of the positive numbers is: " << (sum_pos/num[i]) <

Last edited on
it works fine, but you should change the while function.
 `` `` ``while (number <= 11)``
Last edited on
I did that but I'm having a problem:
if you enter 10 it finishes the program.

also the cout statements at the end:

I'm suppose to have them average out the sum_greater_than_zero/ (number of positive numbers inputted)
and

sum_less_than_zero / (number of negative numbers inputted)

I think your average values are a bit off. The average for negative numbers is the sum / number of negative numbers. Same with positive not the sum of each type / total of all numbers. Also you are incrementing number by 2 each time. Maybe you meant braces around your if statements? Either way if its not greater than 0 it has to be less than or equal so you can simply use an else statement and then after adding to the sum you can simply increment the count by 1.

 ``1234567`` `````` if (number > 0) sum_greater_than_zero = sum_greater_than_zero + number; number++; if (number < 0) else sum_less_than_zero = sum_less_than_zero + number; number++;``````

Fixes the second issue addressed now for the first you will want two more variables.
`int positiveNumbers = 0 , negativeNumbers = 0;`

Then fix the if statements and your average:

 ``1234567891011`` ``````if (number > 0) { sum_greater_than_zero = sum_greater_than_zero + number; ++positiveNumbers; } else { sum_less_than_zero = sum_less_than_zero + number; ++negativeNumbers; } ++number;``````

and

 ``123`` ``````cout << "The average of the positive numbers is: " << (sum_greater_than_zero/positiveNumbers) <

Keep in mind you are doing integer division also I would suggest you use double division if you want more accurate results. 1 / 2 = 0 with ints 1.0 / 2.0 = 0.5 with doubles. It floors ( rounds down ) it in other words .99999 in int is considered 0.
Thanks giblit!

I was trying to do the second issue you told me about but I was putting ++ at the end and kept getting initialization errors.
This is my new code:

 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int main() { double sum_greater_than_zero = 0, sum_less_than_zero = 0,num[10]; int i; int positiveNumbers = 0 , negativeNumbers = 0; for(i=0;i<10;i++){ std::cout << "Enter a number:"; std::cin >> num[i]; if (num[i] > 0) { sum_greater_than_zero = sum_greater_than_zero + num[i]; ++positiveNumbers; } else { sum_less_than_zero = sum_less_than_zero + num[i]; ++negativeNumbers; } } cout << "The sum of the positive numbers is: " << sum_greater_than_zero << endl; cout << "The average of the positive numbers is: " << (sum_greater_than_zero/positiveNumbers) <

runs smooth thanks for the help!
Last edited on
Topic archived. No new replies allowed.