+2 votes
in Mathematics by kratos

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 is

5 4 3

6 7 8

1 2 9

Output through the function should be:\

Diagonal One: 5 7 9

Diagonal Two: 3 7 1

1 Answer

+4 votes
by kratos
 
Best 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)]<<" ";

}

...