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
Post a Comment