| | |
Matrix Multiplication (sorry)
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 3
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:
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:
C Syntax (Toggle Plain Text)
#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; }
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
You can't treat this as a declaration:
And get away with this: (As far as i know)
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:
C Syntax (Toggle Plain Text)
void MatrixMult(int x[][50], int y[][50], int z[][50], int *n);
C Syntax (Toggle Plain Text)
int x[n][n], y[n][n], z[n][n];
Last edited by devnar; Dec 4th, 2008 at 1:21 pm.
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 3
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.
C Syntax (Toggle Plain Text)
#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:
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:
It should be:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
int a[n][n], b[n][n], c[n][n];
c Syntax (Toggle Plain Text)
int i, j, n, a[10][50], b[10][50], c[10][50]; //or something similar
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 3
thanks a lot i got it to work, i deleted some unnecessary stuff rearranged some lines to make more sense.
heres the final version
heres the final version
C Syntax (Toggle Plain Text)
#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]; }
![]() |
Similar Threads
- C++ Matrix Multiplication (C++)
- need help (C)
- matrix multiplication help.. (C)
- Multyplying one dimensional dynamic arrays(matrix) (C++)
- Method that recieves matrix values as a string (C#)
- Transpose & Inverse Matrix, Matrix Determinant (C++)
- Program on matrix calculations (C)
- Gabor filter (C++)
- creating a ellispse at any aspect angle (C)
- Backp/Restore HardDisk Image (C++)
Other Threads in the C Forum
- Previous Thread: Help with C and MySql
- Next Thread: Isn't it wrong? char **argv
Views: 639 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch c++ char character code coke command conversion convert createcopyoffile csyntax database decimal dice directory directorystructure do-not-use drawing dude dumpandrun dynamic error evaluation exec factorial fflush(stdout) fgets file fork free function functions givemetehcode givemetehcodez grade graphics gray hangman highest histogram homework i/o initialization input insert int integer kernel lazy line linked linkedlist linux list lists loop malloc matrix memory mysql no-effort output path pointer pointers precedence problem process profile program programming read recursion recursive recv research reverse scanf socket socketprograming spoonfeeding string strings strtok structures student swap system test turbo-c unix user variable visual windows winxp





