Hi !
I tried to compile this program on red hat 9.0 but I keep getting segementation fault when I run the output. The program multiplies two matricies and is very simple one :
Code:#include<stdio.h> #include<stdlib.h> int main() { int **A, **B, **C, row1, row2, col1, col2, sum, i, j, k; printf("Enter the rows and columns of matrices:\n"); printf("Matrix one row and columns\n"); scanf("%d %d", &row1, &col1); printf("Matrix two row and columns\n"); scanf("%d %d", &row2, &col2); if(col1 != row2) { printf("Matrices cannot be multiplied\n"); exit(1); } A = malloc(row1*sizeof(int*)); B = malloc(row2*sizeof(int*)); C = malloc(row1*sizeof(int*)); for(i=0; i<row1; i++) A[i] = malloc(col1*sizeof(int)); for(i=0; i<row2; i++) B[i] = malloc(col2*sizeof(int)); for(i=0; i<row1; i++) C[i] = malloc(col2*sizeof(int)); printf("Enter elements for first matrix row by row\n"); for(i=0; i<row1; i++) for(j=0; j<col1; j++) scanf("%d", &A[i][j]); printf("Enter elements for second matrix row by row\n"); for(i=0; i<row2; i++) for(j=0; j<col2; j++) /*---------MULTIPLICATION------------------------- */ for(i=0; i<row1; i++) { for(j=0; j<col2; j++) { sum = 0; for(k=0; k<col1; k++) sum += A[i*k][j]*B[i][j*k]; C[i][j] = sum; } } for(i=0; i<row1; i++) { for(j=0; j<col2; j++) printf("%d ", C[i][j]); printf("\n"); } return 0; }



LinkBack URL
About LinkBacks



