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
|
/************************ Start Program *****************************/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
bool openAndVerify(ifstream &, string &, int);
double averageCalc(ifstream &);
double stndrdDevCalc(ifstream &, string &, double, int);
void print(double, double, string, int);
string getBankName(ifstream &);
// Part E
int main() {
int counter, numDesired, numCount;
double mean[10];
double stndrdDev[10];
string bankName;
string fileName[10];
ifstream inputFile;
double min = 0;
string betterBank;
cout << "Greetings!\nEnter number of banks would you like to analyze: ";
cin >> numDesired;
counter = 0;
while(counter < numDesired) {
if(openAndVerify(inputFile, fileName[10], counter) == false) {
cout << "Error: bad file name. Program terminated!" << endl;
return 0;
} else {
bankName = getBankName(inputFile);
cout << bankName << endl;
mean[counter] = averageCalc(inputFile);
stndrdDev[counter] = stndrdDevCalc(inputFile, fileName[counter], mean);
if (counter == 0) {
betterBank = bankName;
min = stndrdDev[0];
}
if (stndrdDev[counter] < min){
min = stndrdDev[counter];
betterBank = bankName;
}
print(stndrdDev[counter], mean[counter], bankName);
counter++;
}
}
cout << betterBank << " is the preferred bank because it has the lowest standard deviation." << endl;
return 0;
}
// PART A
bool openAndVerify(ifstream &inputFile, string[] &fileName, int counter) {
cout << "Please enter file name: ";
//getline(inputFile, fileName);
//getline(cin, fileName);
cin >> fileName[counter];
cout << "Filename: " << fileName[counter] << endl;
inputFile.open(fileName[counter].c_str());
if(inputFile.fail()) {
inputFile.close();
return false;
} else {
return true;
}
}
// PART B
double averageCalc(ifstream &inputFile) {
int numEntries = 0;
double sum;
double time[20];
sum = 0;
while(!inputFile.eof()) {
inputFile >> time[numEntries];
sum += time[numEntries];
numEntries++;
}
inputFile.close();
return sum / numEntries;
}
// PART C
double stndrdDevCalc(ifstream &inputFile, string[] &fileName, double[] mean, int counter) {
int numEntries = 0;
double time, stndrdDevCalcNumerator = 0;
inputFile.open(fileName[counter].c_str());
inputFile.ignore(500, '\n');
while(!inputFile.eof()) {
inputFile >> time;
stndrdDevCalcNumerator += pow((time - mean), 2);
numEntries++;
}
inputFile.close();
return sqrt(stndrdDevCalcNumerator / (double(numEntries) - 1));
}
// Part D
void print(double[] stndrdDev, double[] mean, string bankName, int counter) {
cout << "===============================================" << endl;
cout << "Bank Name: " << bankName << endl;
cout << setprecision(3) << showpoint << fixed;
cout << "Average wait time: " << mean[counter] << endl;
cout << "Standard Deviation of the wait time list: " << stndrdDev[counter] << endl;
cout << "===============================================" << endl << endl;
}
string getBankName(ifstream &inputFile) {
string bankName;
getline(inputFile, bankName);
return bankName;
}
|