Velocity Reviews > recursive function

# recursive function

fr3shman@gmail.com
Guest
Posts: n/a

 01-16-2005
------------------------------------------------------
main()
{
void kk();
kk(0);
getch();
}

void kk(j)
int j;
{
if (j<10)
{
j++;
printf("the number is:%d\n",j);
kk(j);
printf("the number is k%d\n",j);
}
}
-------------------------------------------------------
I thought the program would printf:
the number is:1
the number is:2
the number is:3
the number is:4
the number is:5
the number is:6
the number is:7
the number is:8
the number is:9
the number is:10
-------------------------------------------------------
but,the result is:
Invalid keyboard code specified
the number is:1
the number is:2
the number is:3
the number is:4
the number is:5
the number is:6
the number is:7
the number is:8
the number is:9
the number is:10
the number is k10
the number is k9
the number is k8
the number is k7
the number is k6
the number is k5
the number is k4
the number is k3
the number is k2
the number is k1
-----------------------------------------------------
why? thanks...
btw,sorry for my poor english.

dbtid
Guest
Posts: n/a

 01-16-2005
Well, it does do that, but that second printf is going to execute after
the recursive calls are completed.

Try tracing out the control flow on a piece of paper and I think you'll
learn a lot.

Martin Ambuhl
Guest
Posts: n/a

 01-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> ------------------------------------------------------
> main()
> {
> void kk();
> kk(0);
> getch();
> }
>
> void kk(j)
> int j;
> {
> if (j<10)
> {
> j++;
> printf("the number is:%d\n",j);
> kk(j);
> printf("the number is k%d\n",j);
> }
> }

Compare your code to the following:

#include <stdio.h>

void kk(int);

int main(void)
{
kk(0);
return 0;
}

void kk(int j)
{
if (j < 10) {
j++;
printf("the number is:%d\n", j);
kk(j);
}
}

the number is:1
the number is:2
the number is:3
the number is:4
the number is:5
the number is:6
the number is:7
the number is:8
the number is:9
the number is:10

Taran
Guest
Posts: n/a

 01-17-2005

(E-Mail Removed) wrote:
> ------------------------------------------------------
> main()
> {
> void kk();
> kk(0);
> getch();
> }
>
> void kk(j)
> int j;
> {
> if (j<10)
> {
> j++;
> printf("the number is:%d\n",j);
> kk(j);
> printf("the number is k%d\n",j);
> }
> }
> -------------------------------------------------------
> I thought the program would printf:
> the number is:1
> the number is:2
> the number is:3
> the number is:4
> the number is:5
> the number is:6
> the number is:7
> the number is:8
> the number is:9
> the number is:10
> -------------------------------------------------------
> but,the result is:
> Invalid keyboard code specified
> the number is:1
> the number is:2
> the number is:3
> the number is:4
> the number is:5
> the number is:6
> the number is:7
> the number is:8
> the number is:9
> the number is:10
> the number is k10
> the number is k9
> the number is k8
> the number is k7
> the number is k6
> the number is k5
> the number is k4
> the number is k3
> the number is k2
> the number is k1
> -----------------------------------------------------
> why? thanks...
> btw,sorry for my poor english.

The output is correct and outputs what your code commands it to do.

For the first call to kk at kk(0), j is incremented and the first
printf prints j value 1. There there's a call to kk(1), j is
incremented and pritnf prints 2 and so on. After some calls to kk where
kk(10) happens then if block is 'jumped over' and the recursive
funtions starts to roll back that's when the second printf prints
".....k9.." and so on.
Try doing doing a dry run, or control flow for some iteration with
/**/ if (j<3)
/**/ {
/**/ j++;

you'll understand it youself.

HTH.
Regards,
Taran
TT

Rufus V. Smith
Guest
Posts: n/a

 01-20-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> ------------------------------------------------------
> main()
> {
> void kk();
> kk(0);
> getch();
> }
>
> void kk(j)
> int j;
> {
> if (j<10)
> {
> j++;
> printf("the number is:%d\n",j);
> kk(j);
> printf("the number is k%d\n",j);
> }
> }
> -------------------------------------------------------
> I thought the program would printf:
> the number is:1
> the number is:2
> the number is:3
> the number is:4
> the number is:5
> the number is:6
> the number is:7
> the number is:8
> the number is:9
> the number is:10
> -------------------------------------------------------
> but,the result is:
> Invalid keyboard code specified
> the number is:1
> the number is:2
> the number is:3
> the number is:4
> the number is:5
> the number is:6
> the number is:7
> the number is:8
> the number is:9
> the number is:10
> the number is k10
> the number is k9
> the number is k8
> the number is k7
> the number is k6
> the number is k5
> the number is k4
> the number is k3
> the number is k2
> the number is k1
> -----------------------------------------------------
> why? thanks...
> btw,sorry for my poor english.
>

That is what you programmed! Why did you expect the results you listed?

Can't tell you why the keyboard code error came up, but the reason it's at
the beginning
is probably because you didn't flush stdout.

But take out your second printf line if you don't want those to print as
from those recursive calls.

perhaps you should have written kk() as:

void kk(j)
int j;
{
if (j<10)
{
j++;
printf("before recursive call. j=:%d\n",j);
kk(j);
printf("call returned, j=%d\n",j);
}
}

and perhaps you should do a fflush(stdout) before the getch();

Rufus