Advertisement
KAR98S

Recursion Visualization.c

Mar 17th, 2021
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. /*      *visualising recusrion*
  2. * ShowFun() keeps track of the recursion
  3. * and visualises the calls/returns
  4. * accordingly. You can visualise any
  5. * kind of recursion function, just call
  6. * ShowFun() with valid arguments at entry
  7. * and exit
  8. */
  9. #include <stdio.h>
  10.  
  11. void ShowFun(char*,int,char);
  12.  
  13. //test recursion function
  14. void f(int n) {
  15.     //enter
  16.     ShowFun("Enter", n, '+');
  17.     if (n <= 0) {
  18.         //exit-conditional
  19.         ShowFun("Exir-Conditional", n, '-');
  20.         return;
  21.     }
  22.     else {
  23.         f(n - 1);
  24.         f(n - 1);
  25.     }
  26.     //exit-default
  27.     ShowFun("Exit-Default", n, '-');
  28. }
  29.  
  30. int main()
  31. {
  32.     f(3);
  33.     return 0;
  34. }
  35.  
  36. void ShowFun(char* str, int n, char op) {
  37.     static int i = 0;
  38.  
  39.     if (op == '+') ++i;
  40.     for (int j = 1; j < i; j++)printf("|\t");
  41.     printf("%s f(%d): %d\n", str, n, i);
  42.     for (int j = 1; j < i; j++)printf("|\t");
  43.     printf("\n");
  44.  
  45.     if (op == '-') --i;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement