I can't figure out why the findsum() is not returning the correct value.
This is the assignment question if it helps:
The main program will call a function named findsum(), sending it the three integer values. The function will determine the sum of the larger 2 of the three values, sending the answer back to the main program.
#include <iostream>
usingnamespace std;
void introduction();
int findsum (int,int,int);
int main()
{
int x=10, y=20, z=30, printsum;
introduction();
cout<<endl;
printsum=findsum(x,y,z);
cout<<printsum;
}
void introduction()
{
cout<<"test";
}
int findsum(int,int,int)
{
int a, b, c, sum;
if ( a < b )
{
int temp = a; a = b; b = temp;
}
if ( b < c )
{
int temp = b; b = c; c = temp;
}
sum = a + b;
return sum;
}
It's okay to declare a function's parameters using only their types, but when defining the function you should specify those in order to use them.
What you are doing now it's just declaring some random variables on the stack inside the function's stackframe, you aren't using the passed value inside the function.
int findsum(int a,int b,int c)
{
int sum=0;
if ( a < b )
{
int temp = a; a = b; b = temp;
}
if ( b < c )
{
int temp = b; b = c; c = temp;
}
sum = a + b;
return sum;
}