Problem is:-
Write a program that reads in ten whole number and then outputs the sum of only all positive numbers, sun of only all negative or zero numbers and also sun of all ten numbers including positive and negative. The user enters the ten number once in any order. Your program should not ask the user to enter the positive numbers and negative numbers separately.
Please tell me how to solve it?
That would be the long way of doing it, but it works. Create a variable for the sum of positive numbers, negative numbers, and whatever sum you need. Make if statements, to check what it is, depending if it's a positive or negative or whatever, put it in one of the variables.
I would recommend learning how to use arrays/vectors (use vectors), and how to use loops (specifically a for-loop in this case) before you continue.
Yes, done it after thinking a lot. But still have a problem. How can I sum all postie and negative numbers. Mean I am still stopped at sum_all variable.
#include <iostream>
using namespace std;
int main()
{
int num, count = 1, sum_positive = 0, sum_negative = 0, sum_all;
do
{
cout << "Enter ten whole numbers\n";
cin >> num;
if (num > 0)
{
sum_positive = sum_positive + num;
}
else
{
sum_negative = sum_negative + num;
}
count++;
} while (count <= 10 );
cout << "Sum of your positive numbers are" << sum_positive << endl;
cout << "Sum of your negative numbers are" << sum_negative << endl;
return 0;
}
Ok, now I am on the second part of problem, in which program also have to find average of all positive numbers,average of all negative numbers, all and average of all postie + negative numbers. I have done this coding but computer is giving error. Why?
Error is "Unhandled exception at 0x00b51675 in jk.exe: 0xC0000094: Integer division by zero."
Compiler gives this error when it reaches at avr_all variable mean line# 42 other programs work fine.
Let's try this again... You do not know more than your compiler, please provide the full and exact error message... The code works perfectly fine for me.
If it is not working in this code you provided then Im afraid I dont know, to my knowledge it should work perfectly fine, which it does on visual studio 2013/2015 and on cpp.sh.
Here how can the sum of negative number greater than 0. Ha ha ha (Laughing).
First that is why compiler skip it and says avr_neg is not initialized and when we have set it to zero then program was working but all the time it was giving same answer which was 0.