Make use of call by reference in C++, to find small, medium and large among three given integers and print true if the difference between small and medium and the difference between medium and large is same.
#include <iostream>
using namespace std;
void sort(int a, int b, int c,
int& small, int& medium, int& large)
{
if (a < b && a < c) {
small = a;
if (b < c) {
medium = b;
large = c;
}
else {
medium = c;
large = b;
}
}
else if (b < a && b < c) {
small = b;
if (a < c) {
medium = a;
large = c;
}
else {
medium = c;
large = a;
}
}
else {
small = c;
if (a < b) {
medium = a;
large = b;
}
else {
medium = b;
large = a;
}
}
}
int main() {
int a, b, c;
cout << "Enter three integers: ";
cin >> a >> b >> c;
int s, m, l;
sort(a, b, c, s, m, l);
if (l-m == m-s) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
return 0;
}
Comments
Leave a comment