0
How to determine how many times recursion function has been called...where i should add count??
#include<stdio.h> void TOH(int n,char x,char y,char z) { if(n>0) { TOH(n-1,x,z,y); printf("\n%c to %c",x,y); TOH(n-1,z,y,x); } } int main() { int n=3; TOH(n,'A','B','C'); }
9 Respuestas
+ 2
And post the code
+ 1
How often TOH runs depends on `n`. Try to figure out a pattern, starting from the simplest case:
TOH(0) runs once, no recursion happens.
TOH(1) runs once, then recurses twice into TOH(0), which run 1 time each.
TOH(1) = 1 + 2*TOH(0) = 1 + 2*1 = 3.
TOH(2) runs once, the recurses twice into TOH(1).
TOH(2) = 1 + 2*TOH(1) = 1 + 2*3 = 7.
TOH(3) runs once, then recurses twice into TOH(2).
TOH(3) = 1 + 2*TOH(2) = 1 + 2*7 = 15.
See where this is going? Can you find a general formula?
+ 1
Not working...take above code and edit
+ 1
I have done that but its not working u try it with c
0
Basically, you should avoid using recursive functions. The problem with those functions are that they use all the resources on a system, and in the end, the system will crash.
0
But that tower hanoi has two recursion functions
0
Try with that
0
IRFAN SHAIKH Use Google, and you will find examples without using recursive functions.
- 1
IRFAN SHAIKH Use Google, and you will find examples without using recursive functions.