1

I am coding a program with the help of recursive calling of a function, I get correct output till my test input gets solved within 10 steps of recursion but if I increase the input value to 11 it starts giving run time error as: Segmentation fault (core dumped)

the code snippet of the function is:

void find(int x) {    
    if(ctr==n-1) {
        po[k]=x;
        k++;
        ctr--;
        return;
    } else {
        ctr++;
        find(x+a);
        ctr++;
        find(x+b);  
        ctr--;
        return;
    }
}
5
  • 2
    What is the size of po? It seems you initial po to be int po[10]. Commented Aug 7, 2014 at 16:46
  • 2
    all pieces are not here. how are ctr, n, k, a & b declared and initialized? Commented Aug 7, 2014 at 16:53
  • It's 1000 i.e int po[1000] Commented Aug 7, 2014 at 16:54
  • n is 11 a is 3 b is 5 Commented Aug 7, 2014 at 16:55
  • ctr=0 which is initialized as a global variable Commented Aug 7, 2014 at 16:57

1 Answer 1

1

I ran it, it needs space in po for 1024 items with x=11,
you only have 1000

Sign up to request clarification or add additional context in comments.

3 Comments

What to do for large values like 58 or so
I have declared po[1024] as global variable
for a large value like 58, you would need 2^57 items, that wont fit in most computers. rethink how you get to the final answer.

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.