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

Write a program that reads in three integers, then uses the function sort3( int&

ID: 675457 • Letter: W

Question

Write a program that reads in three integers, then uses the function sort3( int& a, int& b, int& c) that swaps its three arguments to arrange them in ascending sorted order. For example,

int v = 3;

int w = 4;

int x = 1;

sort3(v, w, x); // v is now 1, w is now 3, x is now 4

Hint: Use the swap function shown in the ShuffleSwap.cpp program.

Provide a program that tests your function via a loop construct for acquiring testing data.

Example run (with user input indicated with bold italics):

Enter three numbers or Q to quit: 2 1 3

Sorted: 1 2 3

Enter three numbers or Q to quit: 4 5 4

Sorted: 4 4 5

Enter three numbers or Q to quit: 3 2 -1

Sorted: -1 2 3

Enter three numbers or Q to quit: 0 15 10

Sorted: 0 10 15

Enter three numbers or Q to quit: Q

Explanation / Answer

#include<iostream>
#include<string.h>
using namespace std;

void sort3( int& a, int& b, int& c) {
    int min,max,mid;
  
    if(a < b) {
        if(a < c)
        {
            min = a;
        } else {
            min = c;
        }
    } else {
        if(b < c)
            min = b;
        else
            min = c;
    }
  
    if(a > b) {
        if(a > c)
        {
            max = a;
        } else {
            max = c;
        }
    } else {
        if(b > c)
            max = b;
        else
            max = c;
    }
  
    if (min == a) {
        if (max == b) {
            mid = c;
        }
        else
            mid = b;
    } else if (min == b) {
        if(max == a)
            mid = c;
        else
            mid = a;
    } else {
        if(max == a)
            mid = b;
        else
            mid = a;
    }
  
    a=min;
    b=mid;
    c=max;
}

int main() {

    int v, w, x;
  
    string in;
    while (true) {
      
        cout<<"Enter three numbers or Q to quit: ";
        cin>>in;
        if(in == "Q") {
            return 0;
        }
      
        v = stoi(in);
      
        cin>>w>>x;
        sort3(v, w, x);
      
        cout<<"Sorted: "<<v<<" "<<w<<" "<<x<<" ";
    }
  
    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