-
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..
-
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;
-
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
*
**
***
***
**
*
-
try putting the printAsterisks(n-1) after your for loop.
-
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();
}
}
}
-
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:
and
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.
-
 Originally Posted by masher
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:
and
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
-
Replies: 3
Last Post: 10-31-2006, 04:51 PM
-
By premartha in forum C++
Replies: 3
Last Post: 10-21-2005, 10:06 AM
-
Replies: 7
Last Post: 08-04-2005, 07:28 PM
-
By librypat in forum Java
Replies: 1
Last Post: 07-16-2002, 11:03 AM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|