Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ เพื่อคูณเมทริกซ์สองตัวโดยส่งเมทริกซ์ไปยังฟังก์ชัน


เมทริกซ์คืออาร์เรย์ของตัวเลขสี่เหลี่ยมที่จัดเรียงในรูปแบบของแถวและคอลัมน์

ตัวอย่างของเมทริกซ์มีดังนี้

เมทริกซ์ 3*4 มี 3 แถว 4 คอลัมน์ดังแสดงด้านล่าง

8 6 3 5
7 1 9 2
5 1 9 8

โปรแกรมที่คูณเมทริกซ์สองตัวด้วยการส่งเมทริกซ์ไปยังฟังก์ชันมีดังนี้

ตัวอย่าง

#include<iostream>
using namespace std;
void MatrixMultiplication(int a[2][3],int b[3][3]) {
   int product[10][10], r1=2, c1=3, r2=3, c2=3, i, j, k;
   if (c1 != r2) {
      cout<<"Column of first matrix should be equal to row of second matrix";
   } else {
      cout<<"The first matrix is:"<<endl;
      for(i=0; i<r1; ++i) {
         for(j=0; j<c1; ++j)
         cout<<a[i][j]<<" ";
         cout<<endl;
      }
      cout<<endl;
      cout<<"The second matrix is:"<<endl;
      for(i=0; i<r2; ++i) {
         for(j=0; j<c2; ++j)
         cout<<b[i][j]<<" ";
         cout<<endl;
      }
      cout<<endl;
      for(i=0; i<r1; ++i)
      for(j=0; j<c2; ++j) {
         product[i][j] = 0;
      }
      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<<"Product of the two matrices is:"<<endl;
      for(i=0; i<r1; ++i) {
         for(j=0; j<c2; ++j)
         cout<<product[i][j]<<" ";
         cout<<endl;
      }
   }
}
int main() {
   int a[2][3] = { {2, 4, 1} , {2, 3, 9} };
   int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} };
   MatrixMultiplication(a,b);
   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

ในโปรแกรมข้างต้น เมทริกซ์ a และ b ทั้งสองจะถูกเริ่มต้นในฟังก์ชัน main() ดังนี้

int a[2][3] = { {2, 4, 1} , {2, 3, 9} };
int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} };

ฟังก์ชัน MatrixMultiplication() ถูกเรียกด้วยค่า a และ b นี้ดูด้านล่าง

MatrixMultiplication(a,b);

ในฟังก์ชัน MatrixMultiplication() หากจำนวนคอลัมน์ในเมทริกซ์แรกไม่เท่ากับจำนวนแถวในเมทริกซ์ที่สอง การคูณจะไม่สามารถทำได้ ในกรณีนี้ ข้อความแสดงข้อผิดพลาดจะพิมพ์ออกมา ได้ดังนี้

if (c1 != r2) {
   cout<<"Column of first matrix should be equal to row of second matrix";
}

ทั้งเมทริกซ์ a และ b จะแสดงโดยใช้ nested for loop สิ่งนี้แสดงให้เห็นโดยข้อมูลโค้ดต่อไปนี้

cout<<"The first matrix is:"<<endl;
for(i=0; i<r1; ++i) {
   for(j=0; j<c1; ++j)
   cout<<a[i][j]<<" ";
   cout<<endl;
}
cout<<endl;
cout<<"The second matrix is:"<<endl;
for(i=0; i<r2; ++i) {
   for(j=0; j<c2; ++j)
   cout<<b[i][j]<<" ";
   cout<<endl;
}
cout<<endl;

หลังจากนี้ เมทริกซ์ผลคูณ [][] จะถูกเตรียมข้อมูลเบื้องต้นเป็น 0 จากนั้นจึงใช้ลูปที่ซ้อนกันเพื่อค้นหาผลคูณของเมทริกซ์ 2 เมทริกซ์ a และ b ซึ่งแสดงให้เห็นในข้อมูลโค้ดด้านล่าง

for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
   product[i][j] = 0;
}
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<<"Product of the two matrices is:"<<endl;
for(i=0; i<r1; ++i) {
   for(j=0; j<c2; ++j)
   cout<<product[i][j]<<" ";
   cout<<endl;
}