Printing a Spiral Matrix with java

Viewed 279

I'm trying to print a spiral matrix using a method that takes in an integer. However, my code on paper works fine, but when I run I get different numbers in place of the number I want.

public class ConstructSpiral{
    public static void main(String[] args) {

        constructSpiral(3);
    }

    public static int[][] constructSpiral(int n) {
        
        int[][] matrix = new  int[n][n];
        int sum = 0;
        int i = matrix.length - 1;
        int j = matrix[i].length - 1;
        
        for(j = matrix[i].length - 1; j >= 0; j--)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }
        
        j = 0;
        for(i = (matrix.length - 1) - 1; i >= 0; i--) {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 0;
        for(j = 1;  j < matrix.length; j++)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 1;
        for(j = 2; j <= i; j--){
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        for(i = matrix.length - 1; i >= 0; i--) {
            for(j = matrix[i].length - 1; j >= 0; j--) 
            {
                System.out.print(matrix[i][j] + ", ");
            }
            System.out.println();
        }

        return matrix;

    }
}

In reality, it should print something like

5 6 7
4 9 8
3 2 1

In help would greatly be appreciated.

1 Answers

I think you are on the right track you might just want to utilize the n parameter of your constructSpiral method a bit more, as well as move the printing logic into a different method:

import java.util.Scanner;

public class ConstructSpiral {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        int n = scanner.nextInt();
        int[][] spiral = constructSpiral(n);
        printSquareMatrix(spiral, n);
    }

    public static int[][] constructSpiral(int n) {
        int[][] matrix = new int[n][n];
        int sum = 1;
        int r1 = 0, c1 = 0, r2 = n - 1, c2 = n - 1;
        int i = 0, j = 0;
        while (sum <= n * n) {
            for (j = c2; j >= c1; j--)
                matrix[r2][j] = sum++;
            r2--;
            for (i = r2; i >= r1; i--)
                matrix[i][c1] = sum++;
            c1++;
            for (j = c1; j <= c2; j++)
                matrix[r1][j] = sum++;
            r1++;
            for (i = r1; i <= r2; i++)
                matrix[i][c2] = sum++;
            c2--;
        }
        return matrix;
    }

    public static void printSquareMatrix(int[][] matrix, int n) {
        int padLength = String.valueOf(n * n).length() + 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%1$-" + padLength + "s", matrix[i][j]);
            }
            System.out.println();
        }
    }

}

Example Usage 1:

Enter value of n: 3
5 6 7 
4 9 8 
3 2 1 

Example Usage 2:

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

Example Usage 3:

Enter value of n: 10
19  20  21  22  23  24  25  26  27  28  
18  51  52  53  54  55  56  57  58  29  
17  50  75  76  77  78  79  80  59  30  
16  49  74  91  92  93  94  81  60  31  
15  48  73  90  99  100 95  82  61  32  
14  47  72  89  98  97  96  83  62  33  
13  46  71  88  87  86  85  84  63  34  
12  45  70  69  68  67  66  65  64  35  
11  44  43  42  41  40  39  38  37  36  
10  9   8   7   6   5   4   3   2   1 
Related