-3

I want to generate a matrix with consecutive numbers starting from 1, in this form zig zag matrix

public static int[][] Zig_Zag(final int size) {
        int[][] data = new int[size][size];
        int i = 1;
        int j = 1;
        for (int element = 0; element < size * size; element++) {
            data[i - 1][j - 1] = element;
            if ((i + j) % 2 == 0) { // Even stripes if (j < size) j++; else i+=
                                    // 2; if (i > 1) i--; } else { // Odd
                                    // stripes if (i < size) i++; else j+= 2; if
                                    // (j > 1) j--; } } return data; }
            }
        }
        return data;
    }

Can anybody help?

Dipali Vasani
  • 2,526
  • 2
  • 16
  • 30
  • 1
    have you tried anything yet? SO is not a code generator platform. – SomeJavaGuy Apr 14 '16 at 11:30
  • I can print it in that form but only starting from the first index – Enxhi Gorari Apr 14 '16 at 11:32
  • Welcome to StackOverflow. A good question consists of what you want to achieve (which you've got), what you've tried so far (missing from your question), and what went wrong (also missing). SO is to help with **_specific_** problems. – Kevin Cruijssen Apr 14 '16 at 11:32
  • This is what i have got and tried so far, I can´t find a way to start printing from the bottom left corner. I have tried rotating the matrix 90 degrees counterclockwise but doesn´t seem to work. – Enxhi Gorari Apr 14 '16 at 11:35

3 Answers3

2

Try this

public static int[][] Zig_Zag(int size) {
    int[][] a = new int[size][size];
    int n = 1;
    for (int r = size, c = 0; r >= 0; --r)
        for (int i = r, j = c; i < size; ++i, ++j)
            a[i][j] = n++;
    for (int r = 0, c = 1; c < size; ++c)
        for (int i = r, j = c; j < size; ++i, ++j)
            a[i][j] = n++;
    return a;
}

and

int[][] a = Zig_Zag(4);
for (int[] r : a)
    System.out.println(Arrays.toString(r));

result:

[7, 11, 14, 16]
[4, 8, 12, 15]
[2, 5, 9, 13]
[1, 3, 6, 10]
0

Try this code :

public static int[][] Zig_Zag(final int size) {
        int[][] data = new int[size][size];
        int i = 1;
        int j = 1;
        for (int element = 1; element <= size * size; element++) {
            data[i - 1][j - 1] = element;
            if ((i + j) % 2 == 0) {
                // Even stripes
                if (j < size)
                    j++;
                else
                    i += 2;
                if (i > 1)
                    i--;
            } else {
                // Odd stripes
                if (i < size)
                    i++;
                else
                    j += 2;
                if (j > 1)
                    j--;
            }
        }
        return data;
    }

    public static void main(String[] args) {
        int[][] data = Zig_Zag(4);
        for(int i=0; i<data.length;i++){
            for(int j=0; j<data[i].length;j++){
                System.out.print(data[i][j]+" ");
            }
            System.out.println("");
        }
    }

Output:

1 2 6 7 
3 5 8 13 
4 9 12 14 
10 11 15 16
Dipali Vasani
  • 2,526
  • 2
  • 16
  • 30
  • This is the code I have. I need the printing to start from the bottom left corner not top left. I tried to change the indexes but it won´t work that way. – Enxhi Gorari Apr 14 '16 at 12:08
0

Not a very elegant solution:

private static int triangle_below(int n) {
    return n * (n + 1) / 2;
}

private static int except_triangle_above(int size, int n) {
    return size * size - triangle_below(2 * size - n);
}

private static int[][] gen(int size) {
    int[][] m = new int[size][size];

    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
            // already filled cells in lower diagonal layers
            int k = Math.min(
                triangle_below(i + j),
                except_triangle_above(size, Math.max(size, i + j + 1))
            );

            // position in current layer
            int l = Math.min(j + 1, size - i);

            m[size - i - 1][j] = k + l;
        }
    }

    return m;
}
jakubdaniel
  • 2,233
  • 1
  • 13
  • 20