Question
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
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.
Note that in the above code we are not including the max number of rows in the array when we pass it as the parameter on the readFilefunction. That is done so we could use the readFile function for different arrays with a different number of possible rows.
The readFile function will open the file and read the data into the passed in array. It will also close the file once it has read in the data. If the file cannot be opened the function should return -1 to the calling function. If the file does not contain any data (or contains less than 5 values) it will return 0. Otherwise the function returns the number of row read, up to 20. The file could contain more than 20 rows of data, but you should only read in the first 20. Do not try to read into the array past the valid limits of the array.
main processing
In this program you are going to read in the file name from cin. The file name will be passed to the readFile function. If the file exists the input file will contain 0 or more rows. The readFile function will return -1 if the file does not exist and 0 to MAX_ROWS depending on how many rows of data were actually in the file.
Assume a file name of badfile.txt. If the file does not exist you should output the following message from main: and exit the program.
Assume now that input file grades.txt exists but does not contain any valid data. The number of rows returned from readFile is 0. Output the following message from main:
The input file "grades.txt" did not contain any data
and exit the program.
If there are 1 to 20 rows the program should:
Calculate and display the average of all of values read into the array
Calculate and display the average for every column in the array
Find and display the smallest value for every row in the array
You will need to have different functions to:
Calculate the average for the array
Calculate the average for a specified column
Find the smallest value in a specified row
You can have additional functions if you want them, but you must have main, readFile and the 3 listed above.
The signature for the get average functions are:
// for the array double average(double values[][MAX_COLUMNS], int numberRows); // for a specified column (column) double columnAverage(double values[][MAX_COLUMNS], int column, int numberRows);
There will be unit tests for readFile, average, and columnAverage.
The signatures for other functions are up to you.
See the sample runs for the output requirements. The averages and smallest values must all be written with two digits to the right of the decimal point.
The main function will be the driver. It should create the array, read in the file name, and call all of the other functions, processing the logic as required.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started