Answer: The c++ program to implement test() method is given below.
#include <iostream>
using namespace std;
int test(int a, int b, int c);
int main() {
int x, y, z;
cout<<"This program displays the largest number."<<endl;
cout<<"Enter first number"<<endl;
cin>>x;
cout<<"Enter second number"<<endl;
cin>>y;
cout<<"Enter third number"<<endl;
cin>>z;
int max = test(x, y, z);
cout<<"The largest number is "<<max<<endl;
return 0;
}
int test(int a, int b, int c)
{
if((a>b)&&(a>c))
return a;
else if((b>a)&&(b>c))
return b;
else if((c>b)&&(c>a))
return c;
}
OUTPUT
This program displays the largest number.
Enter first number
12
Enter second number
0
Enter third number
-87
The largest number is 12
Explanation:
The method test() is declared as follows.
<em>int test(int a, int b, int c);</em>
The method is declared with return type int since it returns an integer, i.e., the largest of the three parameters.
This program is designed to accept 0 and all values of integers. Hence, input is not tested for validity. It is assumed that the user enters only numerical values.
The program declares and uses three integer variables to store the user input.
<em>int x, y, z;</em>
The method test() is designed to accept three integer parameters. This method is called once the user enters three numbers for comparison.
<em>int test(x, y, z);</em>
These numbers are compared in order to return the largest number.
No variable is used for comparison. The method simply compares the numbers using multiple if-else statements.
<em>if((a>b)&&(a>c))</em>
<em> return a;</em>
The same comparison logic is applied for other two input values.
The largest number is returned to the calling function which is main() in this program. This value is stored in the integer variable, max, declared in the main() method. The variable max is declared and initialized simultaneously.
<em>int max = test(x, y, z);</em>
This value is then displayed on the screen.
<em>cout<<"The largest number is "<<max<<endl;</em>