Sunday, 2 November 2014

addition of two matrix

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r1,c1,r2,c2,i=0,j=0;
  int*ptr1;
  int*ptr2;
  int*ptr3;
  cout<<"Enter row and column of a matrix for matrix1";
  cin>>r1>>c1;
  ptr1=new int[r1*c1];
  ptr2=new int[r2*c2];
  ptr3=new int[r1*c1];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  {
  cin>>ptr1[i*c1+j];
  }
  }
  cout<<endl<<"Matrix form-1";
  cout<<endl;
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  cout<<ptr1[i*c1+j]<<'\t';
  cout<<endl;

  }
  cout<<"Enter row and column of a matrix for matrix2";
  cin>>r2>>c2;
  ptr2=new int[r2*c2];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r2;i++)
  {
  for(j=0;j<c2;j++)
  {
  cin>>ptr2[i*c2+j];
  }
  }
  cout<<endl<<"Matrix form-2";
  cout<<endl;
  for(i=0;i<r2;i++)
  {
  for(j=0;j<c2;j++)
  cout<<ptr2[i*c2+j]<<'\t';
  cout<<endl;

  }
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c2;j++)
  ptr3[i*c2+j]=ptr1[i*c2+j]+ptr2[i*c2+j];

  }
  cout<<endl<<"The resulting matrix after adding two matrices";
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  cout<<ptr3[i*c1+j]<<'\t';
  cout<<endl;

  }
  getch();
}

No comments:

Post a Comment