এই প্রোগ্রামটি ফিক্সড সাইজ মাল্টি-ডাইমেনশনাল অ্যারে ব্যবহার করে ম্যাট্রিক্স গুণ করে। এটি পূর্বের ম্যাট্রিক্স মাল্টিপ্লিকেশন প্রোগ্রামের অনুরূপ, শুধু এখানে ফাংশন ব্যবহার করা হয়নি।
উদাহরণ কোড
#include <iostream>
using namespace std;
int main() {
int product[10][10];
int r1 = 2, c1 = 3, r2 = 3, c2 = 3;
int i, j, k;
int a[2][3] = {{2, 4, 1}, {2, 3, 9}};
int b[3][3] = {{1, 2, 3}, {3, 6, 1}, {2, 9, 7}};
if (c1 != r2) {
cout << "Column of first matrix should be equal to row of second matrix";
return 0;
}
cout << "The first matrix is:\n";
for (i = 0; i < r1; ++i) {
for (j = 0; j < c1; ++j)
cout << a[i][j] << " ";
cout << endl;
}
cout << "\nThe second matrix is:\n";
for (i = 0; i < r2; ++i) {
for (j = 0; j < c2; ++j)
cout << b[i][j] << " ";
cout << endl;
}
// Initialize product to 0
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
product[i][j] = 0;
// Multiplication
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
for (k = 0; k < c1; ++k)
product[i][j] += a[i][k] * b[k][j];
cout << "\nProduct of the two matrices is:\n";
for (i = 0; i < r1; ++i) {
for (j = 0; j < c2; ++j)
cout << product[i][j] << " ";
cout << endl;
}
return 0;
}
আউটপুট
The first matrix is: 2 4 1 2 3 9 The second matrix is: 1 2 3 3 6 1 2 9 7 Product of the two matrices is: 16 37 17 29 103 72
ব্যাখ্যা
কোডটি ১০০৬০৮৩১১৪ নম্বর আর্টিকেলের সাথে Поч কোন পার্থক্য নেই, শুধু ফাংশন ব্যবহার না করে মেইন-এ সবকিছু করা হয়েছে। এজন্য কোড রিইউজেবিলিটি কম। বাস্তব প্রোগ্রামে ফাংশন বা ক্লাস ব্যবহার করা উচিত।