Recursion assignment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Recursion assignment

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    11

    Recursion assignment

    I have an assignment where i have to use a recursion method to print a pattern of asterisks.

    Write a program, called RecursiveAsterisks, that has a recursive method called printAsterisk that takes an integer n as a parameter. The method should print a pattern of 2n lines of asterisks. The first line contains one asterisk, the next line contains 2, and so on up to the nth line which contains n asterisks. Line number n+1 again contains n asterisks, the next line has n-1 asterisks, and so on until line number 2n which has just one asterisk. Your main method should ask the user for the value of n. Below is an example of what would be displayed by the call printAsterisk(4).

    *
    **
    ***
    ****
    ****
    ***
    **
    How would i make it so that what ever n is currently equal to it prints that many asterisks?

    Im pretty lost with recursion so..

  2. #2
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    recursion is:

    Recursion is to call yourself over and over.
    In your case you have to println(*);
    for int i = 0 til i = n; i++
    println;
    It is not exactly but something like that.
    Good luck;

  3. #3
    Join Date
    Oct 2006
    Posts
    11
    Okay i figured out how to make it print the first part but how do i make it go back?

    Code:
    import java.util.Scanner;
    
    public class RecursiveAsterisks 
    {
    	public static void main (String[] args)
    	{
    		
    		Scanner reader = new Scanner(System.in);
    		int n;
    		int num;
    		
    		System.out.println("Enter a number: ");
    		n = reader.nextInt();
    		
    		
    		printAsterisks(n);
    		
    	}
    	public static void printAsterisks(int n)
    	{
    		if(n == 0)
    			return;
    		
    		if(n > 0) {
    			printAsterisks(n - 1);
    			for( int i = 0; i < n; i++)
    				System.out.print("*");
    			System.out.println();
    		}
    		
    		
    	}
    }
    liek rite now it will only print
    *
    **
    ***

    But i need it to print
    *
    **
    ***
    ***
    **
    *

  4. #4
    Join Date
    Jul 2005
    Posts
    78
    try putting the printAsterisks(n-1) after your for loop.

  5. #5
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Code:
    /**
     * Write a description of class aaaa here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    public class RecursiveAsterisks 
    {
    	public static void main (String[] args)
    	{
    		
    		Scanner reader = new Scanner(System.in);
    		int n;
    		
    		System.out.println("Enter a number: ");
    		n = reader.nextInt();
    		for( int i = 0; i < (n+1); i++)
    		printAsterisks(i );
    		
    		for( int i = (n); i > 0; i--)
    		printAsterisks(i );
    	}
    		
    	public static void printAsterisks(int n)
    	{
    		
    		if(n > 0) {
    		   
    			for( int i = n; i >0; i--)
    			
    				System.out.print("*");
    			System.out.println();
    		}
    		
    		
    	}
    }

  6. #6
    Join Date
    Jul 2005
    Posts
    78
    Is that your program?

    It's not recursive. The first program you posted is recursive.

    A recursive method is one that calls itself. If you just call your printAsterisks(n) method, then it will print a single line of asterisks.

    I've tried writing some code to solve this, and I can get asterisks like:
    Code:
    *
    **
    ***
    and
    Code:
    ***
    **
    *
    but I haven't been able to get the double one specified in a single method.

    I can fudge it by writing one method that does the increasing one, a second method that does a decreasing one and a third method that calls the first then second. It works, but I think it's outside the intent of the question.


    Hint:

    This is yours:
    Code:
    if(n > 0) 
    {
       printAsterisks(n - 1);
       for( int i = 0; i < n; i++)
          System.out.print("*");
       System.out.println();
    }
    have a look at this
    Code:
    if(n > 0) 
    {
       for( int i = 0; i < n; i++)
          System.out.print("*");
       System.out.println();
       printAsterisks(n - 1);
    }
    Last edited by masher; 03-21-2007 at 08:04 PM.

  7. #7
    Join Date
    Feb 2014
    Posts
    1
    Quote Originally Posted by masher View Post
    Is that your program?

    It's not recursive. The first program you posted is recursive.

    A recursive method is one that calls itself. If you just call your printAsterisks(n) method, then it will print a single line of asterisks.

    I've tried writing some code to solve this, and I can get asterisks like:
    Code:
    *
    **
    ***
    and
    Code:
    ***
    **
    *
    but I haven't been able to get the double one specified in a single method.

    I can fudge it by writing one method that does the increasing one, a second method that does a decreasing one and a third method that calls the first then second. It works, but I think it's outside the intent of the question.


    Hint:

    This is yours:
    Code:
    if(n > 0) 
    {
       printAsterisks(n - 1);
       for( int i = 0; i < n; i++)
          System.out.print("*");
       System.out.println();
    }
    have a look at this
    Code:
    if(n > 0) 
    {
       for( int i = 0; i < n; i++)
          System.out.print("*");
       System.out.println();
       printAsterisks(n - 1);
    }
    I have had a look at these and still scratching my head as to how to combine them into one method. Any help on this would be greatly appreciated.

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2006, 04:51 PM
  2. Replies: 3
    Last Post: 10-21-2005, 10:06 AM
  3. Replies: 7
    Last Post: 08-04-2005, 07:28 PM
  4. java assignment array
    By librypat in forum Java
    Replies: 1
    Last Post: 07-16-2002, 11:03 AM
  5. help regarding java assignment
    By abbas in forum Java
    Replies: 0
    Last Post: 12-12-2001, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center