Followers

Saturday, December 3, 2011

SUM OF TWO MATRICES : SPECIAL DESIGN

#include<iostream.h>
#include<conio.h>
int n, m , x[3][3], y[3][3], z[3][3], i, j, k, l;

void inpt(int a[3][3] , int n , int m)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<m ; j++)
      cin>>a[i][j];
  cout<<"the matrix :\n";
  for(i=0 ; i<n ; i++)
  {
    cout<<"\n";
    for(j=0 ; j<m ; j++)
      cout<<a[i][j];
  }
  cout<<"\n";
}

void sum(int x[3][3], int y[3][3] , int z[3][3], int n, int m)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<m ; j++)
      z[i][j]= x[i][j] + y[i][j];
}

void otpt(int x[3][3], int y[3][3] , int z[3][3], int n, int m)
{
  for(i=0 ; i<n ; i++)
  {
    if(i==1)
    {
      for(j=1 ; j<=1 ; j++)
      cout<<" "<<x[i][j]<<" +";
      for(k=1 ; k<=1 ; k++)
      cout<<" "<<y[i][k]<<" =";
      for(l=1 ; l<=1 ; l++)
      cout<<" "<<z[i][l];
    }

    else
    {
      for(j=0 ; j<3 ; j++)
      {
      if(j==1)
        continue;
      cout<<x[i][j]<<" ";
      }

      for(k=0 ; k<3 ; k++)
      {
      if(k==1)
        continue;
      cout<<y[i][k]<<" ";
      }

      for(l=0 ; l<3 ; l++)
      {
      if(l==1)
        continue;
      cout<<z[i][l]<<" ";
      }
    }
    cout<<"\n";
  }
}

void main()
{
  clrscr();
  cout<<"Enter the number of rows : ";
  cin>>n;
  cout<<"Enter the number of columns : ";
  cin>>m;
  cout<<"Enter the 1st matrix :\n";
  inpt(x,n,m);
  cout<<"Enter the 2nd matrix :\n";
  inpt(y,n,m);
  sum(x,y,z,n,m);
  cout<<"\nThe sum of the given matrices :\n";
  otpt(x,y,z,n,m);
  getch();
}
.................................................................
OUTPUT:(CLICK TO ENLARGE)


No comments:

Post a Comment