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

This program allows the user to enter 5 integers. There are no restrictions on t

ID: 3814706 • Letter: T

Question

This program allows the user to enter 5 integers. There are no restrictions on these integers. Your program should then sort the integers from small-est to largest using an if-else chain and then display the sorted list to the monitor.

For example, if the file contains: 2 1 2 01, your program would display:1 0 1 2 2

Requirements: Do not use loops or arrays in any way. Do not use any built-in C++ function that might be used for sorting. It is impractical to

enumerate all possibilities in this case. Try to think of a sorting method which is easier to implement, using a series of if-else statements (probably

15 or fewer lines).

Explanation / Answer

#include <iostream>

using namespace std;

int min(int *a, int *b)
{
int temp;
if (*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
}
}

int main()
{
cout << "Enter 5 velements:" << endl;
int one, two, three, four, five;
cin >> one >> two >> three >> four >> five;

min(&one, &two);
min(&one, &three);
min(&one, &four);
min(&one, &five);

min(&two, &three);
min(&two, &four);
min(&two, &five);

min(&three, &four);
min(&three, &five);

min(&four, &five);
  
cout << one << " " << two << " " << three << " " << four << " " << five << endl;

return 0;
}

This has defined a function to exchange seocnd number iwth first number if first number is larger.

Sample rrun

5 4 3 2 1
1 2 3 4 5

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