Product of the Matrices in C++ Programming Language The Crazy Programmer
NOTE: E&OE, means errors and omissions are
expected and accepted, in other words, if
there is any error in the following article, the same has to be corrected by
the user or the beneficiary accordingly.
This article contains works derived from other reputed works of huge publication houses and notes
that were made by me in my school life.These articles are just to help people
who need these articles desperately and urgently. Any queries, complaints, and
corrections will be enthusiastically entertained.
Write
a C++ Program to find the Product of the Matrices
Product
of the Matrices in C++ Programming Language
Code Sample 1 (for Dev C++ & CodeBlocks)
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<iostream> #include<conio.h> using namespace std; int main() { int A[10][10], B[10][10], C[10][10], m, n, p, q, i, j, k; cout<<"Enter the number of rows and columns in Matrix A"; cout<<"(Enter the return button once):"<<endl; cin>>m>>n; cout<<"Enter the number of rows and columns in Matrix B"; cout<<"(Enter the return button once):-"<<endl; cin>>p>>q; if(n==p) { cout<<"Enter the elements of Matrix A"; cout<<"(Enter the return button once):-"<<endl; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>A[i][j]; cout<<"Enter the elements of Matrix B"; cout<<"(Enter the return button once):-"<<endl; for(i=0;i<p;++i) for(j=0;j<q;++j) cin>>B[i][j]; cout<<"\n\nMatrix A:-"; for(i=0;i<m;++i) { cout<<"\n \t"; for(j=0;j<n;++j) cout<<A[i][j]<<"\t"; } cout<<"\n\nMatrix B:-"; for(i=0;i<p;++i) { cout<<"\n \t"; for(j=0;j<q;++j) cout<<B[i][j]<<"\t"; } for(i=0;i<m;++i) { for(j=0;j<q;++j) { C[i][j]=0; } } for(i=0;i<m;++i) { for(j=0;j<q;++j) { for(k=0;k<n;++k) { C[i][j]=C[i][j]+(A[i][k]*B[k][j]); } } } cout<<endl<<"Product of the two Matrices:-"<<endl; for(i=0;i<m;++i) { for(j=0;j<q;++j) { cout<<"\n \t"<<C[i][j]; if(j == q-1) cout << endl; } } } else cout<<"\n\nMatrices are not compatible for multiplication!\n\n"; return(0); } |
Code Sample 2 (for Turbo 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<iostream.h> #include<conio.h> void main() { clrscr(); int A[10][10], B[10][10], C[10][10], m, n, p, q, i, j, k; cout<<"Enter the number of rows and columns in Matrix A"; cout<<"(Enter the return button once):"<<endl; cin>>m>>n; cout<<"Enter the number of rows and columns in Matrix B"; cout<<"(Enter the return button once):-"<<endl; cin>>p>>q; if(n==p) { cout<<"Enter the elements of Matrix A"; cout<<"(Enter the return button once):-"<<endl; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>A[i][j]; cout<<"Enter the elements of Matrix B"; cout<<"(Enter the return button once):-"<<endl; for(i=0;i<p;++i) for(j=0;j<q;++j) cin>>B[i][j]; cout<<"\n\nMatrix A:-"; for(i=0;i<m;++i) { cout<<"\n \t"; for(j=0;j<n;++j) cout<<A[i][j]<<"\t"; } cout<<"\n\nMatrix B:-"; for(i=0;i<p;++i) { cout<<"\n \t"; for(j=0;j<q;++j) cout<<B[i][j]<<"\t"; } for(i=0;i<m;++i) { for(j=0;j<q;++j) { C[i][j]=0; } } for(i=0;i<m;++i) { for(j=0;j<q;++j) { for(k=0;k<n;++k) { C[i][j]=C[i][j]+(A[i][k]*B[k][j]); } } } cout<<endl<<"Product of the two Matrices:-"<<endl; for(i=0;i<m;++i) { for(j=0;j<q;++j) { cout<<"\n \t"<<C[i][j]; if(j == q-1) cout << endl; } } } else cout<<"\n\nMatrices are not compatible for multiplication!\n\n"; getch(); } |
Sample Output:
Topics covered here from class XI course are Programs on String manipulations and Programs on array manipulations (1D & 2D).
___________________________________________________________________
Know more about stuff related to this practical
experiment (Important for Boards):-
- Viva Questions Array Manipulations.
- Difference between Dev C++ and Turbo C++.
- Difference between Dev C++ and CodeBlocks.
- Sum of 1 – x/2! + x^2/3! – x^3/4! + x^4/5! - ……..+ n series in C++ Programming Language
- Qualitative analysis for the identification of the functional group in the organic sample Acetic Acid (Example 1)
- Qualitative analysis for the identification of the functional group in the organic sample Benzoic Acid (Example 2)
- How to find out the mass of salt required to achieve the required concentration of a solution
- Different types of Hydrocarbons
Reference:-
- Arora, Sumita. Computer Science with C++ for Class - 12
- Arora, Sumita. Computer Science with C++ for Class – 11
- C programming Interview questions and answers [http://www.cquestions.com/]
- The Crazy Programmer [https://www.thecrazyprogrammer.com/]
- Programiz [https://www.programiz.com/cpp-programming/examples/matrix-multiplication]
- Matrix Calculator [https://matrixcalc.org/en/]
For latest syllabus, both theory and practical papers
for class XII, check out the CBSE’s official site http://cbseacademic.in/ for more info.
For more awesome coding and programs related to this language log onto http://www.thecrazyprogrammer.com or https://www.w3schools.in/.
___________________________________________________________________
Related Questions-
Loop Based program code
Loops for while
Array manipulations
Header files
Dev c++
Turbo C++
CodeBlocks
CBSE class 12 computer science practicals
CBSE class XII computer science practicals
Benzoic Acid
Aptitude Amplifier ©2017. All Rights Reserved.