C++ Matrix Multiplication Program

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

C++ Matrix Multiplication Program

30 thoughts on “C++ Matrix Multiplication Program”

  1. 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'

    1. 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.

      1. 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

    1. 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().

    2. 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

    1. 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.

  2. Your Website really helps me in passing my exams 😛 since I need to do ctrl + F on my notebook (real notebook) Hehhee.

    1. Hey Shreekant, I am so happy to know that my website is helping you, keep visiting and recommend it to others 🙂

  3. 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++.

    1. 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.

  4. Piyush kumar dubey

    is we can get the .cpp file which we direct copy and paste in our turbo c++ folder and the program will run automatically…

  5. 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

Leave a Comment

Your email address will not be published. Required fields are marked *