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 30 rows and 5 columns.  You will be reading in the contents from a file. You will need to read the file name from cin. You will prompt for the file name with the prompt: Enter input file name The file will consist of up to 30 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 30 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 30. If there are more than 30 rows of input you should only read in the first 30 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 30 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.  No other use of global variables is allowed. Here is an example of some partial code: ... 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 = 30;    string inputFileName;    double grades[MAX_ROWS][MAX_COLUMNS];    ...    int actualRows = readFile(grades, MAX_ROWS,    inputFileName);     ...    } ... 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 readFile function. 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 30. The file could contain more than 30 rows of data, but you should only read in the first 30. 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. If there are 1 to 30 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 signatures for the average and columnAverage 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 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. Sample run 1 (valid data) Contents of cin: grades1.txt Contents of grades1.txt: 61.4 89.5 62.6 89.0 100.0 99.5 82.0 79.0 91.0 72.5 Here is the output to cout: Enter input file name Processing 2 rows, and 5 columns Average for all values is 82.65 Average for column 0 is 80.45 Average for column 1 is 85.75 Average for column 2 is 70.80 Average for column 3 is 90.00 Average for column 4 is 86.25 Smallest value for row 0 is 61.40 Smallest value for row 1 is 72.50 Sample run 2 (invalid file name) Contents of cin: badfile.txt Here is the output to cout: Enter input file name File "badfile.txt" could not be opened Sample run 3 (value, but empty file) Contents of cin: grades.txt Here is the output to cout: Enter input file name The input file "grades.txt" did not contain any data

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Use C++

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 30 rows and 5 columns. 

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

Enter input file name

The file will consist of up to 30 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 30 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 30. If there are more than 30 rows of input you should only read in the first 30 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 30 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. 

No other use of global variables is allowed.

Here is an example of some partial code:

... 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 = 30;

   string inputFileName;

   double grades[MAX_ROWS][MAX_COLUMNS];

   ...

   int actualRows = readFile(grades, MAX_ROWS,

   inputFileName);

    ...

   }

...

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 readFile function. 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 30. The file could contain more than 30 rows of data, but you should only read in the first 30. 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.

If there are 1 to 30 rows the program should:

  1. Calculate and display the average of all of values read into the array
  2. Calculate and display the average for every column in the array
  3. Find and display the smallest value for every row in the array

You will need to have different functions to:

  1. Calculate the average for the array
  2. Calculate the average for a specified column
  3. 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 signatures for the average and columnAverage 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 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.

Sample run 1 (valid data)

Contents of cin:

grades1.txt

Contents of grades1.txt:

61.4 89.5 62.6 89.0 100.0

99.5 82.0 79.0 91.0 72.5

Here is the output to cout:

Enter input file name

Processing 2 rows, and 5 columns

Average for all values is 82.65

Average for column 0 is 80.45

Average for column 1 is 85.75

Average for column 2 is 70.80

Average for column 3 is 90.00

Average for column 4 is 86.25

Smallest value for row 0 is 61.40

Smallest value for row 1 is 72.50

Sample run 2 (invalid file name)

Contents of cin:

badfile.txt

Here is the output to cout:

Enter input file name File "badfile.txt" could not be opened

Sample run 3 (value, but empty file)

Contents of cin:

grades.txt

Here is the output to cout:

Enter input file name The input file "grades.txt" did not contain any data

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education