C++ Algorithm.

Write the steps to follow (an algorithm) to read in ten whole numbers and to output the sum of all the numbers greater than zero, the sum of all the numbers less than zero, and the sum of all the numbers. The User enters the ten numbers just once each and the user can enter them in any order. Your solution should not ask the user to enter the positive numbers and the negative numbers separately.


I'm need of help
Do you have any code?
#include<iostream>

using namespace std;

int main()
{
int a[10];
int pos=0;
int neg=0;
int sum=0;
int i, n=5;

for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n;i++)
{
if(a[i]<0)
{
neg+=a[i];
sum+=a[i];
}
else
{
pos+=a[i];
sum+=a[i];
}
}
cout<<pos<<endl;
cout<<neg<<endl;
cout<<sum<<endl;

return 0;
}


Here is the solution.
Looks correct, only thing need to fix is initialize n = 10, since you want user to inout 10 numbers.

Is there any other issue you are facing?
Try use ++i (pre-increment) instead of i++ (post-increment) it makes your loops at lot easier to debug if you use the index variable in your loop. Try only to use i++
if the situations that really demands it. This is just preference tho, however it might save you some trouble in the future =)
Last edited on
Topic archived. No new replies allowed.