Why is my C matrix multiplication program not working properly?

In summary, James889's code does not work as intended and he does not properly initialize his result matrix.
  • #1
James889
192
1
Hello,

I am trying to write a program that multiplies together two 3x3 matrices.

However my program does not really work as intended, and i get some really strange results.

Any ideas?
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {

int result_matrix[3][3];

int matrixA[3][3] = { {2,2,2},
                             {2,2,2},
                             {2,2,2} };

int matrixB[3][3] = { {4,4,4},
                            {4,4,4},
                            {4,4,4} };
        for(int rows=0; rows<3; rows++) {

                for(int columns=0; columns<3; columns++){

                for(int r=0,c=0; r<3,c<3; r++,c++){
         printf("Element at result_matrix[%d][%d] is MatrixA[%d][%d] * MatrixB[%d][%d]\n",rows,columns,r,c,c,r);
                result_matrix[rows][columns] += matrixA[r][c] * matrixB[c][r];

                }
        }
}
printf("%d\n",result_matrix[0][0]);
printf("%d\n",result_matrix[0][1]);
printf("%d\n",result_matrix[0][2]);
printf("%d\n",result_matrix[1][0]);
printf("%d\n",result_matrix[1][1]);
printf("%d\n",result_matrix[1][2]);
printf("%d\n",result_matrix[2][0]);
printf("%d\n",result_matrix[2][1]);
printf("%d\n",result_matrix[2][2]);
return 0;
}
 
Last edited:
Physics news on Phys.org
  • #2
There's no initialization of the elements of result_matrix.
 
  • #3
Hi James889! :smile:

And you inner loop is not right.
For starters, it should not cycle over r and c, but only over c.
 
  • #4
Code:
Element at result_matrix[0][0] is: 25
Element at result_matrix[0][1] is: 24
Element at result_matrix[0][2] is: 24
Element at result_matrix[1][0] is: 25
Element at result_matrix[1][1] is: 24
Element at result_matrix[1][2] is: -1077941132
Element at result_matrix[2][0] is: -1077941124
Element at result_matrix[2][1] is: 672789176
Element at result_matrix[2][2] is: -1077941296
This is after i changed the inner loop to only loop through c
 
  • #5
James889 said:
This is after i changed the inner loop to only loop through c

From the wikipedia page about http://en.wikipedia.org/wiki/Matrix_multiplication" :
a39785aa9f8b4ab1c26e97efdaa35a3d.png


Do you see the difference with your code?

Furthermore, MisterX already remarked that you need to initialize your result matrix to zero before trying to multiply the matrices.

Cheers! :smile:
 
Last edited by a moderator:
  • #6
Code:
result_matrix[3][3] = { {0} };

./run

Code:
Element at result_matrix[0][0] is: 24
Element at result_matrix[0][1] is: 24
Element at result_matrix[0][2] is: 24
Element at result_matrix[1][0] is: 24
Element at result_matrix[1][1] is: 24
Element at result_matrix[1][2] is: 24
Element at result_matrix[2][0] is: 24
Element at result_matrix[2][1] is: 24
Element at result_matrix[2][2] is: 24

:biggrin:
 
  • #7
James889 said:
:biggrin:

And yet another satisfied customer! :smile:
 
  • #8
Code:
result_matrix[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
 

1. What is C matrix multiplication?

C matrix multiplication is a mathematical operation used to multiply two matrices, which are rectangular arrays of numbers or symbols. In C programming, matrices are represented as 2D arrays, and matrix multiplication involves multiplying each element of one matrix with the corresponding element in the other matrix and adding the products together to get a new matrix.

2. How is C matrix multiplication different from regular multiplication?

C matrix multiplication is different from regular multiplication because it follows the rules of matrix algebra, which involves multiplying rows and columns instead of individual numbers. Also, the number of columns in the first matrix must be equal to the number of rows in the second matrix in order for the multiplication to be possible.

3. What are the advantages of using C matrix multiplication?

C matrix multiplication allows for efficient computation of large and complex mathematical operations, such as solving systems of linear equations, finding eigenvalues and eigenvectors, and performing transformations in computer graphics. It also simplifies the process of solving problems involving multiple variables and equations.

4. Are there any limitations to C matrix multiplication?

One limitation of C matrix multiplication is that it can only be performed on matrices with compatible dimensions. This means that the number of columns in the first matrix must be equal to the number of rows in the second matrix. Another limitation is that matrix multiplication is not commutative, meaning that the order of multiplication matters.

5. Can C matrix multiplication be applied to non-numeric matrices?

Yes, C matrix multiplication can be applied to non-numeric matrices, such as matrices containing symbolic variables or matrices representing transformations in computer graphics. However, the multiplication operation may differ depending on the type of matrix being multiplied.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
657
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
867
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
914
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
739
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top