Mar 28, 2020 at 12:16am UTC
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 Mar 28, 2020 at 12:24am UTC
Mar 28, 2020 at 1:11am UTC
What have you tried so far?
Do you know how to print ALL the elements of a 2d vector?
Show us that first.
Mar 28, 2020 at 1:32am UTC
In which order should the elements of the matrix
1, 2, 3,
4, 5, 6,
7, 8, 9
be visited?
Last edited on Mar 28, 2020 at 1:33am UTC
Mar 28, 2020 at 1:36am UTC
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 Mar 28, 2020 at 1:45am UTC
Mar 28, 2020 at 1:36am UTC
@mbozzi
From the bottom left diagonal i.e
7
4 8
1 5 9
2 6
3
Mar 28, 2020 at 2:11am UTC
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 Mar 28, 2020 at 3:16am UTC
Mar 28, 2020 at 9:57am UTC
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 Mar 28, 2020 at 11:22am UTC
Mar 28, 2020 at 1:32pm UTC
I didn't want to give him something he could immediately copy.
¯\_(ツ)_/¯