C ++ array elements: Process 2d diagonally

Let's say we have two-dimensional arrays, arr[N][N] , where N is a constant integer . Assume that each element of arr initialized.

How to print arr elements with anti-diagonal using nested loops?

What I mean:

  • After the first iteration of the outermost loop, arr[0][0] will print
  • After the second iteration of the outer loop, arr[0][1] and arr[1][0] will be printed
  • After the third iteration of the outermost loop, arr[0][2] , arr[1][1] and arr[2][0] will be printed
  • ...
  • After the last iteration of the loop of the outer loop, arr[N-1][N-1] will be printed.

Thank you for your time!

+4
source share
7 answers

Sorry, who wrote "The second half should be similar" ... it is not.

Anyway, here you go:

 // traverse array diagonally int c, tmp, x; for (c = N - 1; c > -N; c--) { tmp = N - abs(c) - 1; x = tmp; while (x >= 0) { if (c >= 0) { std::cout << arr[x][tmp - x] << ", "; } else { std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", "; } --x; } std::cout << "\n"; } 

Do you need this for a game or something else?

[edit] looking at it again, I think my answer was not very beautifully written. It runs quickly here:

Suppose N is 3.

We need an iteration over coordinate combinations, which looks like this:

 (0, 0) (1, 0), (0, 1) (2, 0), (1, 1), (0, 2) (2, 1), (1, 2) (2, 2) 

So first, some placeholders:

 int c, // a counter, set by the outer loop tmp, // for intermediate results x; // the x-index into *arr* (*y* will be defined implicitly) 

Now this outer loop

 for (c = N - 1; c > -N; c--) { 

does iterating over {2, 1, 0, -1, 2}.

Next step

  tmp = N - abs(c) - 1; x = tmp; 

turns {2, 1, 0, -1, -2} into {0, 1, 2, 1, 0}, which are the lengths of the required outputs at this stage minus one (therefore, they can be used as indices). We make two copies of this, tmp and x.

Now we are counting from x to 0:

  while (x >= 0) { ... --x; } 

if we are in the upper left half of arr denoted by c> = 0, the x-indices in arr should start diagonally and go down to zero (from 0 to 0, from 1 to 0 and 2 to 0), while the y-indices should start from zero and go to the diagonal (from 0 to 0, from 0 to 1 and from 0 to 2):

  if (c >= 0) { std::cout << arr[x][tmp - x] << ", "; } 

when we are in the lower right half, x-indices should start with N and go down to the diagonal (from 2 to 1 and 2 to 2), while y-indices should start from the diagonal and go to N (from 1 to 2 and from 2 to 2):

  else { std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", "; } 

Finally, we just need a line break at the end of each line:

  std::cout << "\n"; 

Savy ?:-)

+3
source

This will work on half the matrix. the other half will be similar:

 for (j = 0 ; j < N ; j++) { for (i = 0 ; i <= j ; i ++) { printf("%d \n",a[i,ji]); } } 
+2
source

You can notice that for any diagonal 2 β€œadjacent” elements are set to [x][y] and [x+1][y-1] : that is, you take a diagonal step to the right and up.

So, you may have a loop that sets the first cell of the diagonal. You only need to iterate all the y values, starting with [0][y] , and then do this right step (diagonally) until you press the top or right side. Then you will need to do the same, moving from [0][N-1] to [N-1][N-1] to cover the other half.

Code follows:

 for (int _y = 0; _y < N; _y++) { int x = 0, y = _y; while (x < N && y >= 0) { cout << arr[x][y]; x++; y--; } cout << endl; // don't forget a newline } 

I am going to leave the second half of the code because it should be about the same.

+2
source

Here is a snippet of Java code, but algo is the same

 for(int i = 0; i < 10; i++){ for(int j = 0; j <= i; j++){ System.out.print(a[j][ij] + " "); } System.out.println(); } 
+1
source

It looks something like this:

 for(row = 0; row < N; row++){ for(j = 0; j <= row; j++){ print Array[row - j][j]; } newline; } 
+1
source

Here is the solution for both halves of the matrix:

  //First half (including middle diagonal) for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { print array[j][i - j]; } newline; } //Second half (excluding middle diagonal) for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { print array[n - i + j][n - j - 1]; } newline; } 
+1
source

Here is one solution, I think its useful R is the total number of rows.

  void diagonalOrder(int arr[][COLS],int R) { for (int i = 0; i < R+COLS-1; i++) { int col; int row; i<COLS?col=i:col=(COLS-1); col>i?row=col-i:row=i-col; for(int j=col;j>=0 ;j--) { if(row<R) cout<<arr[row][j]<<" "; row++; } cout<<endl; } } ie. const int ROWS = 4; const int COLS = 3; int arr[][COLS] = {{ 1, 2, 4 }, { 3, 5, 7}, { 6, 8, 10}, { 9, 11, 12} }; diagonalOrder(arr,ROWS); Output ---------- 1 2 3 4 5 6 7 8 9 10 11 12 ------------------------------------------------------ const int ROWS = 8; const int COLS = 3; int arr8[][COLS] = {{ 1, 2, 4 }, { 3, 5, 7 }, { 6, 8, 10 }, { 9, 11, 13 }, { 12, 14, 16}, { 15 ,17, 19}, { 18 ,20, 22}, { 21, 23, 24} }; cout<<"\n\n8*3 Matrix"<<endl<<endl; diagonalOrder(arr8,8); -------------------------------------------------------------- Output -------------------------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ----------------------------------------- int arr[][COLS] = {{ 1, 2, 4 ,20}, { 3, 5, 7,20}, { 6, 8, 10,20}, { 9, 11, 12,20} }; ------------------------------------------------------------- Output ------------------------------------------------------------- 1 2 3 4 5 6 20 7 8 9 20 10 11 20 12 20 You can work with n*n Matrix .. 
0
source

All Articles