Questions Again

Write a function in C++ which accepts a 2D array of integers and its size as
arguments and displays the elements which lie on diagonals. 2
[Assuming the 2D Array to be a square matrix with odd dimension
i.e. 3×3, 5×5, 7×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

I am not getting this question..I mean How can I find The diagonal 2?
Last edited on
You know, if you use better titles it will be easier to find a previous post that have the same issues as you.
By instance http://cplusplus.com/forum/beginner/62876/#msg340321
oh.. That message too was posted by me sorry...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>

using std::cout;
using std::endl;

int main()
{
  const int rows = 3;
  const int cols = 3;

  int v2d[][cols] = {{5,4,3},{6,7,8},{1,2,9}};

  // print the diagonal (0,0) (1,1) (2,2)
  cout << "Diagonal One: ";
  for(int r = 0, c = 0; r < rows; r++,c++)  // increment both r and c index
  {
    cout << v2d[r][c] << " ";
  }
  cout << endl;

  // print the diagonal (0,2) (1,1) (3,0)
  cout << "Diagonal Two: ";
  for(int r = 0, c = cols - 1; r < rows; r++,c--)  // increment r index, decrement c index
  {
    cout << v2d[r][c] << " ";
  }
  cout << endl;
  return 0;
}
Topic archived. No new replies allowed.