Hi, I have to write code for a program that finds the maximum and minimum out of the three integers entered my the user. All three numbers need to be different but if two or more are the same I need to execute the loop again. This is my 1st problem it doesn't execute the loop again if I enter similar numbers. My 2nd problem is that the program outputs the max value for both minimum and maximum. Please help. Thanks a lot!
Code:
#include <iostream>using namespace std;
bool is_different(int a, int b, int c);
int max_of_three(int a, int b, int c);
int greater_of_two(int a, int b, int c);
int min_of_three(int a, int b, int c);
int smaller_of_two(int a, int b);
void print_result(int max, int min);
int main()
{
int a;
int b;
int c;
int max;
int min;
do
{
cout << "Enter the first integer: "<< endl;
cin >> a;
cout << "Enter the second integer: " << endl;
cin >> b;
cout << "Enter the third integer: "<< endl;
cin >> c;
max = max_of_three (a, b, c);
min = min_of_three (a, b, c);
print_result (max,min);
}
while (!(is_different ( a, b, c)));
}
bool is_different (int a, int b, int c)
{
if (a == b || b ==c || b ==c)
{
return false;
}
else
{
return true;
}
}
int greater_of_two (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
int max_of_three (int a, int b, int c)
{
int max1 = greater_of_two (a, b);
int max2 = greater_of_two (max1, c);
return max2;
}
int smaller_of_two (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
int min_of_three (int a, int b, int c)
{
int min1 = smaller_of_two (a, b);
int min2 = smaller_of_two (min1, c);
return min2;
}
void print_result (int max , int min)
{
cout << "The maximum integer is " << max << endl;
cout << "The minimum integer is " << min << endl;
}
This is the output for two similar numbers:
Enter the first integer:
5
Enter the second integer:
6
Enter the third integer:
5
The maximum integer is 6
The minimum integer is 6
This is the output for 3 different numbers:
Enter the first integer:
1
Enter the second integer:
2
Enter the third integer:
3
The maximum integer is 3
The minimum integer is 3