Hey guys and galls. I am trying to create a compare function, but it doesn't work properly. Somehow the program is "forgetting" the value I told it previously.
#include <iostream>
#include <cmath>
usingnamespace std;
int sum1 (int);
int sum2 (int);
int sum3 (int);
int sum4 (int);
int compare (int);
int main ( )
{
int n = 5;
int sumone = sum1(n);
int sumtwo = sum2 (n);
int sumthree = sum3 (n);
int sumfour = sum4 (n);
int comp1;
int comp2;
int comp3;
cout << "The value of sum1 is " << sumone << endl;
cout << "The value of sum2 is " << sumtwo << endl;
cout << "The value of sum3 is " << sumthree << endl;
cout << "The value of sum4 is " << sumfour << endl;
if (comp1 = 0)
cout << "sum1 and sum2 are different" << endl;
if (comp1 = 1)
cout << "sum1 and sum2 are the same" << endl;
if (comp2 = 0)
cout << "sum2 and sum3 are different" << endl;
if (comp2 = 1)
cout << "sum2 and sum3 are the same" << endl;
if (comp3 = 0)
cout << "sum3 and sum4 are different" << endl;
if (comp3 = 1)
cout << "sum3 and sum4 are the same" << endl;
system("Pause");
return 0;
}
int sum1 (int n )
{
int sumone = 0;
int k = 1;
for (k = 1; k <= n; k++)
sumone += k * k * k;
return sumone;
}
int sum2 (int n)
{
int sumtwo = (n * n) * (n + 1) * (n + 1) / 4;
return sumtwo;
}
int sum3 (int n)
{
int insideparen = 0;
int m = 1;
while (m <= n){
insideparen += m;
m++; }
int sumthree = insideparen * insideparen;
return sumthree;
}
int sum4 (int n)
{
int sumfour = n * (n + 1) * (2 * n + 1) / (4 + 2);
return sumfour;
}
int compare (int sumone, int sumtwo, int sumthree, int sumfour)
{
int comp1;
int comp2;
int comp3;
if (sumone != sumtwo)
return comp1 = 0;
elsereturn comp1 = 1;
if (sumtwo != sumthree)
return comp2 = 0;
elsereturn comp2 = 1;
if (sumthree != sumfour)
return comp3 = 0;
elsereturn comp3 = 1;
}
The value of sum1 is 225
The value of sum2 is 225
The value of sum3 is 225
The value of sum4 is 55
sum1 and sum2 are the same
sum2 and sum3 are the same
sum3 and sum4 are the same
Press any key to continue . . .
You're trying to return too many results. Return can only handle 1. so any int function can return 1 variable. if you want to 'return' multiple you need to call that variable in the parameters using the call-by-reference method. [done by type&, where type is int short, double etc.]
here is a debugged version of your compare function:
also in your displaying if statements you must use the equal to operator not the assignment operator [==; not =] This should clear up your troubles. I ran it on my system with the necessary changes and it worked just fine. Hope this answers your questions.
--Edit--
Oh! I almost forgot! I declared all the variables as global for the trial, make sure you declare your comp1 comp2 comp3 inside main!
if (comp1 = 0)
cout << "sum1 and sum2 are different" << endl;
if (comp1 = 1)
cout << "sum1 and sum2 are the same" << endl;
if (comp2 = 0)
cout << "sum2 and sum3 are different" << endl;
if (comp2 = 1)
cout << "sum2 and sum3 are the same" << endl;
if (comp3 = 0)
cout << "sum3 and sum4 are different" << endl;
if (comp3 = 1)
cout << "sum3 and sum4 are the same" << endl;
to
if (comp1 == 0)
cout << "sum1 and sum2 are different" << endl;
else
cout << "sum1 and sum2 are the same" << endl;
if (comp2 == 0)
cout << "sum2 and sum3 are different" << endl;
else
cout << "sum2 and sum3 are the same" << endl;
if (comp3 == 0)
cout << "sum3 and sum4 are different" << endl;
else
cout << "sum3 and sum4 are the same" << endl;
for the output to display properly. Guess that's a lesson for me. Better to use if/else then all ifs...
I wonder why I got this with the original couts...
The value of sum1 is 225
The value of sum2 is 225
The value of sum3 is 225
The value of sum4 is 55
sum1 and sum2 are the same
sum2 and sum3 are the same
sum3 and sum4 are different
Press any key to continue . . .
I'm just musing at this point. My question has been answered :)!!!
Hmm... thought I mentioned that at the end of my post... *re-reads* Yeah I did, I'll make sure things like that end up first next time :P
OH! and just a heads up, don't mix the parameter call methods. I.e. is you want one to be int& they all must be type& in order for the function to work properly, even if you don't change the value in some of the parameters :~