"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Matrix (diagonal elements ). Show all posts
Showing posts with label Matrix (diagonal elements ). Show all posts

Monday, February 1, 2010

Write a program to find the sum of squares of elements on the diagonal of a square matrix nxn


#include<iostream.h>
#include<conio.h>


# define n 10


void main( )
{
int mat[n][n];


int d;
long  left_diag = 0, right_diag = 0;


// Input elements
cout<<  "\n  Enter dimension of square matrix:";
cin >> d;


for(int i = 0; i < d ; i++)
for( int j = 0; j < d ; j++)
{cout<<"\n Enter elements for "<<  i+1 << "," << j+1 <"location :";
cin >> mat[i][j];
}


clrscr();


//Print the array
for( i = 0; i < d ; i++)


{for( j = 0; j < d ; j++)
   cout<<  mat[i][j]<<"\t";
cout<< "\n";
}


//left diagonal ----


for( i = 0; i < d ; i++)


 for( j = 0; j < d ; j++)
      if(i= = j)
         left_diag += mat[i][j] * mat [i][j];


//right diagonal ----


for(i = 0; i < d ; i++)


for( j = 0; j < d ; j++)
   if((i+j) = = (d - 1))
      right_diag += mat[i][j] * mat [i][j];


cout<< "\n Sum of the squares of left diagonal elements = " << left_diag;


cout<< "\n Sum of the squares of right diagonal elements = " <<  right _diag;




getch ( );


}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.