Results 1 to 6 of 6
  1. #1
    kanika is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Lightbulb recursion question

    void printArray(int i)
    {
    if(i==0) return;

    else printArray(i-1);
    System.out.println("["+(i-1+"]"+values[i-1]));


    }
    explain above code for i =1 value

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: recursion question

    Please go through BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    Quote Originally Posted by kanika View Post
    explain above code for i =1 value
    You first. Tell us what you think it does.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: recursion question

    What do you think happens?
    Have you tried to step through it, say on a piece of paper?

    ETA: And just exactly how slow am I?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    kanika is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Default Re: recursion question

    Quote Originally Posted by kanika View Post
    void printArray(int i)
    {
    if(i==0) return;

    else printArray(i-1);
    System.out.println("["+(i-1+"]"+values[i-1]));


    }
    explain above code for i =1 value
    the output is [0]0
    but i want to know what happens after where the control go after
    else printArray(i-1);
    and when i==0
    where the control goes

    the full program is

    public class RecTest
    {
    int values[];
    RecTest(int i)
    {
    values=new int[i];
    }
    void printArray(int i)
    {

    if(i==0) return;


    else printArray(i-1);
    System.out.println("["+(i-1+"]"+values[i-1]));



    }


    }
    class Recursion2
    {
    public static void main(String args[])
    {
    RecTest ob=new RecTest(10);
    int i;
    for(i=0;i<10;i++)
    ob.values[i]=i;
    ob.printArray(10);
    }
    }


    the output is
    [0]0
    [1]1
    [2]2
    [3]3
    [4]4
    [5]5
    [6]6
    [7]7
    [8]8
    [9]9

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: recursion question

    Darryl posted a link for BBCodes.
    You need to use the code tag to format your code, as that lot is hard to read.

    And you need to tell us what you think happens, not simply ask the same question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: recursion question

    I normally explain recursion with my 'schizophrenic me' example, so here goes: if someone asks me to recursively print out n numbers from an array, I brag that I can do it; I do know that nothing needs to be printed if n == 0 and I can print one number (the last one): array[n-1]. For the rest I ask my clever (but shy) friend to do the job; this is what it looks like:

    Java Code:
    void me(int[] array, int n) { // this is me, bragging
       if (n > 0) {
          friend(array, n-1); // he does the  hard work
          System.out.println(array[n-1]); // I do the last number
       }
    }
    But sadly enough I don't have a friend; it's purely my imagination; my friend is me, so:

    Java Code:
    void me(int[] array, int n) { // this is me, bragging
       if (n > 0) {
          me(array, n-1); // he^H^HI does^H^H the  hard work
          System.out.println(array[n-1]); // I do the last number
       }
    }
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Recursion: Fibonacci Number Question
    By judemartin99 in forum New To Java
    Replies: 10
    Last Post: 10-06-2013, 05:36 PM
  2. Recursion question?
    By kraigballa in forum New To Java
    Replies: 2
    Last Post: 03-23-2012, 05:55 PM
  3. Again recursion question
    By luke in forum New To Java
    Replies: 5
    Last Post: 10-05-2010, 12:02 AM
  4. Recursion question
    By luke in forum New To Java
    Replies: 12
    Last Post: 10-04-2010, 10:04 PM
  5. Touch recursion question
    By myst in forum New To Java
    Replies: 18
    Last Post: 06-08-2010, 06:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •