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
|
//This program does the following:
// Input:
// List.txt data file
// Output:
// Build array of values from data
// calculate mean/max/min/standard deviation
// quartile analysis
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
const int INDEX = 500;
int theData[INDEX];
//Prototypes
void getData(int[INDEX], int&);
void getData(ifstream& fin, int vin[], int& vnum);
double getAverage(int vin[], int dsize);
double getMax(int vin[], int vnum);
double getMin(int vin[], int vnum);
double getSD(int vin[], int dsize);
int main()
{
int Enum = 0;
double mean = getAverage(theData, Enum);
cout << "|** Score Analysis **|" << endl << endl;
//Open data file
fstream fin;
fin.open("List.txt");
//Check if file opened correctly
if (fin.fail() )
{
cout << "Problem opening file";
//cin.get();
exit(-1);
}
//Establish max boundaries for index size
while (Enum < INDEX && !fin.eof())
{
fin >> theData[Enum];
++Enum;
}
//Average Calculation
cout << "Average Score: " << setprecision(2) << setw(4) << getAverage(theData, Enum) << endl;
mean = getAverage(theData, Enum);
//Max Calculation
cout << "Max Score: " << setprecision(2) << setw(4) << getMax(theData, Enum) << endl;
//Min Calculation
cout << "Min Score: " << setprecision(2) << setw(4) << getMin(theData, Enum) << endl << endl;
//Standard Deviation Calculation
cout << "Standard Deviation: " << setprecision(2) << setw(4) << getSD(theData, Enum) << endl;
//Close File
fin.close();
system("pause");
return 0;
}
//****Functions****\\
void getData(int[INDEX], int& dsize)
{
dsize = 0;
ifstream fin;
}
void getData(ifstream& fin, int vin[], int& vnum)
{
int i = 0;
fin >> vin[i];
while (!fin.eof() && i < INDEX)
{
i++;
fin >> vin[i];
}
vnum = i;
}
double getAverage(int vin[], int dsize)
{
double sum = 0.0;
for (int i = 0; i < dsize; i++)
sum = sum + vin[i];
return sum / dsize;
}
double getMax(int vin[], int vnum)
{
int max = vin[0], i = 0;
for(i=0; i < vnum; i++)
{
if (max < vin[i])
max = vin[i];
}
return max;
}
double getMin(int vin[], int vnum)
{
int min = vin[0], i = 0;
for(i=0; i < vnum; i++)
{
if (min > vin[i])
min = vin[i];
}
return min;
}
double getSD(int vin[], int dsize)
{
double sum = 0.0, sum2 = 0.0, avg = 0.0, variance = 0.0;
double SD = 0.0;
for (int i = 0; i < dsize; i++)
sum = sum + vin[i];
avg = sum / dsize;
for (int i = 0; i < dsize; pow((i - avg),2))
sum2 = sum2 + vin[i];
return SD = sum2 / dsize;
}
|