Find Maximum

Program Listing

• This program will find the largest value in an array.
• A very simple modification of the FindMax() function will change it into the FindMin() function.

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 #include 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 ```