i'm suppose to rewrite the findMAX() so that the variable MAX is declared in main (), is used to store the maximum value of two passed numbers.The value of MAX should be set directly from within findMAX().
so can someone help me finish this?
heres my code:
#include %26lt;iostream%26gt;
using namespace std;
int findMAX(int, int); // the funtion prototype
int main()
{
int firstnum, secnum, MAX;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; firstnum;
cout %26lt;%26lt; "Great! Please enter a second number: ";
cin %26gt;%26gt; secnum;
MAX = findMAX (firstnum, secnum); // the function is called here
cout %26lt;%26lt; "\nThe maximum of the two numbers are :" %26lt;%26lt; MAX %26lt;%26lt; endl;
return 0;
}
// following is the function findMAX()
int findMAX(int x, int y)
{ //start of function body
int maxnum; //variable declaration
if (x %26gt;= y) //find the max number
maxnum = x;
else
maxnum = y;return maxnum;
cout %26lt;%26lt;"\nThe maximum of the two numbers entered are: " %26lt;%26lt; maxnum %26lt;%26lt; endl;
system( "pause" );
return 0;
}
// End of Program
Help with a c++ program.?
%26gt;:(
since i guess theres no way i can post another comment, so I guess i'll use my sisters account.
anyway so the code looks like this:
#include %26lt;iostream%26gt;
using namespace std;
int findMAX(int x, int y); // the funtion prototype
int main()
{
int firstnum, secnum, MAX;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; firstnum;
cout %26lt;%26lt; "Great! Please enter a second number: ";
cin %26gt;%26gt; secnum;
MAX = findMAX (firstnum, secnum); // the function is called here
cout %26lt;%26lt; "\nThe maximum of the two numbers are :" %26lt;%26lt; MAX %26lt;%26lt; endl;
return 0;
}
// following is the function findMAX()
int findMAX(int x, int y)
{ //start of function body
int maxnum; //variable declaration
if (x %26gt;= y) //find the max number
maxnum = x;
else
maxnum = y;
return maxnum;
cout %26lt;%26lt;"\nTe maximum of the two numbers entered are: " %26lt;%26lt; maxnum %26lt;%26lt; endl;
system( "pause" );
return 0;
}
// End of Program
so is this right?
Reply:and if you want it even shorter, drop the redundant maxnum variable and use a somewhat more standard syntax.
int findMAX(int x, int y)
{
return x %26gt;= y ? x : y;
}
Reply:Your findmax() function should be
int findMAX(int x, int y)
{ //start of function body
int maxnum; //variable declaration
if (x %26gt;= y) //find the max number
maxnum = x;
else
maxnum = y;
return maxnum;
}
// End of Program
EDIT:
and if you want to make the function shorter:
int findMAX(int x, int y)
{
x %26gt;= y ? return x :return y;
}
blazing star
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment