# Help with recursive squares

• 04-18-2011, 08:32 PM
luke
Help with recursive squares
Hello, I have to write a program that produces the recursive pattern shown below.

I tried different recursions but I cannot get it done. This is what I've got so far:

Code:

```static void drawSquare(int n, double x, double y, double size){                 if(n <= 0){  // n is number of squares                                               // I use predefined class StdDraw                         StdDraw.setPenColor(StdDraw.GRAY);                         StdDraw.filledSquare(x, y, size);                         StdDraw.setPenColor(StdDraw.BLACK);                         StdDraw.square(x, y, size);                 }                 else{                                                 // x0, y0, x1, y1 - coordinates of the squares, produced after each recursive step                         x0 = x/2;                         y0 = y/2;                         x1 = x + x/2;                         y1 = y + y/2;                         size = size/2.2;                                         }         }```
I haven't included the recursive calls in the else statement because they don't make sense.

The pseudocode would be something like:

Code:

```draw the first big square; change x, y, size; draw first smaller square; change x, y, size; draw second smaller square; change x, y, size; draw third smaller square; change x, y, size; draw fourth smaller square; . . . so on```
but I am not able to put this pseudodoce into real recursion and actually I am not sure if this would be the proper sequence of steps to draw the squares.

Any ideas or suggestions are greatly appreciated.