Sum of Positive and Negative numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 7
Reputation: nuubee is an unknown quantity at this point 
Solved Threads: 0
nuubee nuubee is offline Offline
Newbie Poster

Sum of Positive and Negative numbers

 
0
  #1
Oct 12th, 2008
Hi all,

Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. int one, two, three, four, five, six, seven, eight, nine, ten;
  8. int count;
  9.  
  10. cout << "This program accepts 10 integer numbers, the returns\n";
  11. cout << "the sum of all positive numbers, the sum of all negative\n";
  12. cout << "numbers and the sum of all the numbers." << endl;
  13.  
  14. cout << "Enter 10 whole numbers, each followed by 'Enter': \n ";
  15. cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten;
  16. cout << endl;
  17.  
  18. cout << "The sum of all 10 numbers = ";
  19. cout << one + two + three + four + five + six + seven + eight + nine + ten;
  20. cout << endl;
  21.  
  22.  
  23.  
  24.  
  25. return 0;
  26.  
  27. }

Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.

Any help would be great. I'm not looking for the code, just someone to point me in the right direction.

Many thanks.
Last edited by cscgal; Oct 12th, 2008 at 5:26 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Sum of Positive and Negative numbers

 
0
  #2
Oct 12th, 2008
Its better if you have an array of 10 ints and then put down a for loop to check whether its positive or negative and add them up.

If you have variables like this., IT WILL TAKE A LONG TIME TO WRITE IF LOOPS. AND THEN ADD.

to check if its negative or positive.

you can use.

  1. if(var<0)
  2. {
  3. cout<<"negative";
  4. }
  5. else
  6. {
  7. cout<<"POSITIVE";
  8. }
Last edited by Sky Diploma; Oct 12th, 2008 at 5:22 am.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: nuubee is an unknown quantity at this point 
Solved Threads: 0
nuubee nuubee is offline Offline
Newbie Poster

Re: Sum of Positive and Negative numbers

 
0
  #3
Oct 12th, 2008
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1
Reputation: tomhogans is an unknown quantity at this point 
Solved Threads: 0
tomhogans tomhogans is offline Offline
Newbie Poster

Re: Sum of Positive and Negative numbers

 
0
  #4
Oct 12th, 2008
Originally Posted by nuubee View Post
Hi all,

Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. int one, two, three, four, five, six, seven, eight, nine, ten;
  8. int count;
  9.  
  10. cout << "This program accepts 10 integer numbers, the returns\n";
  11. cout << "the sum of all positive numbers, the sum of all negative\n";
  12. cout << "numbers and the sum of all the numbers." << endl;
  13.  
  14. cout << "Enter 10 whole numbers, each followed by 'Enter': \n ";
  15. cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten;
  16. cout << endl;
  17.  
  18. cout << "The sum of all 10 numbers = ";
  19. cout << one + two + three + four + five + six + seven + eight + nine + ten;
  20. cout << endl;
  21.  
  22.  
  23.  
  24.  
  25. return 0;
  26.  
  27. }

Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.

Any help would be great. I'm not looking for the code, just someone to point me in the right direction.

Many thanks.
To make your program much more efficient I would recommend learning about iterative loops (using 'for' statements) and arrays.

http://www.hitmill.com/programming/cpp/forLoop.htm
http://www.cplusplus.com/doc/tutorial/arrays.html
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 119
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 8
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Sum of Positive and Negative numbers

 
0
  #5
Oct 13th, 2008
It would be good if you learned about looping as tomhogans mentioned, also try and learn arrays, it would make the program easier to read as you wont have so many variables.

To get you started I suggest you try and write it out on paper its not too difficult, in order to calculate the totals of the positive or negative try and divide the number by 2 and if it returns a 0 then its positive else its negative.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: Sum of Positive and Negative numbers

 
0
  #6
Oct 13th, 2008
Here's the code XD
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int numbers[10];
  8. int poscount=0, negcount=0, totalcount=0;
  9. cout << "This program accepts 10 integer numbers, the returns the sum of all positive\n";
  10. cout << "numbers, the sum of all negative numbers and the sum of all the numbers." << endl << endl;
  11. for( int i=0; i<10; i++)
  12. {
  13. cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i;
  14. cin >> numbers[i];
  15. if( numbers[i] > 0 )
  16. poscount += numbers[i];
  17. else
  18. negcount += numbers[i];
  19. }
  20. cout << endl;
  21. cout << "Sum of positive numbers: " << poscount << endl;
  22. cout << "Sum of negative numbers: " << negcount << endl;
  23. cout << "Sum of all numbers: " << poscount + negcount << endl;
  24. system("pause>nul");
  25. return 0;
  26. }
I wouldn't know where to start if I didn't know arrays and the logic of array indexes.

When you state int number; you're asking for the computer to allocate enought memory for 1 integer number to be stored. But when you say int numbers[10]; you're asking for enough space to be allocated for 10 integers, all ten with the tag numbers but you can tell them apart by the index. You really have 10 variables but you can tell them apart by the index number. So you will have 1 integer in numbers[0] through numbers[9]. The max the index number goes for that array is 9, not 10. Then to print out cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i; we're using the index number to show a different number, in this case how many integers have been input so far.
Any questions feel free to pm me. These forums are very active and messages can get lost very easily inside threads.
Last edited by emotionalone; Oct 13th, 2008 at 4:44 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC