How to solve a C-segment failure with vectors?

Asked

Viewed 19 times

1

Look! I don’t know what’s going wrong. When I compile my code in the codeblock, it’s all right. However when I sent the code to the evaluation in the run codes, they returned that besides taking too long it is giving Segmentation Failure. Could someone help me?

#include <stdio.h>

int main(){

   int fib[60], N;

   scanf("%d", &N);

   int ent[N];
   fib[0] = 0;
   fib[1] = 1;

   for(int i=0; i < N; i++)
     scanf("%d", &ent[i]);

   for(int j=2; j < 60; j++)
    fib[j] = fib[j-1] + fib[j-2];


    for(int k=0; k < 60; k++)
    printf("Fib(%d) = %d\n", ent[k], fib[ent[k]]);

    return 0;
}

My intention with this code is to make that when the person type a number N, appear N spaces for it to fill that will be the position of the Fibonacci sequence so that it will be:

Entree:
3
2
3
6

Exit:
Fib(2) = 1
Fib(3) = 2
Fib(6) = 8

I hope you’ve made your point...

  • 1

    The last loop is to traverse ent that has such a size N and not 60 as it is coded.

  • For this type of targeting flaws the ideal is to use the Valgrind tool or similar. This can give you an idea of where the problem lies in the code. From any form to @Maniero has already indicated what the problem is in your code.

  • Thank you guys. You helped me a lot!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.