This is the code I have so far. I haven't been successful when try to finish/fix it. I have included what I have so far. Can anyone help me out?
/*************************************************************
* Description: Reads in 10 integers and prints out *
* - the sum of all the positive numbers *
* - the sum of all the negative numbers *
* - the sum of ALL numbers *
*************************************************************/
#include <iostream>
using namespace std;
int main()
{
int MyNumber, AllSum, GreaterSum, LessSum, i;
cout << "This program produces the sum of 10 numbers as entered by the user "
<< endl << "and also reports the sum of the positive numbers "
<< endl << "and the sum of the negative numbers entered. " << endl;
// Initialize the variables
AllSum = 0;
GreaterSum = 0;
LessSum = 0;
i = 1;
// Begin the loop
do
{
// Get input from user
cout << "Enter a number ";
cin >> MyNumber;
// update each summation as needed
Update AllSum here
Update GreaterSum
As Needed
Update LessSum
As Needed
i++; //update the loop control variable
} while (i != XX);
cout << "Sum of the numbers greater than zero = " << GreaterSum << endl;
cout << "Sum of the numbers less than zero = " << LessSum << endl;
cout << "Total sum = " << AllSum << endl;
int main()
{
int MyNumber, AllSum, GreaterSum, LessSum, i;
cout << "This program produces the sum of 10 numbers as entered by the user "
<< endl << "and also reports the sum of the positive numbers "
<< endl << "and the sum of the negative numbers entered. " << endl;
// Initialize the variables
AllSum = 0;
GreaterSum = 0;
LessSum = 0;
i = 0;
// Begin the loop
do
{
// Get input from user
cout << "Enter a number ";
cin >> MyNumber;
// update each summation as needed
//Update AllSum here
//Update GreaterSum
//As Needed
//Update LessSum
//As Needed
i++; //update the loop control variable
} while (i < 10);
cout << "Sum of the numbers greater than zero = " << GreaterSum << endl;
cout << "Sum of the numbers less than zero = " << LessSum << endl;
cout << "Total sum = " << AllSum << endl;
return 0;
}
Second: This really looks like a homework assignment.
To answer the question asked: Do what the comments on line 23-29 tell you to do. You need to update those 3 variables. You will always add the user value to AllSum, you will only add to the GreaterSum if it's greater then 0. Use an if statement.
All you needed to do was do some Control Statements and update your variables. your program would compile but all your answers were 0. this is because of your variables werent being used. i put the secions of code that i did in /* comments */ just take away the /* and */ symbols and remove my explanation of the code and your program should work as intended. learn from it and love what you do and you will soon be able to tackle any problem.
#include <iostream>
usingnamespace std;
int main()
{
int MyNumber, AllSum, GreaterSum, LessSum, i;
cout << "This program produces the sum of 10 numbers as entered by the user "
<< endl << "and also reports the sum of the positive numbers "
<< endl << "and the sum of the negative numbers entered. " << endl;
// Initialize the variables
AllSum = 0;
GreaterSum = 0;
LessSum = 0;
i = 0;
// Begin the loop
do
{
// Get input from user
cout << "Enter a number ";
cin >> MyNumber;
// update each summation as needed
//Update AllSum here
/* this statement basically say if the number is less than 0 then it is negative and will go into the LessSum variable
and if not then it is positive and goes into the GreaterSum variable. in case you are not familiar with this symbol +=,
all it does is combines the + and = operators into one nice compact operator.
if (MyNumber >= 0)
GreaterSum += MyNumber;
else
LessSum += MyNumber;
//Update GreaterSum
//As Needed
//Update LessSum
//As Needed
i++; //update the loop control variable
/* sets AllSum to the solution of GreaterSum + LessSum
AllSum = GreaterSum + LessSum;*/
} while (i < 10);
cout << "Sum of the numbers greater than zero = " << GreaterSum << endl;
cout << "Sum of the numbers less than zero = " << LessSum << endl;
cout << "Total sum = " << AllSum << endl;
system("Pause");
return 0;
}