Confused

This is a discussion on Confused within the C Programming forums, part of the General Programming Boards category; Hi, Below i have pasted the code, now i understand that this is a recursive algorithm however when i step ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Confused

    Hi,

    Below i have pasted the code, now i understand that this is a recursive algorithm however when i step through this im really confused .

    I understand that the Scale (5) line states the starting number for the value of 'n' once this is found the program then enters the first if statement, checks to see if its greater than 1 if it is it then minuses 1 from n's value and repeats this until n is not greater than 1 or is equal to 1.

    From that point it then moves forward to the if n ==1 line where it prints a single dash and line space. After that it then enters the FOR loop. - This is where i get confused

    the counter within the FOR loop is set to 0 if the counter is less than the value of N it then increments the counter by 1 and again prints a single dash followed by a line space.

    it then steps onto the scale (n - 1) line this then minuses 1 from the value of n making it zero.

    N = 0 at this point the program returns to the first if statement to check it n > 1 which is isnt and moves into the n == 1 statement and prints a single dashed line.

    the counter and n are both equal to zero this stage are they or arent they? shouldnt the program stop there?

    i know the actual outcome but cant get my head round why its like that?

    the output is beneath the code :S please help

    Code:
    void scale( int n )
    {
    int counter;
    
    if ( n > 1 )
    {
    scale( n - 1 );
    
    for ( counter = 0; counter < n; counter++ ) printf( "-" );
    printf( "\n" );
    
    scale( n - 1 );
    }
    else
    {
    if ( n == 1 ) printf( "-\n" );
    }
    }
    
    int main( void )
    {
    printf( "\n" );
    scale( 5 );
    }

    -
    --
    -
    ---
    -
    --
    -
    ----
    -
    --
    -
    ---
    -
    --
    -
    -----
    -
    --
    -
    ---
    -
    --
    -
    ----
    -
    --
    -
    ---
    -
    --
    -
    Last edited by jeev2005; 05-29-2006 at 01:02 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,040
    You should draw everything out on a peice of paper to figure out what's going on.

    Remmember that every call to scale() has it's own 'n'.


    I'll take you through the first few calls to scale():

    1)scale(5) is called. Becouse 5>1, scale(5-1) is called.
    2)scale(4) is called. As before scale(4-1) is called.
    . . .
    5)scale(1) is finally called. This prints "-\n" and returns to scale(2)
    6)scale(2) gose through the for loop, printing two dashes.
    7)scale(2) calls scale(1). This prints "-\n" and returns to scale(2)
    8)scale(2) is finished. it returns to scale(3)
    9)scale(3) gose through the for loop, printing three dashes.
    . . .
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    thats where i was getting confused ok so once it fulfils the criteria it returns to 2...... why is this is this just the way the recursion works?

    Really Appreciate you response!

    Thanks

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Well scale(2) is waiting for scale(1) to complete, and it can only proceed once it is completed. So once scale(1) completes, it returns to scale(2) which can then proceed. After scale(2) would complete, scale(3), which had been waiting for scale(2) to complete in order to proceed, can continue, and so on...

    Make any sense ?

    And yes, without going into the beefy details, that is how recursion works.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Yes kind of , i will post tomorrow as i must rest now. Thanks for you help..... if i struggle to understand this any further i will post again......

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,040
    Honestly, I'm not kidding, you should draw it out on paper.

    When a function makes a call to another function, it stops and waits for that function to return. Then it continues where it left off, replaning the function call with the returned value or void.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 04:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21