Verify Order


Program Listing

VerifyOrder.cpp

/************************************************************************
*                                                                       *
*   Program:    VerifyOrder                                             *
*                                                                       *
*   Purpose:    This program will read a list of numbers and then       *
*               verify that the numbers are in ascending order          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

bool VerifyOrder(int array[], int size, bool ascending = true);
void ReadList(int array[], int& size, int maxSize);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To read a list of numbers and then verify that the      *
*               numbers are in ascending order                          *
*                                                                       *
************************************************************************/

int main() {

//  Establish the array

    const int maxSize = 100;
    int array[maxSize];
    int size;

//  Read numbers into the array

    ReadList(array, size, maxSize);

//  Verify that the numbers are in order

    bool InOrder = VerifyOrder(array, size);

//  Report the results

    if (InOrder)
        cout << endl << "The list is in ascending order" << endl;
    else
        cout << endl << "The list is not in ascending order" << endl;
    
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   VerifyOrder                                             *
*                                                                       *
*   Purpose:    To verify that the numbers in the array are in          *
*               ascending or descending order                           *
*                                                                       *
************************************************************************/

bool VerifyOrder(int array[], int size, bool ascending) {

    if (ascending) {
        for (int i = 0; i < size - 1; i++)
            if (array[i] > array[i + 1])
                return false;
    }
    else {
        for (int i = 0; i < size - 1; i++)
            if (array[i] < array[i + 1])
                return false;
    }
    return true;
}

/************************************************************************
*                                                                       *
*   Function:   ReadList                                                *
*                                                                       *
*   Purpose:    To read a list of numbers                               *
*                                                                       *
*   Note:       The input is controlled by end-of-file                  *
*                                                                       *
************************************************************************/

void ReadList(int array[], int& size, int maxSize) {
    cout << "Enter a list of integers (CTRL-D to quit)." << endl;
    int i = 0;

//  Read numbers until end-of-file is detected

    while (i < maxSize && cin >> array[i])
        i++;

//  Store the size of the list

    size = i;
    return;
}

Sample Run #1

Enter a list of integers (CTRL-D to quit).
30 50 55 60 75 80 90
The list is in ascending order

Sample Run #2

Enter a list of integers (CTRL-D to quit).
30 50 60 55 75 80 90
The list is not in ascending order



Return to Lectures page


e-mail me at robbk@hsc.edu

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

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

Return to Coms 261 home page