c++ programs examples

C++ Program to multiply two 2D arrays and display the 1st, 2nd and 3rd arrays in proper format

c++ programs examples
/* 11. Write a program to multiply two 2-D arrays and display the 1st, 2nd and 3rd array in proper format. */

#include<iostream.h>
#include<conio.h>
 void main()
 {
  int a[100][100],b[100][100],c[100][100],i,j,k,row,col,coln;
  clrscr();
  cout<<"Enter first 2-D matrix row size->"<<endl;
  cin>>row;
  cout<<"Enter first 2-D matrix column size->"<<endl;
  cin>>col;
  cout<<"Second 2-D matrix row size cann't be changed"<<endl;
  cout<<"Therefore,enter column size->"<<endl;
  cin>>coln;
  cout<<"Enter elements in the first 2-D matrix"<<endl;
  for(i=0;i<row;i++)
  {
	for(j=0;j<col;j++)
	cin>>a[i][j];
	}
	cout<<"Enter elements in the second 2-D matrix"<<endl;
	for(i=0;i<col;i++)
	{
	 for(j=0;j<coln;j++)
	 cin>>b[i][j];
	 }
	 cout<<"First 2-D matrix is displayed"<<endl;
	 for(i=0;i<row;i++)
	 {
	  for(j=0;j<col;j++)
	  {
		cout<<"\t"<<a[i][j];
		}
		cout<<endl;
		}
		cout<<"Second 2-D matrix"<<endl;
	 for(i=0;i<col;i++)
	 {
	  for(j=0;j<coln;j++)
	  {
		cout<<"\t"<<b[i][j];
		}
		cout<<endl;
		}
	   for(i=0;i<row;i++)
	  {
		for(j=0;j<coln;j++)
		{
		c[i][j]=0;
		 for(k=0;k<coln;k++)
		 c[i][j]=a[i][k]*b[k][j]+c[i][j];
		 }
		}
	 cout<<"After multiplication resultant matrix is->"<<endl;
	 for(i=0;i<row;i++)
	 {
	  for(j=0;j<coln;j++)
	  {
		cout<<"\t"<<c[i][j];
		}
		cout<<endl;
	  }
	  getch();
	  }



Leave a Reply

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