514 views
0 votes
This program will read the contents of a file into an array and calculate various values based on the contents of the file.

The program will make use of a two dimensional array that has 20 rows and 5 columns.

You will be reading in the contents from a file. You will need to read the file name from cin.

The file will consist of up to 20 sets of 5 values. The values will all be double floating point values.

readFile function

One function you will be required to have is called readFile. This function will read the input file. Each read will read in 5 columns of information into the next available row in the two dimensional array. The first 5 values are read into row 0, the next 5 values will be read into row 1, and so on up to 20 rows of input. You will need to keep track of how many rows of input you have read in. This could be anywhere from 0 to 20. If there are more than 20 rows of input you should only read in the first 20 rows.

To help facilitate this you need to create a global const int value for the maximum number of columns.

Your program should use thisconst value when creating the arrays or when defining function prototypes and function headers.

You will also be creating a const int value for the maximum number of rows, but this will be in your main function.

You should not be hard coding 5 or 20 in your code anywhere except in the two const int declarations. The only possible exception to this is the readFile function. It is easier to just read in all 5 columns at a time. If you want to just read in an entire row at a time you can do that in the readFile function. It is possible to write your code to use the MAX_COLUMNS value and read in one value at a time, but it makes the program logic more difficult. Once you have your code working and submitted you might want to see if you can read in the values using MAX_COLUMNS and reading in just one value at a time. This enhancement is not required for the assignment.

No other use of global variables is allowed.

User Miyagisan
by
4.5k points

1 Answer

6 votes

Answer:

See explaination

Step-by-step 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;

}

User Ghazyy
by
4.8k points