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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <iostream> // for screen and kbd io
#include <iomanip>
#include <fstream> // file io
#include <cstdlib> // for exit()
#include <cmath>// for sqrt
#include <string>
#include <vector>
using namespace std;
void fill_vector(ifstream& in_file, vector<double>& v, double a[], const int size, int& number_used);
//Precondition: in_file is open
//Postcondition: original vector v is cleared (all elements are deleted)
// and v.size() set to 0
//v[0] through v[v.size()-1] have been filled with the values read sequentially
//from the text file opened (represented by in_file).
//The values are filled starting at index 0.
//When eof of file is reached, the number of value read must be equal to v.size().
void sort(vector<double>& v, double a[], int number_used);
//Algorithm to use: Selection Sort.
//Postcondition: The values of v[0] through v[v.size() - 1] have
//been rearranged so that v[0] <= v[1] <= ... <= v[v.size() - 1].
void swap_values(double& v1, double& v2);
//Interchanges the values of v1 and v2.Project 6 Computing Statistics Using Vectors: Mean, Standard Deviation, Median
int index_of_smallest(const vector<double>& v, int start_index, const double a[], int number_used);
//Precondition: 0 <= start_index < v.size()-1.
//Returns the index i such that v[i] is the smallest of the values
//v[start_index], v[start_index + 1], ..., v[v.size() - 1].
double mean_average(const vector<double>& v, double value[], const int size, int number_of_values );
//Precondition: v.size() > 0
//Postcondition: mean_avg returns the mean average of values in v[0]...v[v.size()-1]
//Errors: values.size() == 0 mean_average returns error code: 2.
double median_average(const vector<double>& v, const int size, int number_of_values);
//Precondition: values.size() > 0
//Postcondition: median_average returns the median_average of values in
//v[0]...v[v.size()-1]
//Errors: v.size() == 0 mean_average returns error code: 2.
double standard_deviation(const vector<double>& v, const double value[]);
//Precondition: v.size() > 0
//Postcondition: standard_deviation returns the standard_deviation of values in
//v[0]...v[v.size()-1]
//Errors: v.size() == 0 standard_deviation returns error code: 2.
const int MAX_SIZE = 1000;
int main()
{
ifstream input;
char name[16];
double value[MAX_SIZE];
double average;
double stdDeviation;
double median;
int number_of_values = 0;
cout << "Please enter a file name: " << endl;
cin >> name;
input.open(name);
if (input.fail())
{
cout << "Cannot open file " << name
<< " Aborting." << endl;
exit(1);
}
fill_vector(input, value, MAX_SIZE, number_of_values);
input.close(); //close the input file for reading
if (number_of_values > 0) {
//compute stats
average = mean_average(value, MAX_SIZE, number_of_values);
median = median_average(value, MAX_SIZE, number_of_values);
stdDeviation = standard_deviation(value, MAX_SIZE, number_of_values);
//output format specifications
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(6);
cout << "Average of "
<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
<< average
<< endl;
cout << "Standard Deviation of "
<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
<< stdDeviation << endl;
cout << "Median of "
<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
<< median << endl;
}
else
cout << "File " << name << " is an empty file.\n";
return 0;
}
void fill_vector(ifstream& in_file, double value[], int size, int& number_used, vector<double>& v)
{
while (in_file >> ws && !in_file.eof() && number_used < size)// compute average
{
number_used++;
in_file >> value[number_used - 1];
}
}
void sort(vector<double>& v, double a[], int number_used)
{
//selection sort
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{//Place the correct value in a[index]:
index_of_next_smallest =
index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
//a[0] <= a[1] <=...<= a[index] are the smallest of the original array
//elements. The rest of the elements are in the remaining positions.
}
}
void swap_values(double& v1, double& v2)
{
double temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(const vector<double>& v, const double a[], int start_index, int number_used)
{
double min = a[start_index];
int index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
//min is the smallest of a[start_index] through a[index]
}
return index_of_min;
}
double mean_average(const vector<double>& v, const double value[], const int size, int number_of_values)
{
double sum = 0.0;
if (number_of_values > size)
number_of_values = size;
for (int i = 0; i < number_of_values; i++)
sum = sum + value[i];
if (number_of_values > 0)
return sum / number_of_values;
else if (number_of_values == 0) {
cerr << "mean_average run-time error: number of values = " << number_of_values << endl;
exit(2);
}
else {
cerr << "mean_average run-time error: number of values = " << number_of_values << endl;
exit(3);
}
}
double median_average(const vector<double> & v, double value[], const int size, int number_of_values)
{
if (number_of_values > size)
number_of_values = size;
sort(value, number_of_values);
if (number_of_values > 0) {
return (number_of_values % 2) ?
value[(number_of_values) / 2]
: ((value[number_of_values / 2 - 1] + value[number_of_values / 2]) / 2);
}
else if (number_of_values == 0) {
cerr << "median_average run-time error: number of values = " << number_of_values << endl;
exit(2);
}
else {
cerr << "median_average run-time error: number of values = " << number_of_values << endl;
exit(3);
}
}
double standard_deviation(const vector <double>& v, const double value[], const int size, int number_of_values)
{
double sum = 0.0;
double stdDeviation;
if (number_of_values > size)
number_of_values = size;
double average = mean_average(value, size, number_of_values);
for (int i = 0; i < number_of_values; i++){
sum = sum + (value[i] - average)*(value[i] - average);
}
//note that these errors below should be caught by mean average
//but cannot assume that some other programmer might not
//change the error processing in mean average.
if (number_of_values > 1)
return sqrt(sum / (number_of_values - 1));
else if (number_of_values == 1) {
return 0.0;
}
else {
cerr << "standard_deviation run-time error: number of values =" << number_of_values <<
endl;
exit(2);
}
}
|