Sort Three


Program Listing

SortThree.cpp

/****************************************************************************
*                                                                           *
*   Program:    SortThree                                                   *
*                                                                           *
*   Purpose:    This program will put three numbers in ascending order      *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Get three integers

    cout << "Enter three integers:  " << flush;
    int a;
    int b;
    int c;
    cin >> a >> b >> c;

//  Swap a and b, if necessary

    if (a > b) {
        int Temp = a;
        a = b;
        b = Temp;
    }
    
//  Swap b and c, if necessary

    if (b > c) {
        int Temp = b;
        b = c;
        c = Temp;
    }
    
//  Swap a and b, if necessary

    if (a > b) {
        int Temp = a;
        a = b;
        b = Temp;
    }
    
//  Print the values in ascending order

    cout << "The integers are " << a << ", " << b << ", and " << c << endl;
    
    return 0;
}

Sample Run

Enter three integers:  5 8 3
The integers are 3, 5, and 8



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:31 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/SortThree.html

Return to Coms 261 home page