#include <iostream> #include <fstream> #include <iomanip> using namespace std; c
ID: 3715720 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <iomanip>
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 main()
{
const int MAX_ROWS = 20;
string inputFileName;
double grades[MAX_ROWS][MAX_COLUMNS];
// Setting the precision
cout << setprecision(2) << fixed << showpoint;
cin >> inputFileName;
int actualRows = readFile(grades, MAX_ROWS, inputFileName);
if (actualRows == -1)
{
cout << "File "" << inputFileName << "" could not be opened" << endl;
}
else if (actualRows == 0)
{
cout << "The input file "" << inputFileName << "" did not contain any data" << endl;
}
else
{
double tot = 0.0, avg = 0.0;
for (int i = 0; i < actualRows; i++)
{
for (int j = 0; j < MAX_COLUMNS; j++)
{
tot += grades[i][j];
}
}
avg = tot / ((actualRows) * (MAX_COLUMNS));
cout << "Processing " << actualRows << " rows, and " << MAX_COLUMNS << " columns" << endl;
cout << "Average for all values is " << avg << endl;
for (int i = 0; i < MAX_COLUMNS; i++)
{
avg = 0;
tot = 0;
for (int j = 0; j < actualRows; j++)
{
tot += grades[j][i];
}
avg = tot / actualRows;
cout << "Average for column " << i << " is " << avg << endl;
}
double min;
for (int i = 0; i < actualRows; i++)
{
min = grades[i][0];
for (int j = 0; j < MAX_COLUMNS; j++)
{
if (min > grades[i][j])
min = grades[i][j];
}
cout << "Smallest value for row " << i << " is " << min << endl;
}
}
return 0;
}
int readFile(double grades[][MAX_COLUMNS], int MAX_ROWS, string inputFileName)
{
// defines an input stream for the data file
ifstream dataIn;
int i = 0, j = 0;
// Opening the input file
dataIn.open(inputFileName.c_str());
// checking whether the file name is valid or not
if (dataIn.fail())
{
return -1;
}
else
{
double grade;
while (dataIn >> grade)
{
if (j == 5)
{
j = 0;
i++;
}
grades[i][j] = grade;
j++;
}
if (i == 0)
{
dataIn.close();
return 0;
}
dataIn.close();
return i + 1;
}
}
Explanation / Answer
PLEASE REFER BELOW CODE
#include <iostream>
#include <fstream>
#include <iomanip>
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 main()
{
const int MAX_ROWS = 20;
string inputFileName;
double grades[MAX_ROWS][MAX_COLUMNS];
// Setting the precision
cout << setprecision(2) << fixed << showpoint;
cin >> inputFileName;
int actualRows = readFile(grades, MAX_ROWS, inputFileName);
if (actualRows == -1)
{
cout << "File "" << inputFileName << "" could not be opened" << endl;
}
else if (actualRows == 0)
{
cout << "The input file "" << inputFileName << "" did not contain any data" << endl;
}
else
{
double tot = 0.0, avg = 0.0;
for (int i = 0; i < actualRows; i++)
{
for (int j = 0; j < MAX_COLUMNS; j++)
{
tot += grades[i][j];
}
}
avg = tot / ((actualRows) * (MAX_COLUMNS));
cout << "Processing " << actualRows << " rows, and " << MAX_COLUMNS << " columns" << endl;
cout << "Average for all values is " << avg << endl;
for (int i = 0; i < MAX_COLUMNS; i++)
{
avg = 0;
tot = 0;
for (int j = 0; j < actualRows; j++)
{
tot += grades[j][i];
}
avg = tot / actualRows;
cout << "Average for column " << i << " is " << avg << endl;
}
double min;
for (int i = 0; i < actualRows; i++)
{
min = grades[i][0];
for (int j = 0; j < MAX_COLUMNS; j++)
{
if (min > grades[i][j])
min = grades[i][j];
}
cout << "Smallest value for row " << i << " is " << min << endl;
}
}
return 0;
}
int readFile(double grades[][MAX_COLUMNS], int MAX_ROWS, string inputFileName)
{
// defines an input stream for the data file
ifstream dataIn;
int i = 0, j = 0;
// Opening the input file
dataIn.open(inputFileName.c_str());
// checking whether the file name is valid or not
if (dataIn.fail())
{
return -1;
}
else
{
double grade;
while (dataIn >> grade)
{
if (j == 5)
{
j = 0;
i++;
}
grades[i][j] = grade;
j++;
}
if (i == 0)
{
dataIn.close();
return 0;
}
dataIn.close();
return i + 1;
}
}
THE COMPILATION ERROR WHICH YOU ARE GETTING IS NOT WITH THIS CODE. THE VARIABLE IN COMPILATION ERROR columnAverage IS NOT AT ALL USED OR EVEN DECLARED IN THIS CODE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.