How to print all the diagonal elements of a 2d vector

For example I have a 2d vector

matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
]

and I want to print the elements in diagonal order from the bottom i.e.

"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".

Can someone show how to print diagonal elements in a 2d vector?

Last edited on
What have you tried so far?
Do you know how to print ALL the elements of a 2d vector?
Show us that first.
@Ganado Yes, I know how to print the elements of a 2d vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void print(vector<vector<int> > vect)
{
      for (int i = 0; i < vect.size(); i++) { 
        for (int j = 0; j < vect[i].size(); j++) 
            cout << vect[i][j] << " "; 
        cout << endl; 
    } 
}

int main() 
{ 
    vector<vector<int> > vect{ { 1, 2, 3 }, 
                               { 4, 5, 6 }, 
                               { 7, 8, 9 } }; 
  
    print(vector<vector<int> > vect);
  
    return 0; 
} 
In which order should the elements of the matrix
1, 2, 3,
4, 5, 6,
7, 8, 9

be visited?
Last edited on
I assume it means from the bottom-left, so starting at [Height-1][0].

Edit: Notice the number of diagonals is Width + Height -1.
So if you can map each "diagonal" (1 to N) to their corresponding elements, you win.
Last edited on
@mbozzi

From the bottom left diagonal i.e

7
4 8
1 5 9
2 6
3
Going off my last post, about how there are Width + Height - 1 diagonals, this might help you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    vect = { {1,2,3,4},
             {5,1,2,3},
             {9,5,1,2} };


    int height = vect.size();
    int width = vect[0].size();
    int n = width + height - 1; // 4 + 3 -1 == 6
    
    for (int diagonal = 0; diagonal < n; diagonal++)
    {
        // start at [Height-1][0]
        int starting_i = std::max(0, height-1 - diagonal);
        int ending_i = std::min(height-1, n-1 - diagonal);
        for (int i = starting_i; i <= ending_i; i++)
        {
            std::cout << "i=" << i << ", ";   
        }
        std::cout << '\n';
    }

i=2, 
i=1, i=2, 
i=0, i=1, i=2, 
i=0, i=1, i=2, 
i=0, i=1, 
i=0, 

So there's the output for the 'i' (each row) elements.
So how would you figure out a pattern for the 'j' elements (each column)?
My advice is: Now that you know the number of diagonals, look at the patterns of which elements are being accessed at each diagonal.

I find it helps to make a plot of the starting index of the element as a function of the diagonal number, and a plot of the ending index at each diagonal.

3x4 matrix

  diagonal #    1         2        3        4        5        6 
  length        1         2        3        3        2        1 
  i =           2        1,2     0,1,2    0,1,2     0,1       0 
  j =           0        0,1     0,1,2    1,2,3     2,3       3 



____________________________

Edit: I actually found it easier to write something like this:
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
int step(int n)
{
    if (n >= 0)
        return 1;
    else
        return 0;
}

int main()
{
    // ...

    for (int diagonal = 0; diagonal < n; diagonal++)
    {
        int diagonal_length_up = 1 + diagonal;
        int diagonal_length_down = step(diagonal - n/2) * (1 + 2 * diagonal - n);
        int diagonal_length = diagonal_length_up - diagonal_length_down;
     
        //std::cout << diagonal_length << '\n';

        int starting_i = /* ... */;
        int starting_j = /* ... */;

        for (int index = 0; index < diagonal_length; index++)
        {
            // ...
        }
    }
}

I'm sure there's other ways to do it.
Make sure it works for both even and odd #s of diagonals.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void printDiagonals( const vector< vector<int> > &vect )
{
   int rows = vect.size(), cols = vect[0].size();
   for ( int d = rows - 1; d >= -( cols - 1 ); d-- )
   {
      for ( int i = max( d, 0 ); i < min( rows, d + cols ); i++ ) cout << vect[i][i-d] << '\t';
      cout << '\n'; 
   } 
}

int main() 
{ 
   vector< vector<int> > vect{ { 1, 2, 3 }, 
                               { 4, 5, 6 }, 
                               { 7, 8, 9 } };
   printDiagonals( vect );
}
Last edited on
I didn't want to give him something he could immediately copy.
¯\_(ツ)_/¯
Topic archived. No new replies allowed.