0

I'm trying to solve this problem but in stuck in converting my while loop into recursion
Ive managed to implement the printMany function as follows

public static void printMany(int count, String s){  
  if(count >= 1) {
    System.out.print(s);
    printMany(count-1, s);
  }
}   

But the current implementation of the hourglass method still uses loops though it displays the correct output.

public static void hourglass(int numberOfStars, int numberOfSpaces){
  while(numberOfStars>0){
    printMany(numberOfSpaces++, " ");
    printMany(numberOfStars--, "X ");
    System.out.println();
  }  

  numberOfSpaces -=2;;
  numberOfStars += 2;

  while(numberOfSpaces>=0){
    printMany(numberOfSpaces--, " ");
    printMany(numberOfStars++, "X ");
    System.out.println(); 
  }
}  

I want to ask, how can I convert this while loop into a recursive call?

1

5 Answers 5

1

I'm not just going to give you the answer, but I'll try to help you along. If you want to break this down using recursion and without loops, the key really is to figure out what the parameters of your recursive helper function must be. It seems like you will always need to remember the original user input (to know how many spaces to print out and to know when to stop the recursion), the current number of stars you're on, and whether you're on the top half of the pyramid or the bottom half. Given all of that information, you should be able to do two things. First, you should be able to correctly print out a line. Second, you should be able to determine what the next line should be. Given this, you can print and recurse, stopping once your base case is reached.

Sign up to request clarification or add additional context in comments.

Comments

0

This is one possible solution:

public static void printMany(int count, String s) {
    if (count == 0)
        return;

    System.out.print(s);
    printMany(count - 1, s);
}

public static void upperhalf(int count, int max) {
    if (count == 0)
        return;

    printMany(max - count, " ");
    printMany(count, "* ");

    System.out.println();

    upperhalf(count - 1, max);
}

public static void lowerhalf(int count, int max) {
    if (count == max)
        return;

    printMany(max - count - 1, " ");
    printMany(count + 1, "* ");

    System.out.println();

    lowerhalf(count + 1, max);
}

public static void hourglass(int n) {
    upperhalf(n, n);
    lowerhalf(0, n);
}

Calling hourglass(1); within e.g. main results to:

*
*

So hourglass(2); prints:

* * 
 *
 *
* * 

And so on...

Comments

0

Instead of a while looping your program you call the same function.

public static void hourglass(int numberOfStars, int numberOfSpaces){
    hourglass(numberOfStars, numberOfSpaces, false);
}

private static void hourglass(int numberOfStars, int numberOfSpaces, boolean dir){
    if(dir==false && numberOfStars > 0){
        printMany(numberOfSpaces, " ");
        printMany(numberOfStars, "X ");
        System.out.println();
        hourglass(--numberOfStars, ++numberOfSpaces, false);
        return;
    }

    if(numberOfStars==0){
        numberOfSpaces -=2;
        numberOfStars += 2;
    }

    if(numberOfSpaces>0){
        printMany(numberOfSpaces, " ");
        printMany(numberOfStars, "X ");
        System.out.println();
        hourglass(++numberOfStars, --numberOfSpaces, true);
    }
}

Comments

0

Here's a piece of code for you. Used symmetrical padding.

public class Hourglass {

    public static void printRow(int nStars, int padding) {
        if (nStars == 0)
            return;

        if (padding > 0) {
            System.out.print(" ");
            printRow(nStars, padding - 1);
            System.out.print(" ");
            return;
        } 

        if (nStars <= 0) 
            return;

        System.out.print("*");
        if (nStars > 1) {
            System.out.print(" ");
            printRow(nStars - 1, padding);
        }
    }

    public static void printTop (int height, int padding) {
        if (height == 0)
            return;

        printRow(height,padding);
        System.out.print("\n");
        printTop(height - 1, padding + 1);
    }

    public static void printBottom(int currentHeight, int height, int padding) {
        printRow(currentHeight, padding);
        System.out.print("\n");
        if (currentHeight < height)
            printBottom(currentHeight + 1, height, padding - 1);
    }

    public static void printHourglass(int height) {
        if (height <= 0)
            throw new IllegalArgumentException();

        printTop(height, 0);
        printBottom(1, height, height - 1);
    }

    public static void main(String[] args) {
        printHourglass(5);
    }
}

Comments

0

Just split that method into two, here's another recursion solution for your question:

public static void hourglass(int numberOfStars, int numberOfSpaces) {
    if(numberOfStars== 0) return;

    printMany(numberOfSpaces++, " ");
     printMany(numberOfStars--, "X ");
     System.out.println();
     hourglass(numberOfStars,numberOfSpaces);  
     numberOfSpaces -=2;
     numberOfStars += 2;
         if(numberOfStars==2)
     hourglassBottom(numberOfStars,numberOfSpaces);

}
public static void hourglassBottom(int numberOfStars, int numberOfSpaces){
    if(numberOfSpaces==0 )return;

     printMany(numberOfSpaces--, " ");
        printMany(numberOfStars++, "X ");
        System.out.println(); 
        hourglassBottom(numberOfStars,numberOfSpaces);  

}

For example, running hourglass(3,1); would give you following:

 X X X 
  X X 
   X 
  X X 
 X X X 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.