- #1
mathrocks
- 106
- 0
I'll be extremely happy if anyone can tell me how the following program works. I understand that it prints the numbers in the array in reverse order, but I have no idea how it does this. It enters the if statement with startIndex=0, calls someFunction again and increases it by one until it hits 10 and exits and prints b[10] but doesn't it keep going through the loop with startIndex=0 and going up until it hits 10?? I don't see how it can decrease and print b[9]...down to b[0]??
Any help would be appreciated
#include <stdio.h>
#define SIZE 10
/* function prototype */
void someFunction( const int b[], int startIndex, int size );
/* function main begins program execution */
int main()
{
int a[ SIZE ] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5 }; /* initialize a */
printf( "Answer is:\n" );
someFunction( a, 0, SIZE );
printf( "\n" );
return 0; /* indicates successful termination */
} /* end main */
/* What does this function do? */
void someFunction( const int b[], int startIndex, int size )
{
if ( startIndex < size ) {
someFunction( b, startIndex + 1, size );
printf( "%d ", b[ startIndex ] );
} /* end if */
} /* end function someFunction */
Any help would be appreciated
#include <stdio.h>
#define SIZE 10
/* function prototype */
void someFunction( const int b[], int startIndex, int size );
/* function main begins program execution */
int main()
{
int a[ SIZE ] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5 }; /* initialize a */
printf( "Answer is:\n" );
someFunction( a, 0, SIZE );
printf( "\n" );
return 0; /* indicates successful termination */
} /* end main */
/* What does this function do? */
void someFunction( const int b[], int startIndex, int size )
{
if ( startIndex < size ) {
someFunction( b, startIndex + 1, size );
printf( "%d ", b[ startIndex ] );
} /* end if */
} /* end function someFunction */