------------------------------------------------------------------------------
MC logo
Integer Library Sort
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Array Echo] [Basic Pointers] [Swap Using Pointers] [Arrays and Pointers] [Array Passing] [Integer Select Sort] [Integer Quick Sort] [Integer Library Sort] [Pointers and References]
libsort.cc
#include <iostream>
#include <algorithm>

using namespace std;

/* 
 * This program reads in integers and sorts them using the library sort 
 * function.  The algorithm header provides the sorting function.
 *
 * Author: Tom Bennet
 */

const int MAX_NUM_INTS = 100;
int main()
{
        int ints[MAX_NUM_INTS];         // Where the numbers go.
                
        // Read them in.  We don't declare i in the loop so we can
        // use its value afterwards.
        int i;
        for(i = 0; i < MAX_NUM_INTS && cin >> ints[i]; ++i);
        int numints = i;

        // Sort them.  The library sort algorithm wants a pointer to the
        // the start of the data, and a pointer one past the end.
        sort(ints, ints + numints);

        // Print them.
        cout << "==================" << endl;
        for(i = 0; i < numints; ++i)
                cout << ints[i] << endl;
        cout << "==================" << endl;
}