Question: I have to develop a C program for the MM to deal with any two valid matrices. The two matrices are read into the program

I have to develop a C program for the MM to deal with any two valid matrices. The two matrices are read into the program from two files, m1.txt and m2.txt, and I have to save the result in another file called mr.txt. I can compare my results with the file mmresult.txt for validation.

Note that the two integers in the first line of each file represent the numbers of rows and columns respectively. So, I need to dynamically create two 2D arrays to store the two matrices for late calculation.

I also have to calculate both CPU and Clock time.

The code below is how far I have got. Basically I have created Matrix A (populate from m1.txt) and Matrix B (populate from m2.txt) and calculated the results and print to file (mr.txt) and also calculated CPU and Clock time. The program works and the results match the mmresults.txt which is great! The problem I am having is writing the code that commits Matrix A and Matrix B to a dynamic 2D array which can accept data from any two inputs and decide the number of columns and rows based on the contents of the .txt file. so basically its 'Note that the two integers in the first line of each file represent the numbers of rows and columns respectively. So, I need to dynamically create two 2D arrays to store the two matrices for late calculation.' That I am having problems with. Please help!

#include #include #include #include

int main() { FILE *fA,*fB,*fC; int num,i,j,k,sum = 0; int matrixA[10][10],matrixB[10][10],matrixC[10][10];

///--------------------------------------------------------------" ///MATRIX A fA=fopen("m1.txt","r");///Opening matrixA File in READ MODE if(fA==NULL) { printf("The matrixA file not found "); return 0; } ///Reading the matrixA File data for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { fscanf(fA,"%d",&matrixA[i][j]); }

} fclose(fA); ///"--------------------------------------------------------------"

///MATRIX B fB=fopen("m2.txt","r");///Opening matrixB File in READ MODE if(fB==NULL) { printf("The matrixB file not found "); return 0; } ///Reading the matrixB File data for(i = 0; i < 3; i++) { for(j = 0; j < 2; j++) { fscanf(fB,"%d",&matrixB[i][j]); }

} fclose(fB);

///--------------------------------------------------------------"; /// Matrix Multiplication /// MatrixC = matrixA * matrixB for(i = 0; i < 2; i++) { for(j = 0 ; j < 2; j++ ) { for(k = 0; k < 4; k++) { sum = sum + matrixA[i][k]*matrixB[k][j]; } matrixC[i][j] = sum; sum = 0; } }

printf(" Matrix c data is : "); for(i = 0; i < 2; i++) { for(j = 0 ; j < 2; j++ ) {

printf("%d\t",matrixC[i][j]);

} printf(" ");

}

///--------------------------------------------------------------"

///MATRIX C fC=fopen("mr.txt","w");///Opening matrixA File in WRITE MODE if(fC==NULL) { printf("The matrixC file not found "); return 0; } ///Writing the matrixC Data into File for(i = 0; i < 2; i++) { for(j = 0; j < 2; j++) {

fprintf(fC,"%d\t",matrixC[i][j]); } fprintf(fC," "); }

fclose(fC);

///--------------------------------------------------------------"

{ struct timeval start, end; gettimeofday(&start, NULL); sleep(3); gettimeofday(&end, NULL); long seconds = (end.tv_sec - start.tv_sec); long micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec); printf("Clock Time elpased is %ld seconds and %ld microseconds ", seconds, micros); }

{ double time_spent = 0.0; clock_t begin = clock();

sleep(3);

clock_t end = clock(); time_spent = (double) (end-begin)/CLOCKS_PER_SEC; printf("CPU Time elpased is %f seconds", time_spent); }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!