Why is my C matrix multiplication program not working properly?

Click For Summary

Discussion Overview

The discussion revolves around issues encountered in a C program intended to multiply two 3x3 matrices. Participants explore potential errors in the code and suggest corrections related to matrix initialization and loop structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the elements of the result_matrix are not initialized, which could lead to unexpected results.
  • Another participant points out that the inner loop should only iterate over the column index, rather than both row and column indices.
  • A participant shares the output of the program after modifying the inner loop, showing that the results are still incorrect despite the changes.
  • There is a suggestion to initialize the result_matrix to zero before performing the multiplication to avoid garbage values.
  • Further clarification is provided regarding the correct initialization of the result_matrix, with examples of different initialization methods.

Areas of Agreement / Disagreement

Participants generally agree on the need to initialize the result_matrix and correct the loop structure, but the specific implementation details and the resulting outputs remain a point of discussion.

Contextual Notes

There are unresolved issues regarding the exact implementation of the matrix multiplication logic and the initialization methods, as well as the impact of these changes on the final output.

James889
Messages
190
Reaction score
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
There's no initialization of the elements of result_matrix.
 
Hi James889! :smile:

And you inner loop is not right.
For starters, it should not cycle over r and c, but only over c.
 
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
 
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:
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:
 
James889 said:
:biggrin:

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

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K