Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,615 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 982 people online right now. Registration is fast and FREE... Join Now!




Recursion Problem

 
Reply to this topicStart new topic

Recursion Problem

gormanator92
post 12 Oct, 2008 - 06:44 AM
Post #1


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 16

I am starting to deal with recursion, and am stuck between two decisions of what the output for this would be.
Keep in mind that it is not a program meant to run, just an example that we are supposed to figure out the output for the number 4. It is kind of psuedocode.

CODE

    public void mystery( int n ){
        if( n <= 0 )
            return;
            
        mystery( n-1 );
        
        for( int i = 0; i < n; i++ ){
            System.out.print( "-" );
        }
        
        for( int i = 0; i < n; i++ ){
            System.out.print( "+" );
        }
        System.out.prinln();
            
    }


Oh yea, I think this code either displays nothing,
or it displays this
-+
--++
---+++
----++++

This post has been edited by gormanator92: 12 Oct, 2008 - 06:47 AM
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 12 Oct, 2008 - 06:56 AM
Post #2


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


You're absolutely right.

It displays nothing if n < 1
else it will display equally many -'s and +'s exactly in the way you displayed them, the number of lines depending on the input of course.

This post has been edited by Gloin: 12 Oct, 2008 - 07:00 AM
User is offlineProfile CardPM

Go to the top of the page

gormanator92
post 12 Oct, 2008 - 06:57 AM
Post #3


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 16

QUOTE(Gloin @ 12 Oct, 2008 - 07:56 AM) *

You're absolutely right.

Which one is right?
User is offlineProfile CardPM

Go to the top of the page

pbl
post 12 Oct, 2008 - 09:42 AM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 189 times

Dream Kudos: 75
My Contributions


Should produce for mystery(4);

-+
--++
---+++
----++++
User is offlineProfile CardPM

Go to the top of the page

bbq
post 12 Oct, 2008 - 07:19 PM
Post #5


D.I.C Head

Group Icon
Joined: 15 May, 2008
Posts: 185



Thanked 15 times

Dream Kudos: 50
My Contributions


You can always test it.

java

class Test
{
public void mystery( int n )
{
if( n <= 0 )
return;

mystery( n-1 );

for( int i = 0; i < n; i++ )
{
System.out.print( "-" );
}

for( int i = 0; i < n; i++ )
{
System.out.print( "+" );
}

System.out.println();
}

public static void main(String[] args)
{
Test myTest = new Test();
myTest.mystery(4);
System.out.println();
myTest.mystery(10);
}

}


This post has been edited by bbq: 12 Oct, 2008 - 07:24 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:57AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month