Sunday, February 1, 2015
Matrix multiplication in C
#include<stdio.h>
void readA(float A[10][10])
{
int i,j;
FILE *inputMatrixA=fopen("matrixA.txt","r");
for(i=0;i<3;++i){
for(j=0;j<3;++j){
fscanf(inputMatrixA,"%f",&A[i][j]);
}
}
fclose(inputMatrixA);
}
void readB(float B[10][10])
{
int i,j;
FILE *inputMatrixB=fopen("matrixB.txt","r");
for(i=0;i<3;++i){
for(j=0;j<3;++j){
fscanf(inputMatrixB,"%f",&B[i][j]);
}
}
fclose(inputMatrixB);
}
void mult(float A[10][10],float B[10][10],float C[10][10])
{
int i,j,k;
float sum=0.0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=0.0;
for(i=0;i<3;i++){ //row of first matrix
for(j=0;j<3;j++){ //column of second matrix
sum=0.0;
for(k=0;k<3;k++) //column of first matrix
sum=sum+A[i][k]*B[k][j];
C[i][j]=sum;
}
}
}
void print(float D[10][10])
{
int i,j;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
printf("%f ",D[i][j]);
}
printf("\n");
}
}
main()
{
float A[10][10],B[10][10],C[10][10];
int i,j;
readA(A);
readB(B);
mult(A,B,C);
print(C);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment