int findpow(int n1,int k, int count){ //while calling, k=1, count=0 if(k<n1) return findpow(n1,k*2,count+1); if(k==n1) return count; if(k>n1) return --count; }
this function returns largest power of 2 less n. when run in ubuntu terminal (g++ 4.8.4), works fine. when running on www.hackerrank.com, gives error(control reaches end of non void function). problem is, participate in many contests on website , have come across problem multiple times.
please tell if know how can fix it.
you can use else if
statement this:
int findpow(int n1,int k, int count){ //while calling, k=1, count=0 if(k<n1) return findpow(n1,k*2,count+1); else if(k==n1) return count; else // eliminate compiler errors (warnings) return --count; }
or said @juanchopanza:
int findpow(int n1,int k, int count){ //while calling, k=1, count=0 if(k<n1) return findpow(n1,k*2,count+1); if(k==n1) return count; // eliminate compiler errors (warnings) return --count; }
it same thing code, not give doubt compiler can no return points function.
Comments
Post a Comment