/* Scott Johnson matrix store and recall (file) using functions, aditional function for matrix addition 08/04/2007 */ #include struct matrix{ int cols; int rows; float arr[16][16]; }; void getMatrix(struct matrix *m); void showMatrix(struct matrix *m); int saveMatrix(struct matrix *m, char *n); int loadMatrix(struct matrix *m, char *n); int sum(struct matrix *m1,struct matrix *m2,struct matrix *m3); int product(struct matrix *m1,struct matrix *m2,struct matrix *m3); main(){ struct matrix m1,m2,m3; int i; /* read first matrix. save in mat1.txt */ printf("matrix 1\n"); getMatrix(&m1); i=saveMatrix(&m1,"mat1.txt"); /* read second matrix. save in mat2.txt */ printf("matrix 2\n"); getMatrix(&m1); i=saveMatrix(&m1,"mat2.txt"); /* load mat1.txt into m1, mat2.txt into m2 */ i=loadMatrix(&m1,"mat1.txt"); i=loadMatrix(&m2,"mat2.txt"); /* add m1 to m2, result in m3. display result if sizes compatible otherwise give error message */ i=sum(&m1,&m2,&m3); if(i!=0){ printf("Matrix sizes incompatible\n"); }else{ printf("sum\n"); showMatrix(&m3); } i=product(&m1,&m2,&m3); if(i!=0){ printf("Matrix sizes incompatible\n"); }else{ printf("product\n"); showMatrix(&m3); } i=product(&m2,&m1,&m3); if(i!=0) printf("Matrix sizes incompatible\n"); else showMatrix(&m3); } void getMatrix(struct matrix *m){ int row,i; printf("enter the number of columns "); scanf("%d",&(*m).cols); printf("enter the number of rows "); scanf("%d",&(*m).rows); printf("number of columns %d\n",(*m).cols); printf("number of rows %d\n",(*m).rows); for(row = 0; row<(*m).rows;row++){ printf("enter %d values of row %d ",(*m).cols,row+1); for(i = 0; i<(*m).cols;i++){ scanf("%f",&(*m).arr[i][row]); } } } void showMatrix(struct matrix *m){ int row,i; for(row = 0; row<(*m).rows;row++){ for(i = 0; i<(*m).cols;i++){ printf("%f ",(*m).arr[i][row]); } printf("\n"); } } int saveMatrix(struct matrix *m, char *n){ int row,i; FILE *store; store = fopen(n,"w"); fprintf(store,"%d\n",(*m).cols); fprintf(store,"%d\n",(*m).rows); for(row = 0; row<(*m).rows;row++){ for(i = 0; i<(*m).cols;i++){ fprintf(store,"%f ",(*m).arr[i][row]); } fprintf(store,"\n"); } fclose(store); return 0; } int loadMatrix(struct matrix *m, char *n){ int row,i; FILE *store; store = fopen(n,"r"); if (store == (NULL)) { return -1; }else{ fscanf(store,"%d\n",&(*m).cols); fscanf(store,"%d\n",&(*m).rows); for(row = 0; row<(*m).rows;row++){ for(i = 0; i<(*m).cols;i++){ fscanf(store,"%f ",&(*m).arr[i][row]); } fscanf(store,"\n"); } } fclose(store); return 0; } int sum(struct matrix *m1,struct matrix *m2,struct matrix *m3){ int i,row; if ((*m1).rows != (*m2).rows || (*m1).cols != (*m2).cols){ return -1; } else { (*m3).cols = (*m1).cols; (*m3).rows = (*m1).rows; for(row = 0; row<(*m1).rows;row++){ for(i = 0; i<(*m1).cols;i++){ (*m3).arr[i][row] = ((*m1).arr[i][row]+(*m2).arr[i][row]); } } return 0; } } int product(struct matrix *m1,struct matrix *m2,struct matrix *m3){ int i,row,col; float temp=0; if((*m1).cols != (*m2).rows){ return -1; } else { (*m3).cols = (*m2).cols; (*m3).rows = (*m1).rows; for(col=0, col<(*m2).cols, col++){ for(row=0,row<(*m1).rows,row++){ for(i=0, i<(*m1).cols, i++) { temp+=(*m1).arr[i][row]*(*m2).arr[cols][i]; } (*m3).arr[row][col] = temp; } } return 0; } }