Question: c++ error: readFile returned a value of 1 when it should have been 0 readFile returned a value of 2 when it should have been
c++
error:
readFile returned a value of 1 when it should have been 0
readFile returned a value of 2 when it should have been 1
-------------------
#include
using namespace std;
const int MAX_COLUMNS = 5;
// read input file and populate the array int readFile(double values[][MAX_COLUMNS], int maxRows, string inputFileName) { int i, j;
//Opening file fstream fin(inputFileName, ios::in);
//Checking file if(fin.fail()) { //Failed to open file return -1; }
//Reading data for(i=0; i
//Closing file fin.close();
//Return number of rows read return i; }
//For complete array double average(double values[][MAX_COLUMNS], int numberRows) { double sum=0; int i,j;
//Iterating over rows for(i=0; i //Finding and returning average return (sum/(double)(numberRows*MAX_COLUMNS)); } //for a specified column double columnAverage(double values[][MAX_COLUMNS], int column, int numberRows) { double sum=0; int i; //Iterating over rows for(i=0; i //Finding and returning average return (sum/(double)(numberRows)); } //Smallest value in each row double smallestValue(double values[][MAX_COLUMNS], int rowNumber) { double minVal=values[rowNumber][0]; int i; //Iterating over columns for(i=0; i //Return min value return minVal; } //Main function int main() { int rows, cols; const int MAX_ROWS = 20; string inputFileName; double grades[MAX_ROWS][MAX_COLUMNS]; int actualRows; //Set to two decimal places cout << fixed << setprecision(2); //Reading file name cin >> inputFileName; //Reading data from file actualRows = readFile(grades, MAX_ROWS, inputFileName); //Checking number of rows if(actualRows == -1) { //Printing error message cout << endl << " File \"" << inputFileName << "\" could not be opened " << endl; return -1; } cout << "Processing " << actualRows << " rows, and " << MAX_COLUMNS << " columns "; //Printing average value cout << " Average for all values is " << average(grades, actualRows); //Printing column wise average for(cols=0; cols //Printing row wise smallest value for(rows=0; rows cout << endl << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
