c++ error: readFile returned a value of 1 when it should have been 0 readFile re
ID: 3719697 • Letter: C
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 1
-------------------
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
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<maxRows && fin.good(); i++)
{
for(j=0; j<MAX_COLUMNS; j++)
{
//Reading value
fin >> values[i][j];
}
}
//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<numberRows; i++)
{
//Iterating over columns
for(j=0; j<MAX_COLUMNS; j++)
{
//Accumulating sum
sum = sum + values[i][j];
}
}
//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<numberRows; i++)
{
//Accumulating sum
sum += values[i][column];
}
//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<MAX_COLUMNS; i++)
{
//Accumulating sum
if(values[rowNumber][i] < minVal)
{
//Updating minimum value
minVal = values[rowNumber][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<MAX_COLUMNS; cols++)
{
//Printing column wise average
cout << " Average for column " << cols << " is " << columnAverage(grades, cols, actualRows);
}
//Printing row wise smallest value
for(rows=0; rows<actualRows; rows++)
{
//Printing row wise smallest value
cout << " Smallest value for row " << rows << " is " << smallestValue(grades, rows);
}
cout << endl << endl;
return 0;
}
Explanation / Answer
The problem for this is in for loop first the value if i is incremented and then the condition will be checked. That is if we have a condition like for(i=0;i<5;i++) in this i will be incremented upto 5 and then it will b echecked so that was the problem. So the valiue to be returned is i-1 not 1.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.