Ads 468x60px

Saturday 19 January 2013

Finding Sum of Two Matrix C++ Program

This program will firstly ask the user to input the number of rows and columns of the matrix that he want to add then the program will ask to user to enter the elements of the matrix after taking the input it will display the matrix that entered with the sum. 

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 //variables
 int r,c, mat1[10][10], mat2[10][10], sum[10][10];
 int i,j;

 //input part begin
 cout<<"Enter the number of rows:\t";
 cin>>r;
 cout<<"Enter the number of columns:\t";
 cin>>c;
 cout<<"Enter the elements of the matrix 1:\n";

 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   cout<<"Row "<<i+1<<", Column "<<j+1<<":\t";
   cin>>mat1[i][j];
  }
 }

 cout<<"Enter the elements of the matrix 2:\n";

 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   cout<<"Row "<<i+1<<", Column "<<j+1<<":\t";
   cin>>mat2[i][j];
  }
 }
 //input parts end

 //displaying the matrices entered

 cout<<"Matrix 1:\n";
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   cout<<mat1[i][j];
   cout<<"\t";
  }
  cout<<endl;
 }

 cout<<"Matrix 2:\n";
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   cout<<mat2[i][j];
   cout<<"\t";
  }
  cout<<endl;
 }

 //calculation part begin
 cout<<"The sum of the matrices are:\n";
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   sum[i][j]=mat1[i][j]+mat2[i][j];
  }
 }
 //calculation part end

 //displaying the sum in matrix form
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   cout<<sum[i][j];
   cout<<"\t";
  }
  cout<<endl;
 }
 getch();
}
Any queries regarding this code, feel free to ask by commenting here.. 
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

 
Related Posts Plugin for WordPress, Blogger...