Super nub, needs brainstorm ideas for ?

so trying to solve this problem:

write a program that reads in ten whole numbers and that outputs the sum of all numbers greater than zero, the sum of all the numbers less than zero(which will be negative or zero), and the sum of all the numbers, whether positive , negative, or zero. the user enters the ten numbers just once each and the user can enter them in any order. your program should not ask the user to enter the positive numbers and the negative number separately.
only allowed to use 4 variables max!
i only started programing 3 days ago, so all i know is the very basic things
i.e. "while" loops, "if-else", constants. so i cant really use any other ways to try n solve this problem.
variable that ive got are

int numbers, pos_sum, neg_sum, sum_all
using code block btw, any ideas are welcome and thanks
I wont write out the code for you as it will be a better learning experience if you figure out how to do it yourself but will give you the basic layout.

First you need to set up a loop that with loop 10 times (for the 10 numbers). I would usually use a for loop for this but you could use a while loop if you don't know for loops yet. At the beginning of each loop you need to ask the user for a number then check to see if that number is above or below 0 (and either add it to pos_sum or neg_sum depending on the case). Then after the loop has completed print out the results.

(Also if you think about it for a while you will see you don't need a "sum_all" variable).
Last edited on
I can't see what you mean by I don't need sum_all because I'm thinking near the end of the program don't I need a variable to add the neg_sum and pos_sum and shows as sum_all? Btw not asking for code just asking what I'm doing wrong and stuff.
another question, how do i limit the number of loops to 10 times if i have more then one "while" loop.
this is what i got so far. i know my "parameters" are wrong cuz i get a infinite loop

int mark, pos_sum(0), neg_sum(0);
if (mark >= 0)
{
while (mark > 0 )
{
cout << "Enter any positive or negative whole number:\n";
cin >> mark;
pos_sum = pos_sum + mark;
}
}
else
{
while (mark < 0)
{
neg_sum = neg_sum + mark;
}

}
cout << pos_sum << " is the sum of the positive numbers.\n";
cout << neg_sum << " is the sum of the negative numbers.\n";
int sum_all;
sum_all = pos_sum + neg_sum;
cout << sum_all << " is the sum of all the numbers you inputed.\n";
To limit your loop to run only 10 times, try making a counter variable. Let's say you have x, at the end of every loop, you increment x by 1. The condition of the loop could be while(x<=10). Your if statements are in an odd spot. I don't think this does what you're expecting. Try having an if after the input, to test positive or negative.
Numbers: -4, 4, 5, -2, -7, -5, 3, 3, -4, -3

Now how would we do this by hand. (Without regrouping the numbers.)

----------------------------------------------------------------------------------------
Let's take the first number.
Is the number positive or negative?
Negative.
So add it to our negative total (which starts at 0)
Our negative total is now -4

Let's take the next (second) number.
Is the number positive or negative?
Positive.
So add it to our positive total (which starts at 0)
Our positive total is now 4

Let's take the next (third) number.
Is the number positive or negative?
Positive.
So add it to our positive total (which is now at 4)
Our positive total is now 9

Etc...
----------------------------------------------------------------------------------------

So you need to try and write that algorithm in C++
THANKS TO ALL WHO RESPONDED! Helped me tons, mathhead200, residentbiscuit, james2250

added count++ variable for limits, made all into 1 while loop , moved the if-else statements inside, and it worked.
Im sure ill have a Ton more questions if this nub question really stumped me.
Topic archived. No new replies allowed.