Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program for adding positive and negative #s 1

Status
Not open for further replies.

ICU2Desktop

Technical User
Jul 12, 2002
11
US
Hi, as you can probably see I am a beginner with C++ and I am trying to write a program that reads in 10 whole #’s and outputs the sum of all #’s greater than 0, the sum of all numbers less than 0 ( which are negative #’s and 0 ) and the sum of all the #’s. I have been able to get the results for the sum of all #’s, but unable to produce the first the others. Any help would be greatly appreciated. Here is what I came up with:
#include <iostream>
using namespace std;

int main( )
{
int n1, n2, n3, n4, n5, n6 , n7 , n8, n9 ,n10, sum_one, sum_two, total_sum;

cout << &quot;Enter ten whole numbers randomly positive or negative:\n&quot;;
cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6 >> n7 >> n8 >> n9 >> n10;


sum_one = (n1>0)+(n2>0)+(n3>0)+(n4>0)+(n5>0)+(n6>0)+(n7>0)+(n8>0)+(n9>0)+(n10>0);
sum_two = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10 <= 0 ;
total_sum = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10 ;


cout << &quot;The first output will be the sum of all\n&quot;
<< &quot;numbers which are greater than zero\n &quot;;
cout << sum_one;
cout << &quot;The second output will be the sum of all\n &quot;
<< &quot;numbers which are less than or equal to zero\n &quot;;
cout << sum_two;
cout << &quot;The third output will be the total sum of all numbers\n &quot;;
cout << total_sum;


return 0;

}
 
Try this at first:
......
int n[10], sum_one = 0, sum_two= 0, total_sum = 0, count;

for ( count = 1; count < 11 ; count++ )
{
cout << &quot;Enter ten whole numbers randomly positive or negative:&quot; << count<< ENDL;
cin >> n[count];
}

for ( ; count > 0; count--)
{
if ( n[ count ]>0) sum_one = sum_one + n[count];
else sum_two = sum_two + n[count];
}
total_sum = sum_one + sum_two;
......
Of course you can do this with one loop;
Now after you read this please get your study book and start reding.
good luck
 
Thanks for pointing me in the right direction of using if-else. Unfortunately, I am still not able to get the desired results and my study book is not very detailed, so I am having a hard time getting this.
 
Here are some of your problems:

sum_one = (n1>0)+(n2>0)... the comparison (n1 > 0) is a boolean ( true/ false ) comparison. (n1 > 0) will return a 0 for false, and a 1 for true. Therefore you aren't getting the sums of all positive numbers, you're getting the count of them.

As it sounds like you've figured out, you need to have a statement like:

if ( n1 > 0 )
sum1 = sum1 + n1; //i.e. sum1 += n1;

This statement has a similar problem:

sum_two = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10 <= 0 ;

Because of C++ operator precedence, the last statement evaluates thusly:


sum_two = ( n1+n2+n3+n4+n5+n6+n7+n8+n9+n10 <= 0 );

Which will assign sum_two a 0 or a 1 based on if the sum of n1 through n10 is greater than or equal to 0.

Since you can only have two cases (n1 > 0 or n1 <= 0 ) you can make a comparison like aaamil3 did:

if ( n1 > 0 )
sum1 = sum1 + n1; //i.e. sum1 += n1;
else
sum2 = sum2 + n1; //i.e. sum2 += n1;

To get the correct results, however, you need such a comparison for each term nX. That's where an array and a for loop would simplify the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top