Answer to Question #308323 in C++ for stuti

Question #308323

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.


1
Expert's answer
2022-03-09T06:38:36-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog