![]() |
|
#1
| |||
| |||
| I asked about this the other day. I desperately need some help with this problem. -------------------------------------------------------------------------------- Write a program that reads in ten whole numbers and that ouputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number 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 numbers separately. One person responded and here's the code he gave me. ---------------------------------------------------------------------------------- Declare an int for the input number: Code: int candidate; ...then create a for loop to ask for input 10 times: Code: for (int i=0; i<10; i++){// stuff goes here} and then cin >> to your int: Code: cin >> candidate; Then you can check whether each number is less than or equal to zero. Then add it to a running total you've already declared as an int. So it'll look somewhat like this: Code: int candidate;int posTotal = 0;int negTotal = 0;int allTotal = 0;for (int i = 0; i < 10; i++){cin >> candidate;if (candidate < 0} posTotal = posTotal + candidate;else negTotal = negTotal + candidate;} Then you can just add up the two totals at the end: Code: allTotal = posTotal + negTotal; And you'll have all three totals, which you can then output any way you like. -------------------------------------------------------------- The code he gave me didn't work, it only read the first number I entered in. I really need to get this done since it's due tommorow so please help. |
|
#2
| |||
| |||
| Never mind, I figured it out. Feel free to delete this thread. |
|
#3
| ||||||||||||
| ||||||||||||
| Next time reply to your own thread rather than making a new one so I can actually see it.
__________________
__________________
"I loved the P182 so much that, when my wife's system was all noisy and needed all sorts of cleaning, I bought her one. Then, when I wanted a cat, I bought a P182. The P182 is not a cat per se, but it's still an excellent buy." My System: 日夏子
|
![]() |
|
| Bookmarks |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Urgent help CC | xactly | General Hardware Chat | 1 | 28th May 2008 06:40 |
| Urgent - please help! | teenee23 | Laptops, Mobiles & PDAs | 1 | 18th May 2008 15:49 |
| URGENT Help Please! | teenee23 | Windows Operating Systems | 8 | 3rd Apr 2008 03:56 |
| Urgent, please help | alih786 | General Hardware Chat | 4 | 30th Jan 2008 13:46 |
| Urgent Help Required Please :) | kelvin | Graphics Cards & Monitors | 7 | 30th Dec 2007 12:13 |
| Thread Tools | |
| |