1

When I try to execute this:

#include<stdio.h>

int byteland(int a)
{
   int e,f,g;
   if ((a/2 + a/3 + a/4) < a)
      return a;
   else
   {
      e = byteland(a/2);
      f = byteland(a/3);
      g = byteland(a/4);
      return e + f + g;
   }
}

int main()
{
   int a, b;
   scanf("%d", &a);
   b = byteland(a);
   return 0;
}

I get segmentation fault (core dumped). Any solution?

4
  • Running without problems for me. What's your input ? Commented May 26, 2014 at 1:13
  • Probably just recursion to deep. How deep is it? Commented May 26, 2014 at 2:27
  • just curiosity, what does this function do? Commented May 26, 2014 at 7:10
  • its solution to spoj.com/problems/COINS Commented May 26, 2014 at 15:50

1 Answer 1

5

If you use 0 for a, you get infinite recursion -- stack overflow. You also get infinite recursion for many negative numbers.

Not sure what this function is supposed to do but there is nothing to break the recursion when a is equal to 0.

I would add a clause to break the recursion when a is equal to 0.

int byteland(int a)
{
   int e,f,g;
   if ( a == 0 )
   {
      return 0;
   }
   else if((a/2+a/3+a/4)<a)
   {
      return a;
   }
   else
   {
      e=byteland(a/2);
      f=byteland(a/3);
      g=byteland(a/4);
      return e+f+g;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.