#include #include // Burton Rosenberg // September 4, 1997 // MaxInteger.C // ================================================= /* readFile: Input: an array of integers, the size of the array and a file name Returns: number of integers read into the array, or -1 if error */ int readFile ( int * destinationArray, int arraySize, char * filename ) { ifstream inFile ; int i ; // Read from the file inFile.open( filename ) ; if ( !inFile ) { cout << "Could not open " << filename << endl ; return -1 ; } i = 0 ; while ( i>destinationArray[i] ) { i++ ; } inFile.close() ; return i ; } // ======================================================= /* findMaxByIndex: Input: array of integers, length of array Output: index i of maximum integer in the array. */ int findMaxByIndex( int * a, int n ) { if ( n<1 ) return -1 ; // ASSERT: array is not empty. int biggest = 0 ; int top = 1 ; // LOOP INVARIANT: for i=0, ..., top-1, // a[biggest] is the max integer. while ( top a[biggest] ) { biggest = top ; } top ++ ; // ASSERT: Loop Invariant } // POSTCONDITION: a[biggest] largest in entire array. return biggest ; } // =========================================================== int main( int argc, char * argv) { const int ARRAY_SIZE = 1000 ; int a[ARRAY_SIZE] ; int aSize = readFile(a, ARRAY_SIZE, "myFile.txt") ; if ( aSize < 0 ) return 0 ; int maxIntAtIndex = findMaxByIndex( a, aSize ) ; cout << "Maximum integer is " << a[maxIntAtIndex] << "." << endl ; return 0 ; }