furiosa
furiosa

Reputation: 49

How do I fix this problem with multiplying two matrices?

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.

Upvotes: 2

Views: 693

Answers (3)

dreamcrash
dreamcrash

Reputation: 51443

   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];
                    }
                } 
            }
        }

Your multiplication code reads the values from the matrices C, A and B. Matrices A and B are read from file, however you forgot to initialize the matrix C with zeros (i.e., int C[5][5] = {0};). Otherwise, matrix C will contain random values. And those random values will be added to the matrices A and B during the operation C[i][j] = C[i][j] + A[i][k] * B[k][j];.

Moreover, change columnsB + 1 to columnsB, in for(int j = 0; j < columnsB + 1; j++). Otherwise, you are going out of the boundaries of the matrix C and B, marked in the following code with "^":

 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];
                      ^^^                 ^^^

Upvotes: 2

AndersK
AndersK

Reputation: 36082

I think the root cause is this line

for(int j = 0; j < columnsB + 1; j++){

The +1 makes you go outside the 5x5 matrix

Also you really need to initialize the C matrix to 0.

Upvotes: 1

Amandeep
Amandeep

Reputation: 41

I think you should do this,

Initialize the c[5][5] with 0.

Upvotes: 1

Related Questions