Here you will get C++ matrix multiplication program.
What we are doing in this program.
Read number of rows and columns for two matrix.
Then check if matrix multiplication is possible or not.
If not possible then show a message to user otherwise multiply them. Finally display the result.
C++ Matrix Multiplication Program
#include<iostream> using namespace std; int main() { int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k; cout<<"Enter rows and columns of first matrix:"; cin>>m>>n; cout<<"Enter rows and columns of second matrix:"; cin>>p>>q; if(n==p) { cout<<"\nEnter first matrix:\n"; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>a[i][j]; cout<<"\nEnter second matrix:\n"; for(i=0;i<p;++i) for(j=0;j<q;++j) cin>>b[i][j]; cout<<"\nThe new matrix is:\n"; for(i=0;i<m;++i) { for(j=0;j<q;++j) { c[i][j]=0; for(k=0;k<n;++k) c[i][j]=c[i][j]+(a[i][k]*b[k][j]); cout<<c[i][j]<<" "; } cout<<"\n"; } } else cout<<"\nSorry!!!! Matrix multiplication can't be done"; return 0; }
Output
This somehow does not work/compile for me.
It throws out the follwing error:
No main found…simply generating .o files…
In file included from
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/backward/iostream.h:31,
from mat.cpp:1:
mat.cpp:2:18: conio.h: No such file or directory
mat.cpp:5: error: `main' must return `int'
mat.cpp:5: error: return type for `main' changed to `int'
mat.cpp: In function `int main(…)':
mat.cpp:6: error: `clrscr' was not declared in this scope
mat.cpp:40: error: `getch' was not declared in this scope
mat.cpp:6: warning: unused variable 'clrscr'
mat.cpp:40: warning: unused variable 'getch'
By taking a look on the errors i concluded that you are using gcc on linux os. The above code is written in turbo c++ so it will not work in gcc.
This will work with gcc and clang and msvc as it doesn’t use any turbo c++ features
You need to call g++ in order to compile it as cpp code else this will throw an error every time you try to compile it.
CmdLine: g++ main.cpp -o main.out -std=c++11 -O3
Mind blowing.
Why do we use getch() before ending any program? What is the function of getch()?
Actually getch() is a function used to read character value without displaying it on the screen. When it is added at the end of the program, compiler waits and stops the screen for reading any character, as we press any key the program ends. I hope this will make you understand the use of getch().
I think u r new to c getch () is for inputting character value bt to keep output on the screen we hv to use getch other wise output will be terminate in just mili seccends
Getch function holds the programm output screen until any key is pressed.
why r u using c[i][j]=0
Jst to initialize destination array
U can skip c [i][j]=0
nice written but you must use //comments
why are we leave space btn cout<<[i][j]<<" ";
so that there are spaces in between the matrices and two numbers can be differentiated.
for ex:- 80 90 39 without space can be 809039. it will be difficult for us to know the numbers without spaces.
thanx …but correct the inverted commas
Why u r equating the rows and columns of two matrices ?!
Only then the multiplication is possible.
Your Website really helps me in passing my exams 😛 since I need to do ctrl + F on my notebook (real notebook) Hehhee.
Hey Shreekant, I am so happy to know that my website is helping you, keep visiting and recommend it to others 🙂
man that program isn’t helpful for me
may be it’s a wrong code
Hey the code gives the wrong answer when i input 1->9 for matrix 1 and 9->1 for matrix 2
hey…plz tell me the use of “k” in the program….
Hey I need some coding clarification ,Sorry to use this page in doing so .
I want to create a function with inputs of type vector and integer but the output should be a matrix .
{ If you are comfortable in matlab it has A = function_name(v,x) ; where x = 5;v = ones(5,1);A = zeros(5,4). }
I want to use the output matrix as input to another function .I’m new to c++ ,How could I do that ?
I tried a few methods ;but the function is of type void , which wont output a result ,and wont give me the freedom to do A = function_name(input1,input2) in c++.
Your the best ….
using namespace std;
what does this line indicate
The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc. Because these tools are used so commonly, it’s popular to add “using namespace std” at the top of your source code so that you won’t have to type the std:: prefix constantly.
thanks a lot
is we can get the .cpp file which we direct copy and paste in our turbo c++ folder and the program will run automatically…
Write a C++ program for matrix multiplication. Multiplication function should notify if the order of the matrix is invalid, using exception.
Can you help me to solve this
thank you sir. very helpful..keep uploading difficult programs
what is math behind the formulae…at the last….