Followers

Monday, March 12, 2012

DIAGONAL ELEMENTS IN A SQUARE MATRIX


#include<iostream.h>
#include<conio.h>
int x[10][10], n, i, j;

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

void otpt(int a[10][10] , int n)
{
  for(i=0 ; i<n/2 ; i++)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      cout<<a[i][j]<<" , ";

    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      {
      cout<<a[i][j];
      if(i==n/2)
        cout<<" .";
      else
        cout<<" , ";
      }
    }
  }

}

void main()
{
  clrscr();
  cout<<"Enter the number of rows or columns : ";
  cin>>n;
  cout<<"Enter a matrix : ";
  inpt(x,n);
  cout<<"Diagonal elements : ";
  otpt(x,n);
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

No comments:

Post a Comment