recursion - How to calculate the number of recursive calls made to the Ackerman() function in C -


i tried writing code calculate ackerman value , number of times function called. however, counter stuck @ 0 time. me out?

/* a(m,n) =    n+1, if m==0 a(m,n) =    a(m-1,1), if m>0 , n==0 a(m,n) =    a(m-1,a(m,n-1)), if m>0 , n>0 */ #include<stdio.h> static int w=0; int ackerman(int m,int n) {      w=w+1;     if(m==0)         return n+1;     else if(m>0 && n==0)         return ackerman(m-1,1);     else if(m>0 && n>0)         return ackerman(m-1,ackerman(m,n-1)); } int mainackerman() {     int m,n;     scanf("%d %d",&m,&n);     printf("%d %d",ackerman(m,n),w);     return 0; } 

you have sequence-point issue. on same line calling ackerman, using value affected call. undefined behaviour. instead:

int result = ackerman(m,n); printf("%d %d", result, w); 

there good question on stackoverflow excellent answers related sequence-points. it's related c++, idea same c.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -