1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
//Written by Morgan Cassiday 3/17/2015
// This program preforms a simple statistical analysis
// of a set of test scores with range 0 ... 100
// It reads from a text data file and writes a summary repot
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
double calcAvg(int score[], int numElems);
void minMax(int score[], int numElems);
double StandardDevCalc(int inValues[], int numElems, double Ave);
void QSummary(int Score[], int numElems);
const int Size = 500; // Size declaration of array
int main()
{
int numElems;
int Score[Size];
double average;
double StandardDev;
ifstream inputFile; //Input file stream object
//Open file and validate file has open. Throw exception if necessary.
inputFile.open("pgm5data.txt");
if (!inputFile) cout << "Problems with file!\n";
//Read numbers from file into the array.
int count;
for (count = 0; count < Size && inputFile >> Score[count]; count++);
inputFile >> Score[count];
//Variable initalized to the known size of the array.
numElems = count;
cout << "You have " << numElems << " numbers of data elements in the data set" << endl;
//Function to calculate average score; results returned to main.
average = calcAvg(Score, numElems);
// Function call for Min and Max
minMax(Score, numElems);
//Display the average.
cout << "The average score is: " << average << endl << endl;
//StandardDev
StandardDev = StandardDevCalc(Score, numElems, average);
cout << "The standard deviaion is: " << StandardDev << endl;
// Q Summary
QSummary(Score, numElems);
system("pause");
return 0;
} // end main function
// Calculate the mean (average) value for the data set
double calcAvg(int score[], int numElems)
{
double sum = 0.0;
for (int i = 0; i < numElems; i++)
sum = sum + score[i];
return sum / numElems;
}
// Calculate the Mix/Min Value
void minMax(int score[], int numElems)
{
int i;
double max = score[0];
for (i = 1; i < numElems; i++)
{
if (score[i] > max)
max = score[i];
}
double min = score[0];
for (i = 1; i<numElems; i++)
{
if (score[i] < min)
min = score[i];
}
cout << "The minimum test score is: " << min << endl;
cout << "The maximum test score is: " << max << endl << endl;
}
// Calculate the standard deviation
// Using the array of data, number of elements, and the mean value
// This function should receive the array, array size,
// the average value of the dataset, and return the standard
// deviation as a floating point value
double StandardDevCalc(int inValues[], int numElems, double Ave)
{
double StandardDev;
double sum = 0;
double temp;
for (int i = 0; i < numElems - 1; i++)
{
temp = inValues[i] - Ave;
sum = sum + pow(temp, 2);
}
StandardDev = sqrt(sum / (numElems - 1));
return StandardDev;
}
// Determine and Write Quartile Summary
// Calculate and write the number of list values
// in each quartile to the console
void QSummary(int Score[], int numElems)
{
int i = 0, quartile4 = 0, quartile3 = 0, quartile2 = 0, quartile1 = 0;
while (i < numElems)
{
if (Score[i] > 74)
quartile4++;
else if (Score[i] > 49)
quartile3++;
else if (Score[i] > 24)
quartile2++;
else if (Score[i] < 24)
quartile1++;
}
cout << "Quartile 4, scores between 75 - 100: " << quartile4 << endl;
cout << "Quartile 3, scores between 50 - 74: " << quartile3 << endl;
cout << "Quartile 2, scores between 25 - 49: " << quartile2 << endl;
cout << "Quartile 1, scores between 0 - 24: " << quartile1 << endl;
}
|