2 Replies - 5017 Views - Last Post: 30 April 2012 - 04:50 AM Rate Topic: -----

#1 nweid1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-February 12

Hourglass using recursion

Posted 29 April 2012 - 10:59 PM

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:
* * * *
 * * *
  * *
   *
  * *
 * * *
* * * *


So far I am able to print the stars but I'm not sure how to deal with the spaces. My teacher said to use a count and I added it but I'm not sure how to get use it appropriately. Here is an example of what prints
* * * *
* * *
* *
*
* *
* * *
* * * *


and here is my 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);
		if(n > 1) {
			spaces(n, count);
			hourglass(n-1, count);
			recursion(n);
		}
	}

	public static void recursion(int n) {
		System.out.print("* ");
		if (n > 1)
			recursion(n - 1);
		else
			System.out.println();
	}
	
	public static void spaces(int count, int n) {
		if(count<n) {
			spaces(count-1, n);
		}
		System.out.print(" ");
	}
}



Thanks!

This post has been edited by baavgai: 30 April 2012 - 04:27 AM
Reason for edit:: tagged stars, because I couldn't tell the diff


Is This A Good Question/Topic? 0
  • +

Replies To: Hourglass using recursion

#2 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Hourglass using recursion

Posted 29 April 2012 - 11:39 PM

Easy solution is to every time you lose a star you add a space and every time you gain a star you lose a space. So for line 1, 0 spaces. line 2, 1 space. line 3 , 2 spaces etc
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Hourglass using recursion

Posted 30 April 2012 - 04:50 AM

Your code will give cleaner output, if still wrong, if you did this:
public static void main(String[] args) {
	hourglass(4, 0);
}

public static void hourglass(int n, int count) {
	if(n > 1) {
		recursion(n);
		hourglass(n-1, count);
	}
	recursion(n);
}



Note, you never use count? Indeed, you may not for the output you're currently getting. I bet it would be useful for each line...

Maybe:
public static void hourglass(int n) {
	hourglass(n, n);
}

public static void hourglass(int n, final int count) {
	if(n > 1) {
		recursion(n, count);
		hourglass(n-1, count);
	}
	recursion(n, count);
}




If you want to keep the recursion as sloppy loop thing going; make it generic:
public static void dup(int n, String s) {
	if (n > 0) { 
		System.out.print(s);
		dup(n-1, s);
	}
}



You'll want to call that twice, once for spaces with a count modifier. Once for stars. And then add a new line.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1