इस ट्यूटोरियल में, हम दो आव्यूहों को गुणा करने के कार्यक्रम पर चर्चा करेंगे।
इसके लिए हमें दो मैट्रिक्स दिए जाएंगे और हमारा काम उन दो मैट्रिक्स के उत्पाद को प्रिंट करना है। एकमात्र शर्त यह है कि पहले मैट्रिक्स के कॉलम की संख्या दूसरे मैट्रिक्स की पंक्तियों की संख्या के बराबर होनी चाहिए।
उदाहरण
#include <iostream>
using namespace std;
#define N 4
//multiplying the elements of both matrices
void calc_product(int mat1[][N], int mat2[][N], int res[][N]){
int i, j, k;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++){
res[i][j] = 0;
for (k = 0; k < N; k++)
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
int main(){
int i, j;
int res[N][N];
int mat1[N][N] = {{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int mat2[N][N] = {{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
calc_product(mat1, mat2, res);
cout << "Resultant matrix :\n";
for (i = 0; i < N; i++){
for (j = 0; j < N; j++)
cout << res[i][j] << " ";
cout << "\n";
}
return 0;
} आउटपुट
Resultant matrix : 10 10 10 10 20 20 20 20 30 30 30 30 40 40 40 40