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
|
#include<iostream>
#include<fstream>
#include <iomanip>
#include<string>
#include<vector>
#include<sstream>
#include<iterator>
#include<stdlib.h>
using namespace std;
const int MAX_COLUMNS = 5; //max number of columns
int readFile(double values[][MAX_COLUMNS], int maxRows, string inputFileName); // prototypes
double average(double values[][MAX_COLUMNS], int numberRows); //
double columnAverage(double values[][MAX_COLUMNS], int column, int numberRows);
double smallestValue(double values[][MAX_COLUMNS], int row);
int main() { //start main function
const int MAX_ROWS = 20; //max number of rows
string inputFileName; //input file to be read
cin >> inputFileName;
double grades[MAX_ROWS][MAX_COLUMNS];
int actualRows = readFile(grades, MAX_ROWS, inputFileName); //if file has errors prints out error message
if (actualRows == -1) {
cout << "File \"" << inputFileName << "\" could not be opened" << endl;
}
else if (actualRows == 0) { //if rows are zero prints error
cout << "The input file \"" << inputFileName << "\" did not contain any data" << endl;
}
else {
cout << "Processing " << actualRows << " rows, and " << MAX_COLUMNS << " columns" << endl;
double ave = average(grades, actualRows);
printf("Average for all values is %.2f\n", ave); // prints average for all values
for (int i = 0; i<MAX_COLUMNS; i++) {
double colAvg = columnAverage(grades, i, actualRows);
std::setprecision(2);
printf("Average for column %d is %.2f\n", i, colAvg); // prints average for columns
}
for (int i = 0; i<actualRows; i++) {
double smallest = smallestValue(grades, i);
std::setprecision(2);
printf("Smallest value for row %d is %.2f\n", i, smallest); //smallest value for rows
}
}
return 0;
} //end main function
int readFile(double values[][MAX_COLUMNS], int maxRows, string inputFileName) { // puts input file into an array
ifstream file;
file.open(inputFileName.c_str());
if (file) {
string line;
int rows = 0;
while (getline(file, line)) {
std::istringstream ss(line);
std::istream_iterator<std::string> begin(ss), end;
std::vector<std::string> arrayTokens(begin, end);
for (int i = 0; i<MAX_COLUMNS; i++) {
double val = atof(arrayTokens[i].c_str());
values[rows][i] = val;
}
rows++;
if (rows == 20) {
break;
}
}
file.close();
return rows;
}
return -1;
} // end function
double average(double values[][MAX_COLUMNS], int numberRows) { // function for calculation of average value
double sum = 0;
for (int i = 0; i<numberRows; i++) {
for (int j = 0; j<MAX_COLUMNS; j++) {
sum += values[i][j];
}
}
return sum / (numberRows*MAX_COLUMNS);
} //end function
double columnAverage(double values[][MAX_COLUMNS], int column, int numberRows) { // calculate column average
double sum = 0;
for (int i = 0; i<numberRows; i++) {
sum += values[i][column];
}
return sum / numberRows;
} //end function
double smallestValue(double values[][MAX_COLUMNS], int row) { //callculate smallest value
double value = 999999;
for (int i = 0; i<MAX_COLUMNS; i++) {
if (values[row][i]<value) {
value = values[row][i];
}
}
return value;
} //end function
|