943,911 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 832
  • C RSS
Dec 4th, 2008
0

Matrix Multiplication (sorry)

Expand Post »
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:

  1. #include <stdio.h>
  2.  
  3. void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
  4. int main(int argc, char *argv[]){
  5. FILE *fin = fopen(argv[1], "r");
  6. int i, j, n = fgetc(fin), x[n][n], y[n][n], z[n][n];
  7.  
  8. for(i = 0; i < n; i++)
  9. for(j = 0; j < n; j++){
  10. x[i][j] = fgetc(fin);
  11. printf("%d", x[i][j]);
  12. }
  13. for(i = 0; i < n; i++)
  14. for(j = 0; j < n; j++) y[i][j] = fgetc(fin);
  15. for(i = 0; i < n; i++)
  16. for(j = 0; j < n; j++) z[i][j] = 0;
  17.  
  18. MatrixMult( x, y, z, &n );
  19.  
  20. for(i = 0; i < n; i++){
  21. for(j = 0; j < n; j++) printf(" %d", z[i][j]);
  22. printf("\n");
  23. }
  24. return(0);
  25. }
  26. void MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
  27. int i, j, k;
  28. for(i = 0; i < *n; i++)
  29. for(j = 0; j < *n; j++)
  30. for(k = 0; k < *n; k++)
  31. z[i][j] += x[i][k] * y[k][j];
  32.  
  33. return;
  34. }
Similar Threads
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Dec 4th, 2008
0

Re: Matrix Multiplication (sorry)

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:
  1. void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
And get away with this: (As far as i know)
  1. int x[n][n], y[n][n], z[n][n];
Last edited by devnar; Dec 4th, 2008 at 2:21 pm.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 4th, 2008
0

Re: Matrix Multiplication (sorry)

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.

  1. #include <stdio.h>
  2.  
  3. int MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
  4. int main(int argc, char *argv[]){
  5. FILE *fin = fopen(argv[1], "r");
  6. int i, j, n, a[n][n], b[n][n], c[n][n];
  7.  
  8. fscanf( fin, "%d", &n );
  9.  
  10. for(i = 0; i < n; i++)
  11. for(j = 0; j < n; j++){
  12. fscanf( fin, "%d", &a[i][j] );
  13. printf("%d", a[i][j]);
  14. }
  15. for(i = 0; i < n; i++)
  16. for(j = 0; j < n; j++) fscanf(fin, "%d", &b[i][j] );
  17. for(i = 0; i < n; i++)
  18. for(j = 0; j < n; j++) c[i][j] = 0;
  19.  
  20. for(i = 0; i < n; i++){
  21. for(j = 0; j < n; j++) printf(" %d", MatrixMult( a, b, c, &n ));
  22. printf("\n");
  23. }
  24. return(0);
  25. }
  26.  
  27. int MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
  28. int i, j, k;
  29. for(i = 0; i < *n; i++)
  30. for(j = 0; j < *n; j++)
  31. for(k = 0; k < *n; k++)
  32. z[i][j] += x[i][k] * y[k][j];
  33.  
  34. return( z );
  35. }
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Dec 4th, 2008
0

Re: Matrix Multiplication (sorry)

This is wrong:
  1. for(i = 0; i < n; i++){
  2. for(j = 0; j < n; j++) printf(" %d", MatrixMult( a, b, c, &n ));
  3. printf("\n");
  4. }

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:

  1. int a[n][n], b[n][n], c[n][n];
It should be:
  1. int i, j, n, a[10][50], b[10][50], c[10][50]; //or something similar
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 4th, 2008
0

Re: Matrix Multiplication (sorry)

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

heres the final version

  1. #include <stdio.h>
  2.  
  3. void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
  4. int main(int argc, char *argv[]){
  5. FILE *fin = fopen(argv[1], "r");
  6. int i, j, n, a[50][50], b[50][50], c[50][50];
  7.  
  8. fscanf( fin, "%d", &n );
  9.  
  10. printf("\n\nFirst Matrix:\n");
  11.  
  12. for(i = 0; i < n; i++){
  13.  
  14. for(j = 0; j < n; j++){
  15.  
  16. fscanf( fin, "%d", &a[i][j] );
  17. printf( " %d", a[i][j] );
  18. }
  19.  
  20. printf("\n");
  21. }
  22.  
  23. printf("\n\nSecond Matrix:\n");
  24. for(i = 0; i < n; i++){
  25.  
  26. for(j = 0; j < n; j++){
  27.  
  28. fscanf( fin, "%d", &b[i][j] );
  29. printf( " %d", b[i][j] );
  30.  
  31. c[i][j] = 0;
  32. }
  33.  
  34. printf("\n");
  35. }
  36.  
  37. MatrixMult( a, b, c, &n );
  38.  
  39. printf("\n\nProduct of the two Matrices:\n");
  40.  
  41. for(i = 0; i < n; i++){
  42.  
  43. for(j = 0; j < n; j++) printf( " %d", c[i][j] );
  44. printf("\n");
  45.  
  46. }
  47. return(0);
  48. }
  49. void MatrixMult(int x[][50], int y[][50], int z[][50], int *n ){
  50. int i, j, k;
  51.  
  52. for(i = 0; i < *n; i++)
  53.  
  54. for(j = 0; j < *n; j++)
  55.  
  56. for(k = 0; k < *n; k++)
  57.  
  58. z[i][j] += x[i][k] * y[k][j];
  59. }
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with C and MySql
Next Thread in C Forum Timeline: Isn't it wrong? char **argv





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC