Sunday 7 July 2013

Two Dimensional Array Program

 How to find boundary elements and higher and lower level of a matrix.

#include<iostream.h>
#include<conio.h>
void main()
{
  int a[5][5],m,n,sum=0;
  clrscr();
  cout<<"Enter row :";
  cin>>m;
  cout<<"Enter coloum :";
  cin>>n;
  cout<<"Enter matrix element :\n";
  for(int i=0;i<m;i++)
  {
        for(int j=0;j<n;j++)
        {
          cout<<i<<j;
          cin>>a[i][j];
        }
  }
  cout<<"\nMatrix is : \n";
  for(i=0;i<m;i++)
  {
        for(int j=0;j<n;j++)
        {
          cout<<"\t"<<a[i][j];
        }
        cout<<"\n";
  }
  cout<<"\nBoundary element :";
  for(i=0;i<m;i++)
  {
        for(int j=0;j<n;j++)
        {
          if((i*j==0)||(i==m-1)||(j==n-1))
          {
            cout<<"  "<<a[i][j];
            sum=sum+a[i][j];
          }
        }
  }
  cout<<"\nSum of boundary element is : "<<sum;

 cout<<"\nHigher level : ";
 sum=0;
 for(i=0;i<m;i++)
 {
   for(int j=0;j<n;j++)
   {
             if(i==j)
             {

               cout<<"\t"<<a[i][j];
               sum=sum+a[i][j];
             }
   }

 }
  cout<<"\nSum of higher level =  "<<sum;

  cout<<"\nLower lavel : ";
  sum=0;
  i=m;
  for(int j=0;j<n;j++)
  {
            i=i-1;
            cout<<"\t"<<a[i][j];
            sum=sum+a[i][j];
  }
  cout<<"\nSum of lower level = "<<sum;
  getch();
}

Here is the Output :->



No comments:

Post a Comment