InterviewSolution
Saved Bookmarks
| 1. |
Write a user-defined function in C++ to find and display the sum of both the diagonal elements of a two dimensional array MATRIX[6][6] containing integers. |
|
Answer» float diagonalSum(float MATRIX[6][6], int r, int c) { int i,j; float sum=0; //We are calculating sum of diagonal elements considering both diagonals //We are adding intersecting element on two diagonal twice for(i=0;i<r;i++) { for(j=0;j<c;j++) { if(i==j) //elements on first diagonal sum+= MATRIX [i][j]; if((i+j)==(r-1)) // elements on off-diagonal sum+= MATRIX [i][j]; } } return sum; } |
|