Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a directory called lab7 on your machine using mkdir lab7. Within this dir

ID: 3801593 • Letter: C

Question

Create a directory called lab7 on your machine using mkdir lab7. Within this directory, complete the program named lab7.c that is shown below. You need to write the five functions shown in red. #include #include int **allocateMatrix(int, int); void initMatrix(int **, int, int); int findRange(int **, int, int); double findAverage(int **, int, int); void printCorners(int **, int, int); int main(int argc, char *argv[]) { srand(0); int **data; int row = atoi(argv[1]); int col = atoi(argv[2]); data = allocateMatrix(row, col); initMatrix(data, row, col); printf("Range of values in the array is %d ", findRange(data, row, col) ); printf("Average of all array values is %lf ", findAverage(data, row, col) ); printCorners(data, row, col); return 0; } A sample execution of this program is shown below ./a.out 2 2 The range of values in the array is 532 The average of all array values is 740.250000 383 886 777 915 A brief description of each of the three functions you have to write is given below: • allocateMatrix – Allocates space for the matrix, returning a pointer to that location in memory. Recall that to allocate space for the matrix, you: o Declare an int ** variable o Assign that variable to the result of allocating space for the “rows” in the matrix o For each row pointer in the matrix, allocate space for all integers on that row (column integers) • initMatrix – Initialize all values in the matrix to a random value in range of 0-999. • findRange – Calculates and returns the range of the elements in the matrix. Recall the range of an matrix is the largest value minus the smallest value in the matrix. • findAverage – Calculates and returns the average value (a double) of all elements in the matrix • printCorners – prints the four values at the corners of the matrix.

Explanation / Answer

Point to Note: Use srand((unsigned)time(NULL)) instead of srand(0) to avoid generating the same random numbers for different runs of the program.

#include <stdio.h>
#include <stdlib.h>

int **allocateMatrix(int row,int col) {
   int **mat = (int **)malloc(row * sizeof(int*));
   for(int i=0;i<row;++i) {
       mat[i] = (int *)malloc(col * sizeof(int));
   }

   return mat;
}

void initMatrix(int **mat,int row,int col) {
   for(int i=0;i<row;++i) {
       for(int j=0;j<col;++j) {
           mat[i][j] = rand()%999; // to keep range between 0 to 999
       }
   }
}

int findRange(int **mat,int row,int col) {
   int range = 0, min = 9999, max = -1;
   for(int i=0;i<row;++i) {
       for(int j=0;j<col;++j) {
           if(mat[i][j]<min) {
               min = mat[i][j];
           }

           if(mat[i][j]>max) {
               max = mat[i][j];
           }
       }
   }

   range = max-min;
   return range;
}

double findAverage(int **mat,int row,int col) {
   int total=0;
   for(int i=0;i<row;++i) {
       for(int j=0;j<col;++j) {
           total+=mat[i][j];
       }
   }
   int nums = row*col; // total number of elements in the matrix
   double ans = (double)total/nums; // typecast int total to double
   return ans;
}

void printCorners(int **mat,int row,int col) {
   printf("corner[0][0]-%d, corner[0][col-1]-%d, corner[row-1][0]-%d, corner[row-1][col-1]- %d",mat[0][0],mat[0][col-1],mat[row-1][0],mat[row-1][col-1]);
}

int main(int argc, char *argv[]) {
   srand(0); // should use "srand ((unsigned)time(NULL))" to avoid getting same random numbers;
   int **data;
   int row = atoi(argv[1]);
   int col = atoi(argv[2]);
   data = allocateMatrix(row, col);
   initMatrix(data, row, col);
   printf("Range of values in the array is %d ", findRange(data, row, col) );
   printf("Average of all array values is %lf ", findAverage(data, row, col) ); printCorners(data, row, col);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote