Matrix Multiplication in C++
Here you will get and learn the program code of 3×3 matrix multiplication in C++ programming.
Working of program
Input the values of matrix 1 and matrix 2 of 3*3 size.
Multiply the values of both matrix and store in matrix 3.
Print the result (matrix 3 values).
Example Code of 3×3 Matrix Multiplication in C++
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 | #include<iostream> using namespace std; main() { int a[3][3],b[3][3],m[3][3]; int rows=0,cols=0; int r=3,c=3,i,j,k; for(i=0;i<r;i++) { rows=i+1; for(j=0;j<c;j++) { cols=j+1; cout<<"Enter the value for Matrix 1 "<<" row "<<rows<<" column "<<cols<<" => "; cin>>a[i][j]; } } for(i=0;i<r;i++) { rows=i+1; for(j=0;j<c;j++) { cols=j+1; cout<<"Enter the value for Matrix 2 "<<" row "<<rows<<" column "<<cols<<" => "; cin>>b[i][j]; } } int r1=0,c1=0,r2=0,c2=0; int p=0; for(i=0;i<r;i++) { for(j=0;j<c;j++) { p=0; for(k=0;k<r;k++) { p=p+a[r1][c1]*b[r2][c2]; c1++;r2++; } m[i][j]=p; c1=0;r2=0;c2++; } r1++;c1=0;c2=0;r2=0; } cout<<"\n***************Matrix after Multiplication***********************\n\n"; for(i= 0;i<r;i++) { for(int j=0;j<c;j++) { cout<<m[i][j]; cout<<" "; } cout<<endl; } } |
Output
Other Similar Programs
Find Highest and Lowest Element of a Matrix in C++
Check out our other C++ programming Examples