Question
In C Language please main.c #include #include matrix.h int main(int argc, char *argv[]) { if (argc!=2) { printf(Usage: %s datafile , argv[0]); return 1; }
In C Language please
main.c
#include
#include "matrix.h"
int main(int argc, char *argv[]) { if (argc!=2) { printf("Usage: %s datafile ", argv[0]); return 1; }
FILE *fp=fopen(argv[1], "r"); if (fp==NULL) { printf("Unable to open %s for reading ", argv[1]); return 2; }
int **matrix; int numRows, numCols; fscanf(fp, "%d%d", &numRows, &numCols); printf("numRows=%d ", numRows); printf("numCols=%d ", numCols);
matrix = allocateMatrix(numRows, numCols);
readMatrix(fp, matrix, numRows, numCols); printMatrix(matrix, numRows, numCols); printf("The range of the matrix is %d ", findRange(matrix, numRows, numCols) ); printf("The average of the matrix is %g ", findAverage(matrix, numRows, numCols) ); printCorners(matrix, numRows, numCols);
freeMatrix(matrix, numRows, numCols);
return 0; }
matrix.h
#ifndef _MATRIX_H_ #define _MATRIX_H_
#include
// Given the number of rows and columns, allocates space for the matrix, // and return the pointer to the matrix. int **allocateMatrix(int numRows, int numCols);
// Free the space allocated for the matrix. void freeMatrix(int **matrix, int numRows, int numCols);
// Populate the matrix using the data read from the specified file. void readMatrix(FILE *fp, int **matrix, int numRows, int numCols);
// Print out the matrix on standard output. void printMatrix(int **matrix, int numRows, int numCols);
// Compute the range of all the elements in the matrix. // Recall the range is the maximum minus the minimum. int findRange(int **matrix, int numRows, int numCols);
// Compute the average (a double) of all the elements in the matrix. double findAverage(int **matrix, int numRows, int numCols);
// Print the four values at the corners of the matrix in the following format. // top-left top-right // bottom-left bottom-right void printCorners(int **matrix, int numRows, int numCols);
#endif
Create a directory called lab7. Download main.candmatrix.h from Blackboard to lab7. Under lab7, create matrix.c, and implement the following functions to deal with a matrix of integers. . int*allocateMatrix (int numRows, int numCols) Given the number of . void freeMatrix (int **matrix, int numRows, int numCols)Free thie . void readMatrix (FILE *Ep, int **matrix, int numRows, int numCols) . void printMatrix (int **matrix, int numRows, int numColsPrint out .int findRange (int **matrix, int numRows, int numCols) Compute the rows and columns, allocates space for the matrix, and retum the pointer to the matrix space allocated for the matrix Populate the matrix using the data read from the specified file. the matrix on standard output. range of all the elements in the matrix. Recall the . double findAverage (int **matrix, int numRows, int numCols) Compute the average (a double) of al the elements in the matrix. is the maximum minus the minimum . void printCorners (int **matrix, int numRows, int numCols)Print the four values at the corners of the matrix in the following format. top-left bottom-left bottom-right top-right After completing the above functions, use the following command to compile the program gcc-Wall-std-99 main.c matrix. Assume the file data.txt contains the following data. 3 4 24 20 11 12 28 19 1B 13 21 16 25 23 The following shows a sample execution of the program. a.out data.txt numRows-3 numCo1s-4 24 20 11 12 28 19 18 13 21 16 25 23 The range of the matrix is 17 The average of the matrix is 19.1667 24 12 21 23Step 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