This is the problem and the program that will not run is below: Write a C++ prog
ID: 646131 • Letter: T
Question
This is the problem and the program that will not run is below:
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.
What is wrong with this program? it will not run after 4 numbers are entered
#include <iostream>
using namespace std;
int main(){
int a, b, c, d;
cout << "Enter 4 numbers: ";
cin >> a >> b >> c >> d;
int a1, a2, a3, a4;
if(a > b && a > c && a > d){
if(b > c && b > d){
if(c > d){
a4 = d;
a3 = c;
a2 = b;
a1 = a;
}
else{
a4 = c;
a3 = d;
a2 = b;
a1 = a;
}
}
else if(c > b && c > d){
if(b > d){
a4 = d;
a3 = b;
a2 = c;
a1 = a;
}
else{
a4 = b;
a3 = d;
a2 = c;
a1 = a;
}
}
else{
if(b > c){
a4 = c;
a3 = b;
a2 = d;
a1 = a;
}
else{
a4 = b;
a3 = c;
a2 = d;
a1 = a;
}
}
}
else if(b > a && b > c && b > d){
if(a > c && a > d){
if(c > d){
a4 = d;
a3 = c;
a2 = a;
a1 = b;
}
else{
a4 = c;
a3 = d;
a2 = a;
a1 = b;
}
}
else if(c > a && c > d){
if(a > d){
a4 = d;
a3 = a;
a2 = c;
a1 = b;
}
else{
a4 = a;
a3 = d;
a2 = c;
a1 = b;
}
}
else{
if(a > c){
a4 = c;
a3 = a;
a2 = d;
a1 = b;
}
else{
a4 = a;
a3 = c;
a2 = d;
a1 = b;
}
}
}
else{
if(b > c && b > a){
if(c > a){
a4 = a;
a3 = c;
a2 = b;
a1 = d;
}
else{
a4 = c;
a3 = a;
a2 = b;
a1 = d;
}
}
else if(c > b && c > a){
if(b > a){
a4 = a;
a3 = b;
a2 = c;
a1 = d;
}
else{
a4 = b;
a3 = a;
a2 = c;
a1 = d;
}
}
else{
if(b > c){
a4 = c;
a3 = b;
a2 = a;
a1 = d;
}
else{
a4 = b;
a3 = c;
a2 = a;
a1 = d;
}
}
}
cout << "The sorted sequence for " << a << " " << b << " " << c << " " << d << " is ";
cout << a4 << " " << a3 << " " << a2 << " " << a1 << endl;
return 0;
}
After entering 4 numbers and hitting enter, nothing happens...why?
Explanation / Answer
Output 1:
enter four integers
4 3 2 1
1
2
3
4
output 2:1 4 1 2
1
1
2
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.