Find Maximum


Program Listing

FindMax.cpp

/************************************************************************
*                                                                       *
*   Program:    FindMax                                                 *
*                                                                       *
*   Purpose:    This program will read a list of numbers and then       *
*               find the largest number in the list                     *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

int FindMax(int array[], int size);
void ReadList(int array[], int& size, int maxSize);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To read a list of numbers and then find the largest     *
*               number in the list                                      *
*                                                                       *
************************************************************************/

int main() {

//  Establish the array

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

//  Read numbers into the array

    ReadList(array, size, maxSize);

//  Find the largest number in the array

    int max = FindMax(array, size);

//  Report the results

    cout << endl << "The largest member is " << max << endl;
    
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   FindMax                                                 *
*                                                                       *
*   Purpose:    To find the largest number in an array of numbers       *
*                                                                       *
************************************************************************/

int FindMax(int array[], int size) {

//  Set the first number as the maximum so far

    int max = array[0];

//  As larger numbers are found, make them the maximum so far

    for (int i = 1; i < size; i++)
        if (array[i] > max)
            max = array[i];
    return max;
}

/************************************************************************
*                                                                       *
*   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

Enter a list of integers (CTRL-D to quit).
30 50 80 25 55 40 15 75
The largest member is 80



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/FindMax.html

Return to Coms 261 home page