Skip to content
Advertisement

Printing a snake pattern using an array

I’m having trouble with an assignment where we are required to print out this array:

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

My code is somewhat correct but it is not printing 10 and 19 where it should be.

My output:

Choose a number for the rows from 0 to 16.
5
Choose a number for the columns from 0 to 16
5
1  0  10  0   19
2  9  11  18  20
3  8  12  17  21
4  7  13  16  22
5  6  14  15  23

My code:

//snake move with the number
import java.util.Scanner;

public class SnakeMove {
    public static void main(String[] args) {
        //create Scanner object
        Scanner inScan = new Scanner(System.in);

        //prompt the user to choose number for the Row from 0 to 16
        System.out.println("Choose a number for the rows from 0 to 16.");

        //take the input from user with nextInt() method
        //use the variable int row
        int row = inScan.nextInt();

        //prompt the user to choose number for the Col from 0 to 16
        System.out.println("Choose a number for the columns from 0 to 16");

        //take the input from user with nextInt()
        //use the variable int col
        int col = inScan.nextInt();
        if (row != col) {
            System.out.println("Run the program again and choose the same number for Row and Col");
            System.exit(0);
        }
        int[][] arr = move(row, col);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println();
        }
    }//main method

    static int[][] move(int row, int col) {
        boolean flag = true;
        int count = 1;
        int[][] array = new int[row][col];
        for (int j = 0; j < array[0].length; j++) {
            if (flag) {
                for (int i = 0; i < array.length; i++) {
                    //assign the increment value of count
                    // to specific array cells
                    array[i][j] = count;
                    count++;
                }
                flag = false;
            } else {
                //row decrement going up
                for (int i = array.length - 1; i > 0; i--) {
                    //assign the increment value of count
                    // to specific array cells
                    array[i][j] = count;
                    count++;
                }
                flag = true;
            }
        }//column increment
        return array;
    }//move method
}//end SnakeMove class

Can anyone detect what is causing the error? Any help would be appreciated.

Advertisement

Answer

This will generate the “snaking” pattern you described.

It could be simplified with ternary but this makes it more readable I think

It would be interesting to find a more clever way about it though, if anyone finds a better way pls comment

public static int[][] genArray(int length) {
    int[][] arr = new int[length][length];

    int counter = 0;
    for (int col = 0; col < arr.length; col++) {
        if (col % 2 == 0) {
            for (int row = 0; row < arr.length; row++) {
                arr[row][col] = counter++;
            }
        } else {
            for (int row = arr.length - 1; row >= 0; row--) {
                System.out.println("row: " + row + ", col: " + col);
                    arr[row][col] = counter++;
                }
            }
        }
    }
    return arr;
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement