I am trying to multiply two matrices, the two matrices are read rom a text file, which is why the values of the matrices are not initialized. This is the code:
int main() {
...
else if (multiplication == true){
if (columnsA == rowsB && rowsA == columnsB){
for(int i = 0; i < rowsA; i++){
for(int j = 0; j < columnsB + 1; j++){
for(int k = 0; k < columnsA; k++){
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
}
else{
printf("\nError: The number of columns in Matrix A must be equal to the number of rows in Matrix B for multiplication");
return 1;
}
}
...
return 0;
}
A is the first matrix, B is the second matrix and C is the output. rowsA is the number of rows in matrix A and columns B is the number of columns in matrixB.
The code works for the first numbers in the ouput matrix, but the last number always ends up being a very long random number.
For example the input will be a 2x2 matrix with the following values:
Row 1: 3 1
Row 2: 5 2
and another 2x2 matrix with the following values
row 1: 4 1
row 2: 2 6
The output comes out as
row 1: 14 9
row 2: 24 -374793898(or some other long, random number)
The first three number are correct (I checked) but the last one is never right.