C matrix multiplication


by James889
Tags: matrix, multiplication
James889
James889 is offline
#1
Jun10-11, 06:12 PM
P: 184
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?
#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;
}
Phys.Org News Partner Science news on Phys.org
NASA's space station Robonaut finally getting legs
Free the seed: OSSI nurtures growing plants without patent barriers
Going nuts? Turkey looks to pistachios to heat new eco-city
MisterX
MisterX is online now
#2
Jun10-11, 06:27 PM
P: 536
There's no initialization of the elements of result_matrix.
I like Serena
I like Serena is offline
#3
Jun10-11, 06:29 PM
HW Helper
I like Serena's Avatar
P: 6,189
Hi James889!

And you inner loop is not right.
For starters, it should not cycle over r and c, but only over c.

James889
James889 is offline
#4
Jun10-11, 07:23 PM
P: 184

C matrix multiplication


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
I like Serena
I like Serena is offline
#5
Jun10-11, 08:51 PM
HW Helper
I like Serena's Avatar
P: 6,189
Quote Quote by James889 View Post
This is after i changed the inner loop to only loop through c
From the wikipedia page about Matrix multiplication:


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!
James889
James889 is offline
#6
Jun11-11, 03:04 AM
P: 184
result_matrix[3][3] = { {0} };
./run

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
I like Serena
I like Serena is offline
#7
Jun11-11, 03:26 AM
HW Helper
I like Serena's Avatar
P: 6,189
Quote Quote by James889 View Post
And yet another satisfied customer!
MisterX
MisterX is online now
#8
Jun11-11, 12:27 PM
P: 536
result_matrix[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };


Register to reply

Related Discussions
Matrix Multiplication and Algebraic Properties of Matrix Operations Calculus & Beyond Homework 2
Matrix multiplication Precalculus Mathematics Homework 2
Matrix multiplication Calculus & Beyond Homework 0
matrix multiplication Linear & Abstract Algebra 1
matrix multiplication Linear & Abstract Algebra 1