You are here:

- Home
- Computing/Technology
- C/C++
- C++
- matrix multiplication

Advertisement

Hello

i have the following question

how can i multiply 2 matrices?

the rules are the following

assume that A and B are matrices

the result will go into matrix C

number of columns in A same as rows in B

rows in C = rows in A

columns in C = columns in B

i've been struggling a lot to write such algorithm

but never found the right solution

thanks

Hi Dear John .K

Your Problem is all about to multiply a square matrix , say number of rows equal to number of columns ,

Here im presenting a sample code to multiply two matrix a , b and the result will be stored in matrix C

#include<iostream.h>

void main()

{

int a[3][3] , b[3][3] , c[3][3];

int i , j , k;

cout<<"Enter Matrix A";

for( i = 0 ; i < 3 ; i++)

for( j = 0 ; j < 3 ; j++)

cin>>a[i][j];

cout<<"Enter Matrix B";

for( i = 0 ; i < 3 ; i++)

for( j = 0 ; j < 3 ; j++)

cin>>b[i][j];

for( i = 0 ; i < 3 ; i++)

for( j = 0 ; j < 3 ; j++)

{

c[i][j] = 0;

for( k = 0 ;k < 3 ; k++)

c[i][j] += a[i][k]*b[k][j];

}

cout<<"The resultant matrix is ";

for( i = 0 ; i < 3 ; i++)

{

for( j = 0 ; j < 3 ; j++)

cout<<a[i][j]<<" ";

cout<<endl;

}

}

Note : I have written this code for a 3x3 matrix with slight modifications you can extend this programme to perform multiplication over a m x n matrix

I havn't test this code , if you found any issues with this programme please do revert me with !

Thanks and Regards !

Prince M. Premnath

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

More than 5 years**Education/Credentials**

MCA