Answer:
see explaination
Explanation:
#Header file section
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX_COLUMNS = 5;
//readFile function reads the input file and populates the array
int readFile(double values[][MAX_COLUMNS], int maxRows, string inputFileName)
{
//variable declaration
int i, j;
string line;
//Openin the file
fstream fin(inputFileName, ios::in);
//Check file exists or not
if (fin.fail())
{
//Failed to open file
return -1;
}
else
{
//Read the data
for (i = 0; i<maxRows && fin.good(); i++)
{
for (j = 0; j<MAX_COLUMNS; j++)
{
//Read the value from each row
fin >> values[i][j];
//Return 0 if the file contains no data
if (fin.fail() && i == 0)
{
return 0;
}
}
}
}
//Close the file
fin.close();
//Return the number of rows read
return i;
}
//Avearge function calcuates the average of the array
double average(double values[][MAX_COLUMNS], int numberRows)
{
double sum = 0;
int i, j;
//Iterate over rows
for (i = 0; i<numberRows; i++)
{
//Iterate over columns
for (j = 0; j<MAX_COLUMNS; j++)
{
//Calculate the sum
sum = sum + values[i][j];
}
}
//Find the avearge and return it
return (sum / (double)(numberRows*MAX_COLUMNS));
}
//columnAverage function calculates the average of each column
double columnAverage(double values[][MAX_COLUMNS], int column, int numberRows)
{
double sum = 0;
int i;
//Iterate over rows
for (i = 0; i<numberRows; i++)
{
//Calculate the sum
sum += values[i][column];
}
//Find the avearge and return it
return (sum / (double)(numberRows));
}
//smallestValue function returns the smallest value in each row
double smallestValue(double values[][MAX_COLUMNS], int rowNumber)
{
double minVal = values[rowNumber][0];
int i;
//Iterate over columns
for (i = 0; i<MAX_COLUMNS; i++)
{
//Calculate the sum
if (values[rowNumber][i] < minVal)
{
//Update the 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);
//Read file name
//cout << "Enter input file name: ";
cin >> inputFileName;
//Read data from file
actualRows = readFile(grades, MAX_ROWS, inputFileName);
//Check number of rows
if (actualRows == -1)
{
//Print error message
cout << endl << "File \"" << inputFileName << "\" could not be opened " << endl;
}
else if (actualRows == 0)
{
//Print error message
cout << endl << "The input File \"" << inputFileName << "\" did not contain any data " << endl;
}
else
{
cout << "Processing " << actualRows << " rows, and " << MAX_COLUMNS << " columns " << endl;
//Print average value
cout << "Average for all values is " << average(grades, actualRows) << endl;
//Print column wise average
for (cols = 0; cols < MAX_COLUMNS; cols++)
{
//Print column wise average
cout << "Average for column " << cols << " is " << columnAverage(grades, cols, actualRows) << endl;
}
//Print row wise smallest value
for (rows = 0; rows < actualRows; rows++)
{
//Print row wise smallest value
cout << "Smallest value for row " << rows << " is " << smallestValue(grades, rows) << endl;
}
}
return 0;