# Hourglass using recursion

Printable View

• May 1st, 2012, 04:31 PM
nWeid1
Hourglass using recursion
I have to write a recursive program that makes an hourglass that suites the size of the user's input. For example, if the user types 4, this should print:

Code :

```* * * * * * * * * * * * * * * * * * *```

and so far I got this far:

* * * *
* * *
* *
*
* *
* * *
* * * *

so I just need to figure out how to get the spaces but I'm not sure how to do this. Thanks for the help.

Code :

```import java.util.InputMismatchException; import java.util.Scanner;   public class Hourglass {   public static void main(String[] args) {   Scanner console = new Scanner(System.in); System.out.println("Enter an integer greater than or equal to 1"); int n = 0; try { n = console.nextInt(); } catch (InputMismatchException e) { e.getMessage(); } if (n < 1) { System.out.println("Invalid input"); } else { System.out.println(""); hourglass(n, 0); System.out.println(""); }   }   public static void hourglass(int n, int count) { recursion(n, count); if(n > 1) { hourglass(n-1, count); recursion(n, count); } }   public static void recursion(int n, int count) { System.out.print("* "); if (n > 1) recursion(n - 1, count); else System.out.println(); } }```
• May 1st, 2012, 04:38 PM
andreas90
Re: Hourglass using recursion
Hello nWeid1!
I can't see the difference between the output as it should be and the one you actually get. What do you mean by "figure out how to get the spaces"?
• May 1st, 2012, 04:48 PM
nWeid1
Re: Hourglass using recursion
I just updated it. for some reason it won't display the spaces unless its
• May 1st, 2012, 05:02 PM
andreas90
Re: Hourglass using recursion
Where in your code do you have these spaces? I can only see two print statements in the recursion code -one for stars and the other for an empty line.
• May 1st, 2012, 05:07 PM
nWeid1
Re: Hourglass using recursion
Well, I don't. I don't know where to implement the code to have them print out properly which is my problem.
• May 1st, 2012, 05:15 PM
andreas90
Re: Hourglass using recursion
For this kind of problems you should write down the logic of your program and *test* it in a piece of paper before you implement code.
Also, what is the variable count? Did you think of a way to use it to accomplish what you want?
• May 1st, 2012, 05:35 PM
nWeid1
Re: Hourglass using recursion
Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.
• May 1st, 2012, 05:50 PM
andreas90
Re: Hourglass using recursion
Quote:

Originally Posted by nWeid1
Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.

You should try it and see what is happening. But again I suggest you writing down the logicof the program.
Hint: not sure if you are allowed to use them, but for loops is a very common approach in such kind of problems.
• May 1st, 2012, 05:57 PM
nWeid1
Re: Hourglass using recursion
Ok I'll try to write it down. And yeah my teacher said we can't use any for loops, hence the reason I'm using recursion in the first place
• May 1st, 2012, 06:56 PM
copeg
Re: Hourglass using recursion
This thread has been cross posted here:

Although cross posting is allowed, for everyone's benefit, please read:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• May 1st, 2012, 10:30 PM
Darryl.Burke
Re: Hourglass using recursion
One more: Hourglass Using Recursion - Java | Dream.In.Code

db

edit The OP is presently throwing a temper tantrum on the thread linked by copeg