# segmentation fault : matrix multiplication ??

This is a discussion on segmentation fault : matrix multiplication ?? within the C Programming forums, part of the General Programming Boards category; Hi ! I tried to compile this program on red hat 9.0 but I keep getting segementation fault when I ...

1. ## segmentation fault : matrix multiplication ??

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;

}```

2. Code:
```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++)```
Shouldn't you be actually scanning something in there?

Quzah.

3. inserted the line " scanf("%d", &B[i][j]); "

Still there is a segmentation fault ??????

4. Repost your updated code. And how about squeezing a lot of that whitespace out so it takes up less space?

5. Here is the updated code:

Code:
```#include<stdio.h>
#include<stdlib.h>

int main()
{
int **A, **B, **C, row1, row2, col1, col2, sum;
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(int 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(int 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(int j=0; j<col2; j++)
scanf("%d", &B[i][j]);
//-------------------------------- MULTIPLICATION ------------------------------------------------------

for(i=0; i<row1; i++)
{
for(int j=0; j<col2; j++)
{
sum = 0;
for(int 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(int j=0; j<col2; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}```

6. well, it seems to work fine for me, and i am using Dev-c++ compiler. i have made some changes as well

Code:
```#include<stdio.h>
#include<stdlib.h>

int main()
{
int **A, **B, **C, row1, row2, col1, col2, sum;
int 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++)
scanf("%d", &B[i][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");
}

fflush(stdin);
getchar();
return 0;
}

/*my ouput
Enter the rows and columns of matrices:
Matrix one row and columns
2
2
Matrix two row and columns
2
2
Enter elements for first matrix row by row
2
2
2
2
Enter elements for second matrix row by row
2
2
2
2
8 8
8 8
*/```
have declared all the varibales which are used in the loop at the beginning of the main,

what compiler and OS are using ??

s.s.harish

7. > sum += A[i*k][j]*B[i][j*k];
Isn't i*k going to result in some very large values - much larger than the values you've allocated for?

8. Code:
```	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;
}
}```

9. Now that's spooky Dave

10. sorry for my dumbness but I still didn't understand what is going on ?

This is following an algorithm in book and it seems to work fine on dev C++ but gives me segementation fault in red hat gcc compiler

11. my sincere apologies for my dumbness and the stupidity of the text , I researched the algorithm and found that it works only for square matricies, hence both matricies should have equal number of rows and colums .

The program works fine it u enter a 2 X 2 matrix, but surprisingly when i enter a 3 X 3 matrix it gives a crazy result with the last row or element messed up and a 4 X 4 matrix gives a segmentation fault ?????

I ran these on gcc compiler in red hat 9.0.

12. > sorry for my dumbness but I still didn't understand what is going on ?
Which bit?
The "why it crashes on one machine and not another"?
Because programs are not obliged to crash at the first moment you make a mistake. Life would be a lot simpler if it did, because then you'd get rid of all those bugs.

```			for( k=0; k<col1; k++) {