98.1k views
5 votes
The following program results in a compiler error. Why? int FindMin(int a, int b) { int min; if (a < b) { min = a; } else { min = b; } return min; } int main() { int x; int y; cin >> x; cin >> y; min = FindMin(x,y); cout << "The smaller number is: " << min << endl; return 0; }

1 Answer

6 votes

Answer:

The answer is "Variable min is defined in the "FindMin()" method, but is used in the main method".

Step-by-step explanation:

Following are the correct code to the given question:

#include <iostream>//header file

using namespace std;

int FindMin(int a, int b) //defining a method FindMin that takes two integer parameters

{

int min; //defining integer variable

if (a < b) //use if to check a value less than b

{

min = a; //holding smallest value in min

}

else //defining else block

{

min = b;//holding smallest value in min

}

return min; //return min value

}

int main() //defining main method

{

int x,y,min; //defining integer variable

cin >> x; //input value

cin >> y; //input value

min = FindMin(x,y); //holding method return value in min variable

cout<<"The smaller number is: "<<min<< endl; //print method value with message

return 0;

}

Output:

Please find the attached file.

In the code, a method "FindMin" is defined that takes two integer parameters "a,b" and inside the method, an integer variable "min" is declared that uses a conditional statement that checks the smallest value that store in the min variable and return its value.

In the main method, three integer variable is declared in which two is used for hold value from user-end pass into the method and store its return value in min variable and print value with the message.

The following program results in a compiler error. Why? int FindMin(int a, int b) { int-example-1
User Isobar
by
4.1k points