// This program performs simple statistical analysis // of a set of test scores with range 0...100. // Created by Sebastian Gosztyla #include #include #include #include using namespace std; const int MAX_ARRAY = 400; // Function prototypes void getData(ifstream& inFile, int inValues[], int& size); void openCheck(ifstream& fileIn); double getAverage(int inValues[], int dataSize); double stdDeviation(int inValues[], int dataSize, double average); void minMax(int inValues[], int numVals, int&min, int&max); void quartile(int inValues[], int numVals, int min, int max, double average, double deviation); int main() { // Declare local variables for main ifstream fileIn; // Array for data storage int testScores[MAX_ARRAY]; // Actual number of values in array // (1 more than largest index) int numElems, min, max; double average, deviation; // Open file fileIn.open("pgm5data.txt"); // Test for file existence openCheck(fileIn); // Read file and count values in array getData(fileIn,testScores,numElems); // Test for non-empty data file if (numElems == 0) cout << "ERROR: No data processed" << endl << endl; //Mininum and Maximum minMax(testScores,numElems,min,max); //Average average = getAverage(testScores,numElems); //Standard Deviation deviation = stdDeviation(testScores,numElems,average); //Quartile quartile(testScores,numElems,min,max,average,deviation); // Close file fileIn.close(); // Terminate program return 0; } //----------------------------------------------------------- // This function reads integers from a file and stores the values in // an array. It returns the loaded array and the number of elements // in the array void getData(ifstream& inFile, int inValues[], int& numVals) { int i = 0; inFile >> inValues[i]; // Test for end of file and array while (!inFile.eof() && i < MAX_ARRAY) { i++; inFile >> inValues[i]; } numVals = i; } //----------------------------------------------------------- // This function checks to see if the file exists void openCheck(ifstream& fileIn) { if (fileIn.fail() ) { cout << "Problem opening file"; exit(-1); } } //----------------------------------------------------------- // This function receives an array of integers, calculates the average // of the array values, and returns it. double getAverage(int inValues[], int dataSize) { double sum = 0.0; for (int i = 0; i < dataSize; i++) sum = sum + inValues[i]; return sum / dataSize; } //----------------------------------------------------------- // This function reads integers from an array and determines // the minimum and maximum value in that array. void minMax(int inValues[], int numElems, int&min, int&max) { max = inValues[0]; min = inValues[0]; int i = 0; for (int i = 0; i < numElems; i++) { if (max < inValues[i]) max = inValues[i]; if (min > inValues[i]) min = inValues[i]; } } //----------------------------------------------------------- // This function receives an array of integers, calculates // the standard deviation and returns it. double stdDeviation(int inValues[], int numElems, double average) { double sum = 0.0; for (int i = 0; i < numElems; i++) sum = sum + pow((inValues[i] - average),2); return sqrt((1.0/numElems) * sum); } //----------------------------------------------------------- // This function reads in an array of test scores and // determines which quartile they fall into. void quartile(int inValues[], int numElems, int min, int max, double average, double deviation) { int quartile1=0, quartile2=0, quartile3=0, quartile4=0; for (int i = 0; i < numElems; i++) { if (inValues[i]< 25) quartile4++; else if (inValues[i] < 50) quartile3++; else if (inValues[i] < 75) quartile2++; else quartile1++; } cout << "SUMMARY" << endl << endl << setprecision(2) << fixed << "Number of Elements: " << numElems << endl << "Standard Deviation: " << deviation << endl << "Max: " << max << endl << "Min: " << min << endl << "Average: " << average << endl << "Quartile 1: " << quartile1 << endl << "Quartile 2: " << quartile2 << endl << "Quartile 3: " << quartile3 << endl << "Quartile 4: " << quartile4 << endl; }