1 Find the errors and correct them There are at least 5 erro
1. Find the errors and correct them. There are at least 5 errors. //This program finds the largest and smallest integer //entered by a user #include using namespace std; int main(){ bool firsttime = true; int val, min, max; char userVal; const char Y; Y = \'Y\'; do;{ cout << \"Enter a number:\"; cin >> val; if(firsttime){ max = min = val; firsttime = false; } else(!firsttime){ if(max < val)max = val; if(min > val){min = val;} } cout << \"Enter another?\"; cin >> userVal >> endl; }while(Y = userVal); cout << \"Max is:\"<< max << \" Min is:\" << min << endl; }
Solution
Here is the program debugged with comments. Find the output as well after that.
#include <iostream> //Error 1: No header file was mentioned//
using namespace std;
int main()
{
bool firsttime = true;
int val, min, max;
char userVal;
char const Y = \'Y\'; // Error 2: char constants must be initialized as and when they are declared //
//Y = \'Y\'; // Hence this line is not required.//
do // Error3: There was a semicolon here. It is not rquired for \'do\' as per syntax //
{
cout << \"Enter a number:\";
cin >> val;
if(firsttime)
{
max = min = val;
firsttime = false;
}
else if(!firsttime) //Error 4: In case of expression evaluation , it must be within if //
{
if(max < val)
max = val;
if(min > val)
{
min = val;
}
}
cout << \"Enter another?\";
cin >> userVal; //Error 5: endl not required while reading a value from console //
}while(Y == userVal); //Error 6: The operator = was used. In while condition, equality is checked . The operator is == //
cout << \"Max is:\"<< max << \" Min is:\" << min << endl;
}
OUTPUT:
Enter a number: 12
Enter another? Y
Enter a number: 12
Enter another? Y
Enter a number:23
Enter another? N
Max is:23 Min is:12
