Followers

Monday, March 12, 2012

SUM OF DIAGONAL ELEMENTS OF A SQUARE MATRIX

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

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)
      b=b+a[i][j];
      else
      continue;
    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      b=b+a[i][j];
      else
      continue;
    }
  }
  cout<<"\nSum of diagonal elements = "<<b;
}

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


No comments:

Post a Comment