#define n 50
void matread(char *, double [][n], int *, int *);
void matprint(double [][n],int ,int, char);
int matmul(int, int,int,int,int *, int *, double [][n],double [n][n], double [][n]);
void matprintf(double [][n], int, int, char *);
int main(){
int ra,rb,rc,ca,cb,cc,flag;
double a[n][n],b[n][n],c[n][n];
FILE *fp;
fp=fopen("matA.dat","w");
fprintf(fp,"%d %d\n%.2lf %.2lf %.2lf\n%.2lf %.2lf %.2lf\n",2,3,
1.0, 0.0, 4.0, -2.0, 4.0, 1.0);
fclose(fp);
fp=fopen("matB.dat","w");
fprintf(fp,"%d %d\n%.2lf %.2lf %.2lf %.2lf \n%.2lf %.2lf %.2lf %.2lf \n%.2lf %.2lf %.2lf %.2lf\n",3,4,2.0,0.0,5.0, 1.0,4.0,0.0,-6.0,1.0,5.0,1.0,2.0,1.0);
fclose(fp);
matread("matA.dat",a,&ra,&ca);
matprint(a,ra,ca,'A');
matread("matB.dat",b,&rb,&cb);
matprint(b,rb,cb,'B');
flag=matmul(ra,ca,rb,cb,&rc,&cc,a,b,c);
if(flag==0){
printf("\nMatrices A and B are incompatiable for marix multiplication");
}
else{
matprint(c,rc,cc,'C');
matprintf(c,rc,cc,"matC.dat"); \\PROBLEM OCCURS WHEN THIS IS EXECUTED
}
return 0;
}
void matprintf(double c[][n], int row, int col, char *file){
FILE *fp;
int i,j;
fp=fopen(file,"w");
printf(fp,"%d %d\n", row, col);
for(i=0;i<row;++i){
for(j=0;j<col;++j){
fprintf(fp,"%lf ", c[i][j]);
}
printf("\n");
}
}
int matmul(int ra, int ca,int rb,int cb,int *row, int *col,
double a[][n], double b[n][n], double c[][n]){
int i,j,k;
if(ca!=rb) return 0;
else {
*row=ra;
*col=cb;
for(i=0;i<*row;++i){
for(j=0;j<*col;++j){
c[i][j]=0;
for(k=0; k<cb; k++)
c[i][j]+= (a[i][k])*(b[k][j]);
}
}
}
return 1;
}
void matread(char *file, double a[][n], int *row, int *col){
FILE *fp;
int p,q,i,j;
fp=fopen(file,"r");
assert(fp!=NULL);
fscanf(fp,"%d %d", &p, &q);
*row=p;
*col=q;
for(i=0; i<*row; ++i){
for(j=0;j<*col; ++j){
fscanf(fp,"%lf",&a[i][j]);
}
}
}
void matprint(double a[][n],int row,int col, char c){
int i,j;
printf("Matrix %c is\n",c);
for(i=0; i<row; ++i){
for(j=0;j<col; ++j){
printf("%.1lf ", a[i][j]);
}
printf("\n");
}
}
This code is supposed to first extract matrices A and B from their respective data files matA.dat and matB.dat. These data files contain matrix size in first line, and matrix entries subsequently.
This is done using matread();(working) Then we print both these matrices using matprint(); (working)
Then it is supposed to check if they are compatible for multiplication(only checks for AB).
if not compatible, matmul() returns 0, if compatible matmul() computes AB, stores in C and returns 1; (working)
also if compatible, we're supposed to now print the matrix C into the data file matC.dat
Now this is where I am lost.
The question explicitly wants me to use this: matprintf(c,rc,cc,"matC.dat"); and then define matprintf() myself.
So my problem is that, first of all matC.dat is non existent at this point of time in main(), so how am I supposed to pass it as a file? Am I supposed to pass it as a string?
please help me.
Currently with this code I get the error:
matrix product.c:53:9: warning: incompatible pointer types passing 'FILE *' (aka 'struct __sFILE *') to parameter of type 'const char *' [-Wincompatible-pointer-types] printf(fp,"%d %d\n", row, col); ^~ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h:170:36: note: passing argument to parameter here int printf(const char * __restrict, ...) __printflike(1, 2); ^ 1 warning generated.
matprintf
anyway (since all it has is the filename), so best not to open it inmain
as well. – Scott Hunter