2D Array in C Write a C program which reads a 4x4 matrices f
2D Array in C
Write a C program which reads a 4x4 matrices from a file and multiplies them.
Function prototype:
Void readMatrices(FILE *fp, int array1[4][4], int array2[4][4]);
Void printMatrices (int array1[4][4], int array2[4][4]);
Void multiply Matrices(int array1[4][4], int array2[4][4], int result[4][4]);
The file will contain the values in the following format:
3 1 2 3 5 6 2 8 7 6 2 8 6 3 6 2 4 6 3 5 7 5 3 6 4 7 8 6 4 3 6 6
0
Solution
#include <stdio.h>
 #include <stdlib.h>
 #include <malloc.h>
 void readMatrices(FILE *file, int array1[4][4], int array2[4][4])
 {
 int i,j;
 for(i = 0; i < 4; i++)
 {
 for(j = 0; j < 4; j++)
 {
 //Use lf format specifier, %c is for character
 fscanf(file, \"%d\", &array1[i][j]);
 
 // mat[i][j] -= \'0\';
 //printf(\"%d\ \",mat[i][j]); //Use lf format specifier, \  is for new line
 }
}
for(i = 0; i < 4; i++)
 {
 for(j = 0; j < 4; j++)
 {
 //Use lf format specifier, %c is for character
 fscanf(file, \"%d\", &array2[i][j]);
 
 // mat[i][j] -= \'0\';
 //printf(\"%d\ \",mat[i][j]); //Use lf format specifier, \  is for new line
 }
}
}
void printMatrices (int array1[4][4], int array2[4][4])
 {
 int i,j;
 printf(\"Matrix 1 :\ \");
 printf(\"\ \");
 for(i = 0; i < 4; i++)
 {
 for(j = 0; j < 4; j++)
 {
printf(\"%d\\t\",array1[i][j]);
 
   
 }
 printf(\"\ \");
 }
printf(\"Matrix 2 :\ \");
 printf(\"\ \");
for(i = 0; i < 4; i++)
 {
 for(j = 0; j < 4; j++)
 {
   
 printf(\"%d\\t\",array2[i][j]);
 
 }
 printf(\"\ \");
}
}
void multiply_Matrices(int array1[4][4], int array2[4][4], int result[4][4])
 {
 int i,j,k,sum=0;;
 for(i=0;i<4;i++)
 { //row of first matrix
 for(j=0;j<4;j++)
 { //column of second matrix
 sum=0;
 for(k=0;k<4;k++)
 sum=sum+array1[i][k]*array2[k][j];
 result[i][j]=sum;
 }
}
 }
int main()
 {
 int array1[4][4],array2[4][4],result[4][4];
FILE *file;
 file=fopen(\"input.txt\", \"r\");
readMatrices(file,array1,array2);
 printMatrices(array1,array2);
 multiply_Matrices(array1,array2,result);
printf(\"Result is \ \");
int i,j;
 for(i = 0; i < 4; i++)
 {
 for(j = 0; j < 4; j++)
 {
   
 printf(\"%d\\t\",result[i][j]);
 
 }
 printf(\"\ \");
}
fclose(file);
 return 0;
 }
======================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ gcc matrix.c
 akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
 Matrix 1 :
3   1   2   3  
 5   6   2   8  
 7   6   2   8  
 6   3   6   2  
 Matrix 2 :
4   6   3   5  
 7   5   3   6  
 4   7   8   6  
 4   3   6   6  
 Result is
 39   46   46   51  
 102   98   97   121  
 110   110   103   131  
 77   99   87   96  
![2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in 2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in](/WebImages/23/2d-array-in-c-write-a-c-program-which-reads-a-4x4-matrices-f-1056829-1761551485-0.webp)
![2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in 2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in](/WebImages/23/2d-array-in-c-write-a-c-program-which-reads-a-4x4-matrices-f-1056829-1761551485-1.webp)
![2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in 2D Array in C Write a C program which reads a 4x4 matrices from a file and multiplies them. Function prototype: Void readMatrices(FILE *fp, int array1[4][4], in](/WebImages/23/2d-array-in-c-write-a-c-program-which-reads-a-4x4-matrices-f-1056829-1761551485-2.webp)
