I know there are multiple threads on Matrix Multiplication but all of them are either different from what i need, or are unanswered. I am trying to multiply two N x N matrices. The two matrices and N are all found in a file via argv[1].

I have spent hours and hours trying to get my code to compile and it finally does. then i ran the program and much to my dissapointment it just output hundreds and hundreds of numbers.

heres my code:

``````#include <stdio.h>

void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
int main(int argc, char *argv[]){
FILE *fin = fopen(argv[1], "r");
int i, j, n = fgetc(fin), x[n][n], y[n][n], z[n][n];

for(i = 0; i < n; i++)
for(j = 0; j < n; j++){
x[i][j] = fgetc(fin);
printf("%d", x[i][j]);
}
for(i = 0; i < n; i++)
for(j = 0; j < n; j++) y[i][j] = fgetc(fin);
for(i = 0; i < n; i++)
for(j = 0; j < n; j++) z[i][j] = 0;

MatrixMult( x, y, z, &n );

for(i = 0; i < n; i++){
for(j = 0; j < n; j++) printf(" %d", z[i][j]);
printf("\n");
}
return(0);
}
void MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
int i, j, k;
for(i = 0; i < *n; i++)
for(j = 0; j < *n; j++)
for(k = 0; k < *n; k++)
z[i][j] += x[i][k] * y[k][j];

return;
}``````
2
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by Dewey1040

Better use fscanf() to read integers. If you use fgetc(), you'll be reading the ascii values of the integers and not their actual values. Also every digit will be treated as a character. How will you read multiple-digit numbers then?

Also you've used `n = fgetc(fin)` . Is that what you want? Does the first digit in your file denote the order of the matrix?

You can't treat this as a declaration:

``void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);``

And get away with this: (As far as i know)

``int  x[n][n], y[n][n], z[n][n];``

this is rushed code i have to go take a psych test, thanks for the help so far, how do i return the value of z from the matrixmult function? or how do u think i should do that? thanks for all the help devnar.

``````#include <stdio.h>

int MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
int main(int argc, char *argv[]){
FILE *fin = fopen(argv[1], "r");
int i, j, n, a[n][n], b[n][n], c[n][n];

fscanf( fin, "%d", &n );

for(i = 0; i < n; i++)
for(j = 0; j < n; j++){
fscanf( fin, "%d", &a[i][j] );
printf("%d", a[i][j]);
}
for(i = 0; i < n; i++)
for(j = 0; j < n; j++) fscanf(fin, "%d", &b[i][j] );
for(i = 0; i < n; i++)
for(j = 0; j < n; j++) c[i][j] = 0;

for(i = 0; i < n; i++){
for(j = 0; j < n; j++) printf(" %d", MatrixMult( a, b, c, &n ));
printf("\n");
}
return(0);
}

int MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
int i, j, k;
for(i = 0; i < *n; i++)
for(j = 0; j < *n; j++)
for(k = 0; k < *n; k++)
z[i][j] += x[i][k] * y[k][j];

return( z );
}``````

This is wrong:

``````for(i = 0; i < n; i++){
for(j = 0; j < n; j++) printf(" %d", MatrixMult( a, b, c, &n ));
printf("\n");
}``````

You can't return arrays since they are always passed by reference. Your previous code for displaying the array was correct.

And I think this is still wrong:

``int a[n][n], b[n][n], c[n][n];``

It should be:

``int i, j, n, a[10][50], b[10][50], c[10][50]; //or something similar``

thanks a lot i got it to work, i deleted some unnecessary stuff rearranged some lines to make more sense.

heres the final version

``````#include <stdio.h>

void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
int main(int argc, char *argv[]){
FILE *fin = fopen(argv[1], "r");
int i, j, n, a[50][50], b[50][50], c[50][50];

fscanf( fin, "%d", &n );

printf("\n\nFirst Matrix:\n");

for(i = 0; i < n; i++){

for(j = 0; j < n; j++){

fscanf( fin, "%d", &a[i][j] );
printf( " %d", a[i][j] );
}

printf("\n");
}

printf("\n\nSecond Matrix:\n");
for(i = 0; i < n; i++){

for(j = 0; j < n; j++){

fscanf( fin, "%d", &b[i][j] );
printf( " %d", b[i][j] );

c[i][j] = 0;
}

printf("\n");
}

MatrixMult( a, b, c, &n );

printf("\n\nProduct of the two Matrices:\n");

for(i = 0; i < n; i++){

for(j = 0; j < n; j++) printf( " %d", c[i][j] );
printf("\n");

}
return(0);
}
void MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
int i, j, k;

for(i = 0; i < *n; i++)

for(j = 0; j < *n; j++)

for(k = 0; k < *n; k++)

z[i][j] += x[i][k] * y[k][j];
}``````