943,621 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6413
  • C++ RSS
Oct 12th, 2008
0

Sum of Positive and Negative numbers

Expand 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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nuubee is offline Offline
7 posts
since Oct 2008
Oct 12th, 2008
0

Re: Sum of Positive and Negative numbers

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 12th, 2008
0

Re: Sum of Positive and Negative numbers

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nuubee is offline Offline
7 posts
since Oct 2008
Oct 12th, 2008
0

Re: Sum of Positive and Negative numbers

Click to Expand / Collapse  Quote originally posted by nuubee ...
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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhogans is offline Offline
1 posts
since Sep 2008
Oct 13th, 2008
0

Re: Sum of Positive and Negative numbers

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008
Oct 13th, 2008
0

Re: Sum of Positive and Negative numbers

Here's the code XD
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Light Poster
emotionalone is offline Offline
33 posts
since Oct 2008
Mar 19th, 2010
-1
Re: Sum of Positive and Negative numbers
sir how bout of we count the number of inputted positive integers, negative and zero and not its sum..

can you revise it for me boss... thanks alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piyota is offline Offline
2 posts
since Mar 2010
Mar 19th, 2010
-1
Re: Sum of Positive and Negative numbers
sorry for flooding... i figured out how to count the positive numbers i just replaced the "numbers[i]" to "1" ...

thanks for the codes anyway.. big help for the completion of my project..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piyota is offline Offline
2 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to build an import library in vs2008
Next Thread in C++ Forum Timeline: problems with Template classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC