Can someone explain why I am getting an extra 2 added to my sum PLEASE!!!
1 2 3 4 5 6
int LoopEvenSum (int num, int &finalLoopSum)
{
//will compute and return the same sum using iteration (i.e. a loop).
for (int i = 0; i <= num; i += 2)
finalLoopSum += i;
}
For a better understanding, the program should be calculating the sum of all EVEN integers from 1 to num (inclusive). Therefore, if num = 5, finalLoopSum should = 6 because (2+4 = 6). If num = 10, finalLoopSum should = 30 because (2+4+6+8+10 = 30). However, I am getting 8 instead of 6, and 32 instead of 30, thus the extra 2. All help is greatly appreciated, as always!
Maybe you forgot to set an initial velue for finalLoopSum.
1 2 3 4 5 6 7
int LoopEvenSum (int num, int &finalLoopSum)
{
//will compute and return the same sum using iteration (i.e. a loop).
finalLoopSum = 0;
for (int i = 0; i <= num; i += 2)
finalLoopSum += i;
}
Also you return nothing from the function. I would declare it as
1 2 3 4 5 6 7 8 9
int LoopEvenSum( int num )
{
//will compute and return the same sum using iteration (i.e. a loop).
int sum = 0;
for (int i = 0; i <= num; i += 2) sum += i;
return ( sum );
}
Yeah, I had that, I just didn't show ALL of the code, there is much more to the program than that little piece and didn't want to include a ton of info when I knew the issue was somewhere in that small piece :)
It makes a little more sense now, I'm still reading up on it...hopefully i get a decent understanding of it. This is my entire program so far, only 2 issues I have left
number 1 - program should loop until user exits with the number 0, however, in doing so...say for the first num i enter 10..and my answers should all be 30...then looping through i enter 5 for the num and my answers should all be 6, tho my answer for the iterative loop is 36. It is not emptying the bank, rather it is holding its previous value. I thought by setting it to 0, would correct that. Any thoughts?
number 2 - the recursive function, is way off in value. Where my answer should be 30, it is 2293570 (i am working on debugging that right now, but of course thoughts and suggestions are appreciated)
#include <iostream>
#include <iomanip>
usingnamespace std;
// function prototypes
int FormulaEvenSum (int num, int &finalFormulaSum);
int LoopEvenSum (int num, int &finalLoopSum);
int RecursiveEvenSum (int num, int &finalRecursiveSum);
int main()
{
int num;
int finalFormulaSum = 0;
int finalLoopSum = 0;
int finalRecursiveSum = 0;
cout << "Program to compute sum of even integers from 1 to num" << endl << endl;
// Read a positive integer from the user (indicating that if the user enters 0, the program will exit)
// Error check the number entered, and loop until the user enters a positive integer
do
{
cout << "Enter a positive integer (or 0 to exit): ";
cin >> num;
cout << endl;
// The program should loop and continue to compute results for new values of num, until the user enters 0 for the value of num.
if (num != 0)
{
FormulaEvenSum (num, finalFormulaSum); // funciton call
LoopEvenSum (num, finalLoopSum); // function call
RecursiveEvenSum (num, finalRecursiveSum); // function call
cout << "Formula result = " << finalFormulaSum << endl;
cout << "Iterative result = " << finalLoopSum << endl;
cout << "Recursive result = " << finalRecursiveSum << endl << endl;
}
// invalid entry
if (num < 0)
{
cout << "Invalid Entry! Please try again!" << endl;
}
}
while (num != 0);
if (num == 0)
{
// exit the program
cout << "Goodbye!" << endl << endl;
}
system("PAUSE");
}
int FormulaEvenSum (int num, int &finalFormulaSum)
{
// formula to compute the sume of the even integers
finalFormulaSum = ((num/2) * ((num/2) + 1));
}
int LoopEvenSum (int num, int &finalLoopSum)
{
// will compute and return the same sum using iteration
for (int i = 0; i <= num; i += 2)
finalLoopSum += i;
}
int RecursiveEvenSum (int num, int &finalRecursiveSum)
{
// will compute and return the same sum using recursion.
if (num < 2)
{
finalRecursiveSum = 0;
}
else
{
finalRecursiveSum = 2 + RecursiveEvenSum (num - 2, finalRecursiveSum);
}
}
void RecursiveEvenSum (int num, int &finalRecursiveSum);
and the definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void RecursiveEvenSum (int num, int &finalRecursiveSum)
{
// will compute and return the same sum using recursion.
if (num < 2)
{
finalRecursiveSum = 0;
}
else
{
RecursiveEvenSum (num - 2, finalRecursiveSum);
finalRecursiveSum += 2 * ( num / 2 ) ;
}
}
int LoopEvenSum ( int num )
{
// will compute and return the same sum using iteration
sum = 0;
for (int i = 0; i <= num; i += 2) sum += i;
return ( sum );
}
As for LoopEvenSum function, If I remove the output parameter and declare sum locally, then I cannot use that sum in main...and I am not allowed to use global variables.
void LoopEvenSum ( int num, int &finalLoopSum )
{
// will compute and return the same sum using iteration
finalLoopSum = 0;
for (int i = 0; i <= num; i += 2) finalLoopSum += i;
}
I found the issue. I can't actually declare a variable that i am also using as an input/output parameter. I was passing it as an output parameter, and also declaring within that function that int finalLoopSum = 0, rather than disregarding "int". Now that i removed the "int", program works fine.
Aside from the recursive function of course. That one is still a little wacky.