Christopher Baldwin - 1 year ago 86

Java Question

Full Disclosure: Homework.

Explanation: I cant understand my teacher.

Problem:

Write a method calledthat takes in two integer`printSquare`

parameters, aand a`min`

, and prints the numbers in the range from`max`

to`min`

inclusive in a square pattern. The square pattern is`max`

easier to understand by example than by explanation, so take a look at

the sample method calls and their resulting console output in the

table below. Each line of the square consists of a circular sequence

of increasing integers betweenand`min`

. Each line prints a`max`

different permutation of this sequence. The first line begins with

min, the second line begins with, and so on. When the`min + 1`

sequence in any line reaches, it wraps around back to`max`

. You`min`

may assume the caller of the method will pass aand a`min`

`max`

parameter such thatis less than or equal to`min`

`max`

I cannot for the life of me figure out how to make the numbers stop at the 'max' value and start over in the middle of the line.

This is what I have so far, apologies but I have trouble with for loops.

`for(int i = 0; i < row; i++)`

{

for(int d = 0; d < row; d++)

{

System.out.print(d+1);

}

System.out.println(i);

}

I know I used row twice, but its the only way i can get the compiler to form a square shape with the loop. Does anyone even remotely understand what i'm trying to do? :/

Answer Source

This is actually a nice mathematical problem. Assume:

```
int side = to - from + 1; /// the size/width of the square.
```

the value at any point in the square (row, col) is:

```
from + ((row + col) % side)
```

you should be able to put that in your loops and "smoke it".

Edit based on comment asking for explanation.

The trick is to loop through all the positions in the 'matrix'. Given that the matrix is square, the loops are relatively simple, just two loops (nested) that traverse the system:

```
final int side = to - from + 1;
for (int row = 0; row < side; row++) {
for(int col = 0; col < side; col++) {
... magic goes here....
}
}
```

Now, in this loop, we have the variables `row`

and `col`

which represent the cell in the matrix we are interested in. The value in that cell needs to be proportional to the distance it is from the origin..... let me explain.... If the origin is the top left (which it is), then the distances from the origin are:

```
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
```

The distance is the sum of the row and the column...... (rows and columns start counting from 0).

The values we put in each matrix are limited to a fixed range. For the above example, with a square of size 5, it could have been specified as `printSquare(1,5)`

.

The value in each cell is the from value (1 in this example) plus the distance from the origin... naively, this would look like:

```
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
```

here the values in the cell have exceeded the limit of 5, and we need to wrap them around... so, the trick is to 'wrap' the distances from the origin..... and the 'modulo' operator is great for that. First, consider the original 'origin distance' matrix:

```
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
```

if we instead populate this matrix with 'the remainder of the distance when dividing by 5' (the modulo 5, or %5) we get the matrix:

```
0 1 2 3 4
1 2 3 4 0
2 3 4 0 1
3 4 0 1 2
4 0 1 2 3
```

Now, if we add this 'modulo' result to the from value (1), we get our final matrix:

```
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
```

in a sense, all you need to know is that the value at each cell is:

```
the from value plus the remainder when you divide the 'distance' by the width.
```

Here's the code I tested with:

```
public static final String buildSquare(final int from, final int to) {
final StringBuilder sb = new StringBuilder(side * side);
final int side = to - from + 1;
for (int row = 0; row < side; row++) {
for(int col = 0; col < side; col++) {
sb.append( from + ((row + col) % side) );
}
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(buildSquare(1, 5));
System.out.println(buildSquare(3, 9));
System.out.println(buildSquare(5, 5));
System.out.println(buildSquare(0, 9));
System.out.println(buildSquare(0, 3));
}
```