Here is the program to find the sum of the secondary diagonal elements (From right top to left bottom ) of the entered integer square matrix. The program asks for the order (M, N) [where M should equal N] of the matrix. Soon after entering the order, the cursor takes to the proper position in the screen to input the matrices. User need to hit ‘Enter’ button after each entry. Matrix is stored as A.
Logic : The secondary diagonal elements will have a constant relation in their row and column column count. As the row count increases, the column count decreases, to maintain the sum of the two to ( M+1) where M is the order. By keeping this in mind, we can trace the matrix till the end using the help of a for loop. The flag ‘sum’ is updated in each of the iterations.
Finally, we’ll get the sum of the diagonal elements after the end of the while loop.
You are always welcome with your suggestion and doubts into our discussion forum.
int A,i,j,m,n,sum = 0;
printf(“\n\n\t ENTER A ORDER OF THE MATRIX M,N…: “);
printf(“\n\n\t ENTER THE ELEMENTS OF THE MATRIX..:\n\n”);
if(m == n)
sum = sum + A[i][n+1-i];
printf(“\n\t THE SUM OF SECONDARY DIAGONAL OF A MATRIX IS…: %d”, sum);
printf(“\n\t THE ORDER OF THE MATRIX IS NOT CORRECT”);
printf(“\n\n\t HELP : ‘M’ SHOULD BE EQUAL TO ‘N'”);
Download exe and source code here.
Leave a Reply