int sum=0;
For (int i=0;i<Line;i++)
For(int j=0;j<COL;j++)
{
//Primary diagonal
if(i==j )sum += A[i][j];
//Secondary diagonal
if(i==(COL-j))sum += A[i][j];
}
Login to rate this answer.
Ashima
Answered On : Aug 9th, 2012
Assuming the matrix size is row, col and i represents the row and j represents the col.
Get the primary Diagonal where i==j and secondary diagonal where i==j-1.
Below is the code for that.
Code
for (int i = 0; i < row; i++)
{
int j = 0;
while (j < col)
{
//Primary diagonal
if (i == j) sum += A[i][j];
j++;
//Secondary diagonal
if (i == (col - j)) sum += A[i][j - 1];
}
}
Login to rate this answer.