Thread: Pls explain how this program works...

  1. #1
    Unregistered
    Guest

    Pls explain how this program works...

    Pls explain how it works...
    Hi,

    This is a program from my book. But no matter how i look at it i jsut couldn't figure out how it works. I think the purpose of it is to reverse the order of an array of numbers. But if possible, pls explain the program line by line, it's very short. But skip lines like #include and int main().

    Thnx in advance.


    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void someFunction( const int [], int );
    
    int main()
    {
       int a[ SIZE ] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5 };
    
       printf( "Answer is:\n" );
       someFunction( a, SIZE );
       printf( "\n" );
       return 0;
    }
    
    void someFunction( const int b[], int size )
    {
       if ( size > 0 ) {
          someFunction( &b[ 1 ], size - 1 );
          printf( "%d  ", b[ 0 ] );
       }
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    lets assume you know everything except the recursive functon....

    void someFunction( const int b[], int size )
    {
    if ( size > 0 ) {
    someFunction( &b[ 1 ], size - 1 );
    printf( "%d ", b[ 0 ] );
    }
    }

    ok lets look at this a little. First off the parameters for this function are a 1d array of constant ints, and an int size. The array is actually passed as a pointer to the first element of the array. Understand that and you will understand the code...

    we will shorten it a little.
    imagine we pass array[3],size 3

    if size > 0 ..... yes so we do ....
    somefunc( &array[1], size 2)

    if size > 0 ...... yes so we do...
    somefunc(&array[2] (this is the clever bit.... we already passed a smaller array and now smaller again. &array[2] because its &x[1] of the previous smaller array),size 1)

    if size > 0...... yes so we do....
    somefunc(&array[3] (as before smaller array still),size 0)

    if size >0 ..... no so return
    print first element of array (that is array[3])
    return

    print first element of array (that is array[2])
    return

    print first element of array (that is array[1])
    return

    print first element of array (that is array [0])
    return

    result printed array in reverse.

    understanding recursion is hard like this. You would be better stepping through it line by line in a debugger and so you can see exactly what happens with your own eyes.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    Ok, thnx for explaining, but there are just 2 more things i need to get clear. How come the ampersand '&' is needed in front of 'b'. And also, whats the purpose of &b[ 1 ] existing.

    thnx very much

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    & is the address of operator. It is needed because you are passing a pointer. without it you would be passing the value stored in b[1]. For the second question all i can say is watch the code run in a debugger. Step through it line by line and watch how it works. When you have seen it work yourself line by line you will almost certainly start to understand how it works. Recursion is not too complex but to the untrained eye it can be hard to follow. This is a reasonably simple example of recursion. Follow it through in your debugger. step through it and all will become clearer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    the problem is, i don't have a debugger, only a simple compiler...

  6. #6
    Unregistered
    Guest
    also, which line actually changes the size of the function? The variable size only changes the value of the variable isn't it?

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Unregistered
    the problem is, i don't have a debugger, only a simple compiler...
    Are you sure? Which compiler are you using? I always thought they all had debuggers. Hmmm...

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    Unregistered
    Guest
    i am using pacific c compiler, but i will get borland c++ builder soon

    Also, which line actually changes the size of the function? The variable size only changes the value of the variable isn't it?

    thnx

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    as a stopgap i suggest you download the reasonable dev-c from www.bloodshed.net. Then you will be able to step through the code and you will see how it works. It is very difficult for me to show you here what your debugger can show you right in front of your eyes. It is imperative to have a good compiler with a debugger for learning c/c++.There are a few good free ones. Borland have a command line compiler available for free. DJGPP is easily found with a search at google.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    void someFunction( const int b[], int size )
    {
    if ( size > 0 ) {
    someFunction( &b[ 1 ], size - 1 );
    printf( "%d ", b[ 0 ] );
    }
    }

    ok one last attempt at helping you understand this. we will use the array[3] size 3 example again for simplicity.

    say we have an array[3] whose address in memory is 0xFFFF0000

    the first time the function is called the if condition is met so somefunction is called again with (&b[1],2)
    lets look at &b[1]...... we know that &b[0] is 0xFFFF0000 so
    &b[1] is 0xFFFF0004 (ints are 4 bytes remember)

    so in reality we are calling somefunc like this....

    someFunction(0xFFFF0004,2)

    this again meets the if condition and calls somefunc again.
    now the array b starts at 0xFFFF0004 so this time &b[1] is....
    0xFFFF0008 so we call the func in reality like this...

    someFunction(0xFFFF0008,1)

    this again meets the if condition so we call somefunc again.
    now the array b starts at 0xFFFF0008 so this time &b[1] is ....
    0xFFFF000C so in reality we call the func like this....

    someFunction(0xFFFF000C,0)
    this does not meet the if condition so the function returns to this line....
    printf( "%d ", b[ 0 ] );
    now in this instance &b[0] is 0xFFFF000C so the contents of that memory location gets printed.
    return again to the same line but this time
    &b[0] is 0xFFFF0008
    print the contents of that memory location.Then return to the same line but this time &b[0] is 0xFFFF0004 so the contents of that memory location is printed.
    return to the same line again only this time &b[0] is 0xFFFF0000 so the contents of that memory location is printed.
    the function then returns to its caller.

    Do you see now whats going on? can you see how it prints the whole array in reverse?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2009, 08:51 PM
  2. Pls help me on this program
    By Lopen007 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2008, 04:14 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Program works under C++ but not under C
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 10-13-2004, 04:36 AM
  5. Random numbers works in one program, not in another
    By Shadow12345 in forum C++ Programming
    Replies: 27
    Last Post: 09-30-2002, 04:06 PM