দুটি ম্যাট্রিক্স গুণ করে ফলাফল প্রিন্ট করুন। শর্ত: প্রথম ম্যাট্রিক্সের কলাম সংখ্যা = দ্বিতীয় ম্যাট্রিক্সের সারি সংখ্যা।
উদাহরণ
#include <iostream>
using namespace std;
#define N 4
void calc_product(int mat1[][N], int mat2[][N], int res[][N]) {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
res[i][j] = 0;
for (int k = 0; k < N; k++)
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
int main() {
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}};
int res[N][N];
calc_product(mat1, mat2, res);
cout << "Resultant matrix:\n";
for (int i = 0; i < N; i++) {
for (int 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