Write a C++ program to sort 4 integers in nondecreasing order. The 4 integers en
ID: 646123 • Letter: W
Question
Write a C++ program to sort 4 integers in nondecreasing order. The 4 integers entered at the keyboard can be
(1) all distinct (e.g. 2 1 3 4),
(2) partially distinct (e.g. 1 4 1 2),
(3) all identical (e.g. 1 1 1 1),
(4) in descending order (e.g. 4 3 2 1), or
(5) already sorted in nondecreasing order (e.g. 1 2 3 4).
Your program should use ONLY the if-else control structure shown in class to solve the problem and no other structures (e.g. for loops, while loops, arrays, etc.) can be used. Also you cannot use the brute force approach to solve the problem by listing all the permutations. Test your program for all the five cases.
The following shows a sample output of running the program.
Enter 4 numbers: 1 3 2 4
The sorted sequence for 1 3 2 4 is 1 2 3 4.
Hi, I've gotten two answers that do not run. I don't have time to fix your answer so please send it to me complete please, please, please. I'm out of time.
Explanation / Answer
C++ Program to sort 4 integers in nondecreasing order using IF-Else control structure:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a;
int b;
int c;
int d;
int smallest;
bool firstExcluded = false;
bool secondExcluded = false;
bool thirdExcluded = false;
bool fourthExcluded = false;
cout << "Enter four integers here: "; // Enter four numbers.
cin >> a;
cin >> b;
cin >> c;
cin >> d;
smallest = a;
if (b<a)
smallest = b;
if (c<smallest)
smallest = c;
if (d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
{
firstExcluded = true;
smallest = b;
}
else if (smallest == b)
{
secondExcluded = true;
smallest = a;
}
else if (smallest == c)
{
thirdExcluded = true;
smallest = a;
}
else if (smallest == d)
{
fourthExcluded = true;
smallest = a;
}
if (!firstExcluded && a<smallest)
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
{
firstExcluded = true;
if (secondExcluded)
smallest = c;
else
smallest = b;
}
else if (smallest == b)
{
secondExcluded = true;
if (firstExcluded)
smallest = c;
else
smallest = a;
}
else if (smallest == c)
{
thirdExcluded = true;
if (firstExcluded)
smallest = b;
else
smallest = a;
}
else if (smallest == d)
{
fourthExcluded = true;
if (firstExcluded)
smallest = b;
else
smallest = a;
}
if (!firstExcluded && a<smallest)
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
firstExcluded = true;
else if (smallest == b)
secondExcluded = true;
else if (smallest == c)
thirdExcluded = true;
else if (smallest == d)
fourthExcluded = true;
if (!firstExcluded)
{
firstExcluded = true;
smallest = a;
}
if (!secondExcluded)
{
secondExcluded = true;
smallest = b;
}
if (!thirdExcluded)
{
thirdExcluded = true;
smallest = c;
}
if (!fourthExcluded)
{
fourthExcluded = true;
smallest = d;
}
cout << smallest;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.