Re: 2D Array - Spiral Print

Hi TA,

I would like to ask you about this spiral print. I am not getting much luck with printing the last part of the matrix and do not get 22, 23, 33, 32 printed. This is the only part. I have re-watched the video two times but still cannot find my error.

The loop is not stopping. How should I fix it? Thanks!

public class S5_5_SpiralPrint {
public static void main(String[] args) {
int[][] arr = {{11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34}, {41, 42, 43, 44}};
spiralPrintClockWise(arr);
}

private static void spiralPrintClockWise(int[][] arr) {
    // Initialise six parameters

    int top, bottom, left, right, counter, direction;
    top = 0;
    bottom = arr.length - 1;
    left = 0;
    right = arr[top].length - 1;
    direction = 1;
    counter = (bottom + 1) * (right * 1);

    while (left <= right && top <= bottom) {
        if (counter > 0) { // This is to check if there is any elements to be printed.
            // This is for top row
            if (direction == 1) {
                for (int i = left; i <= right; i++) {
                    System.out.print(arr[top][i] + ", "); // top is a fixed row. Therefore, top is used. i is variable.
                    counter--;
                }
                direction = 2; // Since for loop is over, change the direction to 2 to print col.
                top++; // Increment by 1 since 14 has been printed and move to direction 2.
            }
            System.out.println();
        }

        if (counter > 0) {
            // This is for right col
            if (direction == 2) {
                for (int i = top; i <= bottom; i++) {
                    System.out.print(arr[i][right] + ", "); // Since col is fixed, right is used. i is array and is changing.
                    counter--;
                }
                direction = 3;
                right--;
            }
            System.out.println();
        }

        if (counter > 0) {
            // This is for bottom row
            if (direction == 3) {
                for (int i = right; i >= left; i--) {
                    System.out.print(arr[bottom][i] + ", ");
                    counter--;
                }
                direction = 4;
                bottom--;
            }
            System.out.println();
        }

// This is the loop I am having trouble with. Thanks.

        if (counter > 0) {
            // This is for left col
            if (direction == 4) {
                for (int i = bottom; i >= top; i--) {
                    System.out.print(arr[i][left] + ", "); // col is fixed, row is changing.
                    counter--;
                }
                direction = 1; // Set the direction to 1
                left++;
            }
            System.out.println();
        }
    }
    System.out.println("END");
}

}

hey @sumyatkw
just a change
counter = (bottom + 1) * (right + 1);
instead of
counter = (bottom + 1) * (right * 1);

Thanks for your prompt reply! :+1: