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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.