|
-
September 21st, 2008, 07:02 PM
#1
Help!
I am trying to write a code that asks for 10 whole numbers to be entered. The program will then compute the sum of the numbers greater than zero, the sum of the numbers less than zero, and then the total sum. I can not get the program to do this for me no matter what I do. PLEASE HELP!!! Also I am using "Pico"
-
September 21st, 2008, 07:19 PM
#2
Re: Help!
Perhaps if you posted your efforts so far.
-
September 21st, 2008, 07:37 PM
#3
Re: Help!
include <iostream>
using namespace std;
int main (void)
{
int count=0;
int number;
int sum = 0;
int sum_greater, sum_less, total_sum;
while (count < 10)
{
cout << "Enter a whole number, then press enter:\n";
cin >> number;
count++;
if (number > 0)
{
cout << "The sum greater than zero is:\n";
sum_greater = sum + number;
cout << sum_greater;
}
}
return 0;
}
If you enter this code into pico or whatever you use you will see the problem I am having.
-
September 21st, 2008, 07:46 PM
#4
Re: Help!
1) You never modify sum, so your sum_greater = sum + number statement is equivalent to just plain sum_greater = 0 + number;
2) You never initialize sum_greater. If it's starting value is 54243423, which it may well be, your final output won't be useful.
-
September 21st, 2008, 07:56 PM
#5
Re: Help!
Even so, when I run this program it doesnt take ten numbers and then compute. It does it for each number entered until there is 10 entries. I know the if else statement needs to be in the loop. I will do the things you said, but how do I get the program to take ten numbers and then add them?
-
September 21st, 2008, 09:41 PM
#6
Re: Help!
You'd need to input the 10 numbers into separate variables (typically an array of size 10), and then have two loops: One to read the values, and the second to operate on them.
However, there's an easier way to adjust the code to acheive the same result: Do the calculations as-you-go (no need for an array or a second loop), but just move the cout statement of the result outside the loop.
-
September 21st, 2008, 11:33 PM
#7
Re: Help!
you can also input the number into a int array. such as int num[10].
Then the program will like this.
for(int i=0;i<10;i++)
{
if(num[i]>0)
sum=sum+num[i];
}
----------------------------------------------------
codeuu,source code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|