How to multiply matrices in C++. How to multiply arrays values in C++. Matrix multiplication program in C++. Logic to multiply two matrices in C++ programming. Program to multiply two arrays and store the result in another array.
Write the C++ program to multiply two matrices?
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include<iostream> using namespace std; int main() { int a[2][2]; // Matrix (array)1 int b[2][2]; // Matrix (array)2 int res[2][2];//matrix(array) to store the result int row, col, i; cout<<"Please Enter value in matrix 1\n"; for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { cout<<"enter value in element#["<<row<<"]["<<col<<"]="; cin>>a[row][col]; } } cout<<"Please Enter value in matrix 2\n"; for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { cout<<"Please enter value in element#["<<row<<"]["<<col<<"]="; cin>>b[row][col]; } } for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { int sum=0; for(i=0; i<=1; i++) { sum+=a[row][i]*b[i][col]; } res[row][col] = sum; } } cout<<"Please value of 1st matrix \n"; for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { cout<<a[row][col]<<"\t"; } cout<<endl; } cout<<"Please value of second matrix \n"; for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { cout<<b[row][col]<<"\t"; } cout<<endl; } cout<<"Please value of resultant matrix\n"; for(row=0; row<=1; row++) { for(col=0; col<=1; col++) { cout<<res[row][col]<<"\t"; } cout<<endl; } } |
Output