Question: Here is the Code file .C /* asg3.c, Currently progam has functions for dynamic allocation of 2D dimensional arrays and initializes matrices You need to
Here is the Code file .C
/* asg3.c, Currently progam has functions for dynamic allocation of 2D dimensional arrays and initializes matrices You need to the multiplication A^T A , with result stored in C Command Line: Compile: gcc asg3.c Run: ./a.out
*/
#include
/* Function Prototypes */ void fillMatrixValues(float **, float **, int, int); float **allocateMatrix(int, int); void freeMatrix(float **,int); /* See Function Definitons below main
/* START MAIN */ int main(void) {
int m,n; clock_t begin, end; double timeSpent;
int row,col,k;
/*Get User Input on Matrix Dimension and Allocate Memory For Matrices*/ printf(" What is the matrix row dimension? "); scanf("%d",&m); printf("What is the matrix column dimension? "); scanf("%d",&n); float **A = allocateMatrix(m,n); float **C = allocateMatrix(n,n); printf(" Memory Allocated for A,C ");
/* Fill out the matrices A,C */ fillMatrixValues(A,C,m,n); printf("A, C now initialized. "); /* Matrix Definiton Routine is Finished */ begin = clock();
/* DO THE MATRIX MULT LOOPS HERE or Call the Matrix Mult Function Here */
end = clock(); if (n>2) { printf("Row 1 (first 3 entries): %8.4f, %8.4f, %8.4f ",C[0][0],C[0][1],C[0][2]); printf("Row 2 (first 3 entries): %8.4f, %8.4f, %8.4f ",C[1][0],C[1][1],C[1][2]); printf("Row 3 (first 3 entries): %8.4f, %8.4f, %8.4f ",C[2][0],C[2][1],C[2][2]); } timeSpent = (double)(end - begin) / CLOCKS_PER_SEC; printf("Matrix Mult Time: %.3e ",timeSpent); /* Deallocate Pointers */ printf("Deallocating Memory for A,C "); freeMatrix(A,m); freeMatrix(C,n); /* Finished DeAllocation */ printf("Press any key to end this madness. "); getch(); return (0);
}
/* Function for allocating a matrix of float */ float **allocateMatrix(int nRows,int nCols) { int row; float **M; /* variable declaration */
/* Dynamic Allocation of Matrix M using malloc */ M = (float **) malloc( nRows* sizeof (float*) ); for (row=0;row /* Function for Freeing 2D arrays */ void freeMatrix(float **M,int nRows) { int row; for(row=0; row void fillMatrixValues( float **A, float **B,int m, int n) { int row, col; float top,bot; for(row=0;row }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
