Program for multiplication of two matrices in c++

Program for multiplication of two matrices in c++

The below program gives multiplication of two matrices.
In this first we enter order of matrix A then entered elements for matrix A, same procedure for matrix B.
matrix_multipication

From above Matrix A of order 1 X 4 and Matrix B of order 4 X 4 then Multiplication of Matrix A and B of order 1 X 4.

Suppose Matrix A of order 1 X 3 and Matrix B of order 4 X 4 then no. of column of matrix A i.e. 3 and no. of rows matrix B i.e. 4 is not match so it gives output as Multiplication is not possible.

#include<iostream.h>
#include<conio.h>

int main()
{
	clrscr();
	int a[10][10], b[10][10],c[10][10];
	int x,y,i,j,m,n;


	cout<<"\nEnter the number of rows and columns for Matrix A:::\n\n";
	cin>>x>>y;

	// x denotes number rows in matrix A
	// y denotes number columns in matrix A

	cout<<"\n\nEnter elements for Matrix A :::\n\n";

	for(i=0;i<x;i++)
	{
		for(j=0;j<y;j++)
		{
			cin>>a[i][j];
		}
		cout<<"\n";
	}



	cout<<"\n\nMatrix A :\n\n";

	for(i=0;i<x;i++)
	{
		for(j=0;j<y;j++)
		{
			cout<<"\t"<<a[i][j];
		}
		cout<<"\n\n";
	}

	cout<<"\n-----------------------------------------------------------\n";

	cout<<"\nEnter the number of rows and columns for Matrix B:::\n\n";
	cin>>m>>n;

	// m denotes number rows in matrix B
	// n denotes number columns in matrix B


	cout<<"\n\nEnter elements for Matrix B :::\n\n";

	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			cin>>b[i][j];
		}
		cout<<"\n";
	}


	cout<<"\n\nMatrix B :\n\n";

	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			cout<<"\t"<<b[i][j];
		}
		cout<<"\n\n";
	}

	if(y==m)
	{

		for(i=0;i<x;i++)
		{
			for(j=0;j<n;j++)
			{
				c[i][j]=0;
				for(int k=0;k<m;k++)
				{
					c[i][j]=c[i][j]+a[i][k]*b[k][j];
				}
			}
		}

		cout<<"\n-----------------------------------------------------------\n";

		cout<<"\n\nMultiplication of Matrix A and Matrix B :\n\n";

		for(i=0;i<x;i++)
		{
			for(j=0;j<n;j++)
			{
				cout<<"\t"<<c[i][j];
			}
			cout<<"\n\n";
		}
	}
	else
	{
		cout<<"\n\nMultiplication is not possible";
	}

	getch();
	return 0;
}

OUTPUT :

Program for multiplication of two matrices in c++
Program for multiplication of two matrices in c++

Leave a Reply

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

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>