Question
You will develop a program in ANSI C that reads a two-dimensional table of integers from a text file and prints out the sum, average
You will develop a program in ANSI C that reads a two-dimensional table of integers from a text file and prints out the sum, average and maximum value of each row in a report-like format. The name of the input text file will be submitted as an entry (i.e., an argument) on the command line rather than through command line redirection.
The program should read in each integer as an integer value, not as a character string. It should also make use of the following standard C functions: printf(), fprintf(), fopen(), fscanf(), and fclose(). In addition, the program should only implement (i.e., define) the main function and each of the functions whose prototypes are listed below.
int main(int argc, char *argv[])
This function displays the program usage message shown below and terminates the program if the number of command line arguments is incorrect.
Usage: a.out
Otherwise, it attempts to open the input text file whose name was specified on the command line. If the program cannot open the file, it prints the error message "Error: Cannot open the file" and terminates; otherwise, it calls the readTableSize() function to get the number of rows and columns for the table in the file. If that function returns FALSE, main() prints the error message "Error: Row and/or column values are out of range" and terminates the program; otherwise, it calls the processTableContents() function. When that function returns, main() closes the input text file and terminates the program.
int readTableSize(FILE *inputFile, int *rowCountPtr, int *colCountPtr)
This function reads the row and column values in the first line of the input file and stores them in local variables. It then checks the range of each value. If both values are within the proper range (stated earlier for the input text file format), it assigns the values, respectively, to *rowCountPtr and *colCountPtr, and returns TRUE; otherwise, it assigns no values to the function parameters and returns FALSE.
void processTableContents(FILE *inputFile, int rowCount, int columnCount)
This function calls the writeReportHeading() function and then enters a for loop that calls the readTableRow() and writeReportRow() functions in order to read a table row and print a report row until all of the rowCount rows in the file are read.
void writeReportHeading(void)
This function prints the column headings of the report. It also prints the dashes underneath each column heading.
void readTableRow(FILE *inputFile, int nbrOfEntries, int rowEntries[])
This function reads a row of integers from the table in the input file and stores them in the rowEntries array.
void writeReportRow(int rowNbr, int nbrOfEntries, int rowEntries[])
This function first computes the sum, average, and maximum value of the values in the rowEntries array. It then prints the row number, the sum, the average, and the maximum value (all to standard out) in formatted columns as shown in the section above for Output Report Format.
Output:
Row Sum Avg Max --- --- ---- --- 1 xxx xx.x xxx 2 xxx xx.x xxx xx xxx xx.x xxx
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