Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program containing a function that sorts three numbers of int type in no

ID: 3568639 • Letter: W

Question

Write a program containing a function that sorts three numbers of int type in nondecreasing
order using the following function heading.
void sort(int& num1, int& num2, int& num3)
The main() function prompts the user to enter three numbers (may not be different). It then
calls the function by passing the three numbers to it. After the control is returned by the function,
the main() function then displays the three numbers in sorted order. The following shows a
sample output of the program.
Enter three numbers: -3 12 5
The three numbers sorted in nondecreasing order: -3 5 12

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

void sort(int &x,int &y,int &z){
   int large,mid,small;
   large = max(x,max(y,z));
  
   if (large == x){
       if (y > z){
           mid = y;
           small = z;
       }
       else{
           mid = z;
           small = y;
       }
   }
   else if (large == y){
       if (x > z){
           mid = x;
           small = z;
       }
       else{
           mid = z;
           small = x;
       }
   }
   else{
       if (y > x){
           mid = y;
           small = x;
       }
       else{
           mid = x;
           small = y;
       }
   }
   cout << small << " " << mid << " " << large << endl;
}

int main() {
   cout << "Enter Three Numbers " << endl;
   int x,y,z;
   cin >> x >> y >> z;
   sort(x,y,z);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote