# Pls explain how this program works...

• 01-04-2002
Unregistered
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().

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 ] );   } }```
• 01-04-2002
Stoned_Coder
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.
• 01-04-2002
Unregistered
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
• 01-04-2002
Stoned_Coder
& 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.
• 01-04-2002
Unregistered
the problem is, i don't have a debugger, only a simple compiler...
• 01-04-2002
Unregistered
also, which line actually changes the size of the function? The variable size only changes the value of the variable isn't it?
• 01-04-2002
Garfield
Quote:

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
• 01-04-2002
Unregistered
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
• 01-05-2002
Stoned_Coder
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.
• 01-05-2002
Stoned_Coder
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?