InterviewSolution
Saved Bookmarks
| 1. |
Write a function in C++ which accepts a 2D array of integers and its size as arguments and display the elements which lie on diagonals.[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x 3, 5 x 5, 7 x 7 etc….] Example, if the array content is5 4 3 6 7 81 2 9Output through the function should be:\Diagonal One: 5 7 9Diagonal Two: 3 7 1 |
|
Answer» const int n=5; void Diagonals(int A[n][n], int size) { int i,j; cout<<"Diagonal One:"; for(i=0;i<n;i++) cout<<A[i]ij]<<" "; cout<<"\n Diagonal Two:" for(i=0;i<n;i++) cout<<A[i][n-(i+1)]<<" "; } |
|