/* Name: findLargestInArray Copyright: Author: Professor Langsa, Date: 2006-10-30-14.42 Description: Finds the largest element in an array. */ #include #include //include library for file processing using namespace std; // Function findlargest finds the largest element in an array. int findLargest(int number[], int count) { int largest, i; largest = number[0]; for (i = 1; i < count; i++) if (number[i] > largest) largest = number[i]; return largest; } // end findLargest /************************************************************************/ // Exercises the findlargest function. int main() { const int MAXNUMBER = 1000; int i, count, number[MAXNUMBER]; int findLargest(int[], int); ifstream infile; //create the file infile for input infile.open("NumbersData.txt"); //Method: open the file //Note: may be combined into one statement - // ifstream infile("MyData.txt"); if ( !infile.is_open() ) { //Method: checks to see if file was opened // The file could not be opened cout << endl << "ERROR: Unable to open file" << endl; system ("PAUSE"); exit(1); } i = 0; while (infile >> number[i]) ++i; count = i; cout << "\nThe array contains " << count << " elements." << endl; cout << "\nThe largest element is: " << findLargest(number, count) << endl;; infile.close(); system("PAUSE"); return 0; }