/* Filename: max.cpp Author: Br. David Carlson Date: January 23, 1998 Modified: June 25, 2000; November 21, 2001 This program prompts the user to enter integers into an array and then prints them on the screen, along with the maximum data item. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux Note: Under Linux, you use CTRL d to end data entry, not CTRL z. */ #include using namespace std; const int ArrayMax = 8; typedef int IntArrayType[ArrayMax]; /* Given: Nothing. Task: To read integers from the keyboard and store them in A, as long as they will fit. Prints a message saying that all fit, that they did not fit, or that there was an error during input. Also counts the number of integers placed into A. Return: A The array with integers placed into it. Count The number of integers placed into A. */ void LoadArray(IntArrayType A, int & Count) { int Temp; Count = 0; // Change to CTRL d for Linux: cout << "Enter your first int (CTRL z to end): "; cin >> Temp; while ((! cin.fail()) && (Count < ArrayMax)) { A[Count] = Temp; Count++; // Change to CTRL d for Linux: cout << "Enter another number (CTRL z to end): "; cin >> Temp; } if (cin.fail()) cout << "All data has been read" << endl; else cout << "Warning: not all of the data would fit in the array" << endl; } /* Given: A An array of integers from index 0 to Count - 1. Count The number of integers in A. Task: To print the contents of A. Return: Nothing. */ void PrintArray(IntArrayType A, int Count) { int k; cout << endl << "The array contains:" << endl; for (k = 0; k < Count; k++) cout << A[k] << " "; cout << endl; } /* Given: A Array of integers, with data from index 0 to Count - 1. Count The number of integers in A. Task: To find the maximum data item in A. Return: This maximum in the function name. */ int FindMax(IntArrayType A, int Count) { int Max, k; Max = A[0]; // assume the maximum is the first item until know better for (k = 1; k < Count; k++) if (A[k] > Max) Max = A[k]; return Max; } int main(void) { IntArrayType DataArray; int Count; LoadArray(DataArray, Count); PrintArray(DataArray, Count); cout << endl << "The maximum data item is " << FindMax(DataArray, Count) << endl; return 0; }