#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 ( );
}
No comments:
Post a Comment