In: Computer Science
Write a C++ program to multiply two matrices a and b and print the result. Use two-dimensional arrays to represent the matrices.
C++ Code:
#include <iostream>
using namespace std;
#define N 4
void multiply(int m1[][N],int m2[][N],int m3[][N])
{
int i, j, k;
for (i=0;i<N;i++) {
for (j=0;j<N;j++) {
m3[i][j]=0;
for(k=0;k<N;k++)
m3[i][j]+=m2[i][k]*m2[k][j];
}
}
}
int main()
{
int i, j;
int m3[N][N]; // To store result
int m1[N][N]={{ 1,2,3,4 },{ 4,3,2,1
},{1,2,3,4},{4,3,2,1 } };
int m2[N][N]={ { 1,2,3,4
},{1,2,3,4},{1,2,3,4},{1,2,3,4} };
multiply(m1, m2, m3);
cout << "Result after multiplication of
matrix is \n";
for (i=0;i<N;i++) {
for (j=0;j<N;j++)
cout<<m3[i][j]<< " ";
cout << "\n";
}
return 0;
}
if you like the answer please provide a thumbs up