5 Replies - 8380 Views - Last Post: 26 February 2012 - 09:05 AM Rate Topic: -----

#1 noobcode   User is offline

  • New D.I.C Head

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

Triangle recursion

Posted 25 February 2012 - 10:23 PM

So i have a program in which (with recursion) i am supposed to take a user input (int) and draw a backward and then forward triangle according to the number... ex below

User input: 4 (ignore the extra spaces inbetween each,, there should be no extra line inbetween) Result: XXXX

XXX

XX

X

X

XX

XXX

XXXX

I have created a method in which i think should do it but I am having a problem wiht it

This si the code I have so far

n is the input number z is a counter to try and go between making a backwards/ forwards triangle
 public static String drawLine(int n, int z){

            if(n!=0&& z<(n+1)){
                for(int i=1;i<=n;i++)
                    System.out.print("X");
                System.out.println();
                z+=1;
                drawLine(n-1,z);

                return"";
            } 
            else{
            if( z==(2*n+1) ) return "";
            z+=1;
            String p = drawLine(n - 1,z);
            p = p + "X";
            System.out.println(p);


           return p;    }

            }



the error message is that there is some infinit loop or an over flow, but I cannot find the error....please help!!

Is This A Good Question/Topic? 0
  • +

Replies To: Triangle recursion

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Triangle recursion

Posted 26 February 2012 - 03:04 AM

I appreciate your economic use of limited resources, but you haven't provided enough information. Post the exact error message (copied and pasted) and the code which might be causing the error. If you have isolated the error to the code you posted, provide some explanation of what that code is doing: explain the concept of your algorithm; the arguments and variables in the code, and how the code is intended to implement your algorithm.

In general, a recursive algorithm requires a "base case" to know when to stop. The base case is not defined in what you've posted, so you're likely in an infinite recursion.
Was This Post Helpful? 0
  • +
  • -

#3 noobcode   User is offline

  • New D.I.C Head

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

Re: Triangle recursion

Posted 26 February 2012 - 07:37 AM

here is the error message
Exception in thread "main" java.lang.StackOverflowError
at AllLabs.drawLine(AllLabs.java:3)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
at AllLabs.drawLine(AllLabs.java:15)
(and so on)

it repeats that last error message alot implying that there is an infinite loop going on
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Triangle recursion

Posted 26 February 2012 - 07:43 AM

Yep, as suspected, your recursion doesn't know when to end. What do you think the "base case" should be? What will be the signal that the recursion can stop?
Was This Post Helpful? 0
  • +
  • -

#5 noobcode   User is offline

  • New D.I.C Head

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

Re: Triangle recursion

Posted 26 February 2012 - 07:53 AM

I thought I had already made a base case that works though...

my base case is that n!=0 for it to stop drawing the "backwards triangle" and I have the z<(n+1) so that it will know to go on to drawing the forward triangle...isn't that my base case..and I even put a base case for the "forward triangle" with the if(z==(2*n+1)...so I'm not exactly sure why the erors are poping up
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Triangle recursion

Posted 26 February 2012 - 09:05 AM

Your approach is more complicated that it needs to be, and I'd rather suggest a simplified approach than fix yours.

I created the code below to suggest a possible answer without giving it to you directly. Note that my version counts down from the user's input to 1 and then counts back up. You can use the same approach and the 'count' to drive a for loop that prints the desired number of Xs in your triangle.

Let me know if you have any questions.
import java.util.Scanner;

public class TestProject2
{
    //Create a Scanner object for keyboard input.
    static Scanner keyboard = new Scanner(System.in);

    // the number to start count from
    static int size;

    // the direction of the recursion
    static boolean decrease = true;

    //Set variables
    public static void main(String[] args)
    {
        // get the starting count
        System.out.println( "Start counting from?: " );

        size = keyboard.nextInt();

        countDownCountUp( size );
        
    } // end method main()

    // method countDownCountUp() accepts the user's input, size,
    // as the starting point, counts down from size to 1 and then
    // counts up to size, inclusive
    public static void countDownCountUp( int n )
    {
        // the return signal is one more than the starting point
        if ( n == size + 1 )
            return;

        // print n if larger than 0
        if ( n > 0 )
        {
            System.out.println( "" + n );
        }

        // if n == 0, don't decrease n further
        if( n == 0 )
        {
            decrease = false;
        }

        if ( decrease )
        {
            countDownCountUp( n - 1 );
        } 
        else
        {
            countDownCountUp( n + 1 );
        }
        
    } // end method countDownCountUp()

} // end class TestProject2

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1