#include #include #include #include #include using namespace std; void getStats(double arr[], int size, double * mean, double * median, double * range); const string outFileName = "dataOut.txt"; const int places = 2, tableCols = 4; const int tableColW[tableCols] = {6, 7, 8, 7}; // This assignment is worth 5 points // No points were deducted for slight misaligned spacing on the table int main() { ifstream inputFile; string inFileName; cout << "Enter file name: "; cin >> inFileName; cin.ignore(); inputFile.open(inFileName); if(inputFile.fail()) { cout << "Error: File not found. Exiting..." << endl; inputFile.close(); return 0; } int k = 0; char ch = ' '; ofstream outputFile; outputFile.open(outFileName); outputFile << fixed << setprecision(places); outputFile << setw(tableColW[0]) << ' ' << '|' << setw(tableColW[1]) << "Mean " << '|' << setw(tableColW[2]) << "Median " << '|' << setw(tableColW[3]) << "Range " << '|' << endl; for(int i = 0; i < tableCols; i++) { for(int j = 0; j < tableColW[i]; j++) outputFile << '-'; outputFile << '+'; } outputFile << endl; double temp; int rows = 0, cols = 0; double * input = NULL; cout << "Now reading " << inFileName << "..." << endl; while(inputFile >> temp) // Get matrix size { cols++; if(inputFile.get() == '\n') rows++; } cols /= rows; //double input[cols]; input = new double[cols]; inputFile.close(); inputFile.open(inFileName); cout << "Writing results to " << outFileName << "..." << endl; for(int i = 0; i < rows; i++) { k = 0; double avg = 0, med = 0, rge = 0; while(inputFile >> input[k] && inputFile.get() != '\n') k++; getStats(input, cols, &avg, &med, &rge); outputFile << "Row " << i+1 << " |" << setw(tableColW[1]-1) << avg << " |" << setw(tableColW[2]-1) << med << " |" << setw(tableColW[3]-1) << rge << " |" << endl; } inputFile.close(); outputFile.close(); cout << "Done" << endl; return 0; } void getStats(double arr[], int size, double * mean, double * median, double * range) { int index_sav; for(int i = 0; i < size; i++) // Sort the values first { index_sav = i; for(int j = i+1; j < size; j++) if(arr[j] < arr[index_sav]) index_sav = j; if(index_sav != i) { arr[i] += arr[index_sav]; arr[index_sav] = arr[i] - arr[index_sav]; arr[i] -= arr[index_sav]; } } double sum = 0; for(int i = 0; i < size; i++) sum += arr[i]; *mean = sum/size; if(size % 2 == 0) *median = (arr[size/2 - 1] + arr[size/2])/2; else *median = arr[size/2]; *range = arr[size-1] - arr[0]; }