2

I'm just a beginner and I'm trying to figure out how to create two-dimensional array, filled with numbers diagonally like in JPEG zigzag ordering: https://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg

I figured out how to get the upper-left part:

r=1;
for(z=0;z<n;z++)
{
    if(z%2=0)
    {
        for(i=0;i<=z;i++)
        {
           t[z-i][i]=r;
           ++r;
        }
    }
    else
    {
        for(i=0;i<=z;i++)
        {
           t[i][z-i]=r;
           ++r;
        }
    }
}  

And it works fine, but I can't figure out how to get the rest of it.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Piotr
  • 47
  • 6
  • 1
    Take a look to the ["Rosetta code - Zig-zag matrix"](https://rosettacode.org/wiki/Zig-zag_matrix). – J. Piquard Nov 30 '16 at 07:02
  • With that said, you can either create 2 more loops one that encapsulates your iteration, and the another loop that would offset your loop, both loops would have to be nested in conditional statements and the parent loop's count would choose the conditional statement in order to traverse the proper way. Although, off the top of my head, the solution I proposed adds complexity to the algorithm. Keep in mind I haven't slept since Monday morning and I stayed at a Holiday Inn express last night. – oOpSgEo Nov 30 '16 at 07:05
  • 1
    @J. Piquard I saw that, but C code shown there is too advanced for me. – Piotr Nov 30 '16 at 07:23
  • The way I see the moving pattern is this: 1. Start at [0,0]. 2. Take one step horizontally forward to [1,0]. 3. Travel diagonally, horizontally backwards and vertically forward until you hit an edge (in this case, [0, 1]). 4. Take one step vertically forward to [0,2]. 5. Travel diagonally, horizontally forward and vertically backward until you hit an edge (in this case, [2,0]). Repeat the process until you hit the last element at [n,n]. – KC Wong Nov 30 '16 at 07:35
  • This page briefly touches on zig-zag ordering and as a a bonus even includes some code. The downside is that it performs Morton order zig-zagging, which as somewhat different than the scheme you've presented. The fella now works with NVIDIA afaik and knows his stuff. http://www.iquilezles.org/www/articles/wavelet/wavelet.htm – enhzflep Nov 30 '16 at 07:36

1 Answers1

0

I saw that, but C code shown there is too advanced for me

You could just keep doing what you're doing with another nested set of for loops and revised indexes:

#include <stdio.h>

void zigZagArray(int *array, int n) {
    int r = 1;

    for (int z = 0; z < n; z++) {
        if (z % 2 == 0) {
            for (int i = 0; i <= z; i++) {
                // array[z - i][i] = r++;
                *((array + (z - i) * n) + i) = r++;
            }
        } else {
            for (int i = 0; i <= z; i++) {
                // array[i][z - i] = r++;
                *((array + i * n) + z - i) = r++;
            }
        }
    }

    for (int z = 1; z < n; z++) {
        if (z % 2 == n % 2) {
            for (int i = 1; i <= n - z; i++) {
                // array[z + i - 1][n - i] = r++;
                *((array + (z + i - 1) * n) + n - i) = r++;
            }
        } else {
            for (int i = 1; i <= n - z; i++) {
                // array[n - i][z + i - 1] = r++;
                *((array + (n - i) * n) + z + i - 1) = r++;
            }
        }
    }
}

void printArray(int *array, int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%2d ", *((array + i * n) + j));
        }
        printf("\n");
    }
}

int main() {

    int a[7][7];
    zigZagArray((int *) a, 7);
    printArray((int *) a, 7);

    printf("\n");

    int b[8][8];
    zigZagArray((int *) b, 8);
    printArray((int *) b, 8);

    printf("\n");

    int c[9][9];
    zigZagArray((int *) c, 9);
    printArray((int *) c, 9);

    return 0;
}

OUTPUT

% ./a.out
 1  2  6  7 15 16 28 
 3  5  8 14 17 27 29 
 4  9 13 18 26 30 39 
10 12 19 25 31 38 40 
11 20 24 32 37 41 46 
21 23 33 36 42 45 47 
22 34 35 43 44 48 49 

 1  2  6  7 15 16 28 29 
 3  5  8 14 17 27 30 43 
 4  9 13 18 26 31 42 44 
10 12 19 25 32 41 45 54 
11 20 24 33 40 46 53 55 
21 23 34 39 47 52 56 61 
22 35 38 48 51 57 60 62 
36 37 49 50 58 59 63 64 

 1  2  6  7 15 16 28 29 45 
 3  5  8 14 17 27 30 44 46 
 4  9 13 18 26 31 43 47 60 
10 12 19 25 32 42 48 59 61 
11 20 24 33 41 49 58 62 71 
21 23 34 40 50 57 63 70 72 
22 35 39 51 56 64 69 73 78 
36 38 52 55 65 68 74 77 79 
37 53 54 66 67 75 76 80 81 
%
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I see, the results vary depending if the given size is odd or even. That shouldn't be a big problem. Thank you :) – Piotr Nov 30 '16 at 08:48
  • @Piotr, the fix turned out to be trivial: change the `if (z % 2) {` in my new code to `if (z % 2 == n % 2) {`. I've updated my code and provided example output to confirm the fix. Enjoy. – cdlane Nov 30 '16 at 19:44