//ic210 section 4002 //19 oct 2009 //practice arrays: read from file, //compute min, max, std avg // the input file looks like this: // N = 3 // DATA: // 67 // 61 // 82 #include #include #include using namespace std; //prototypes int* readArrayFromFile (string, int&); void calcMinAndMax (int*, int, int&, int&); double calcAverage (int*, int); double calcStdDev (int*, int, double); int main() { // Read the array int N; int* myArray = readArrayFromFile("numbers.txt", N); cout << "Got array of size: " << N << endl; // Get the min and max int min, max; calcMinAndMax(myArray, N, min, max); cout << "Min is " << min << endl; cout << "Max is " << max << endl; // Compute the average /* Comment this so we can run the program when the functions are not defined double average; average = calcAverage(myArray, N); cout << "Average is " << average << endl; // Compute standard deviation cout << "Std. deviation is " << calcStdDev(myArray, N, average) << endl; */ return 0; } int* readArrayFromFile (string fname, int& nval) { string junk; ifstream fin; fin.open(fname.c_str()); if(!fin) { cout << "You have ERROR in opening file:" << fname; exit(1); } fin >> junk >> junk >> nval; fin >> junk; int* myArray = new int[nval]; for(int i=0;i < nval; i++) { fin >> myArray[i]; } return myArray; } void calcMinAndMax (int* array, int N, int& min, int& max) { //initialize min and max min = array[0]; max = array[0]; //compute min and max for (int j = 0; j < N; j++) { if (min > array[j]) { min = array[j]; } if (max < array[j]) { max = array[j]; } } return; }