Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Proceed to Spiral class and implement its static method:

int[][] spiral(int rows, int columns)
Return a two-dimensional array coming in the form of a table and containing numbers from 1 up to rows * columns. The size of the table will be specified by the given parameters.
Numbers fill the "table" clockwise from the top-level corner in a spiral manner.
For example, for parameter values (3, 4), the output array should be:
1  2  3  4
10 11 12  5
9  8  7  6

For example, for parameter values (5, 5), the output array should be:
123   45
16  17  18  19   6
15  24  25  20   7
14  23  22  21   8
13  12  11  10   9


This is my code:


Java
<pre>
class Spiral {
    static int[][] spiral(int rows, int columns) {
        int[][] matrix = new int[rows][columns];
        int col_start = 0, col_end = rows - 1;
        int row_start = 0, row_end = columns - 1;
        int element = 1;
 
        while(col_start <= col_end && row_start <= row_end) {
            //top
            for(int j = col_start; j <= col_end; j++) {
                matrix[row_start][j] = element++;
            }
 
            // right side
            for(int i = row_start + 1; i <= row_end; i++) {
                matrix[i][col_end] = element++;
            }
 
            //bottom side
            for (int j = col_end - 1; j >= col_start; --j) {
                matrix[row_end][j] = element++;
            }
 
 
            //left side
            for (int i = row_end - 1; i > row_start; --i) {
                matrix[i][col_start] = element++;
 
            }
            col_start++;
            col_end--;
            row_start++;
            row_end--;
        }
 
        return matrix;
    }
    public static void main(String[] args) {
        {
            int[][] spiral = Spiral.spiral(4, 5);
 
            for (int i = 0; i < spiral.length; i++) {
                for (int j = 0; j < spiral[i].length; j++) {
                    System.out.print(String.format("%4s", spiral[i][j]));
                }
                System.out.println();
            }
        }
    }
}


What I have tried:

Basically I know how to generate the matrix in spiral form. When the number of rows is equal with the number of columns the result is correct.
But when the number of rows is different than the the number of columns I have a wrong result.


Do you know what should I do to find the correct result?
Posted
Updated 27-Nov-22 2:00am
v2
Comments
Richard MacCutchan 27-Nov-22 7:29am    
You have not explained what is wrong with the above code.
Cris29M 27-Nov-22 8:04am    
When the number of rows is different than the the number of columns I have a wrong result.
Richard MacCutchan 27-Nov-22 8:10am    
Yes, we know that, but you have not explained in what way it is wrong. It is no good saying things like, "I get wrong result", "it's not working", etc. We have no idea what you see on your screen and neither can we read your mind.
Cris29M 27-Nov-22 10:08am    
Yeah, you're right! I should have been more explicit!
I received this result:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3"
Richard MacCutchan 27-Nov-22 10:19am    
Array indices go from 0 to Array.length - 1. So for an array of 3 elements the indices are 0, 1 and 2. So you need to inspect all your loops carefully to see which one is breaking the code.

1 solution

The way I would do it is to use a single loop: 1 to N (where N is the number of elements).
I'd also add a x and y initially set to zero, and a deltaX and deltaY set to 1 and 0 respectively.

Each time I went round the loop, I'd put the loop counter value into the array at (x, y) and change x and y by their respective delta values.
Then check: is x right of the array? If it is, then x moves back one value, and deltaX becomes 0. y is incremented by one, and deltaY is set to one.

Then check y for it's limit and start it moving to the left where necessary.
Then check x for the left hand limit, and so forth.

If you think about it, that's pretty much the way you do it yourself!
Give it a try on paper and you'll see what I mean.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900