How to determine how many times recursion function has been called...where i should add count?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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'); }

12th Jun 2021, 6:26 PM
IRFAN SHAIKH
9 Answers
+ 2
And post the code
13th Jun 2021, 7:35 AM
IRFAN SHAIKH
+ 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?
12th Jun 2021, 6:38 PM
Schindlabua
Schindlabua - avatar
+ 1
Not working...take above code and edit
13th Jun 2021, 6:42 AM
IRFAN SHAIKH
+ 1
I have done that but its not working u try it with c
13th Jun 2021, 7:35 AM
IRFAN SHAIKH
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.
13th Jun 2021, 7:14 AM
Jan
Jan - avatar
0
But that tower hanoi has two recursion functions
13th Jun 2021, 7:45 AM
IRFAN SHAIKH
0
Try with that
13th Jun 2021, 7:45 AM
IRFAN SHAIKH
0
IRFAN SHAIKH Use Google, and you will find examples without using recursive functions.
13th Jun 2021, 8:40 AM
Jan
Jan - avatar
- 1
IRFAN SHAIKH Use Google, and you will find examples without using recursive functions.
14th Jun 2021, 5:34 AM
Derek Lo
Derek Lo - avatar