0

So in a week 3 short, Doug asks us to define a file collatz.c that returns the number of steps taken to do the collatz conjecture on different numbers and I ended up with this.

#include <stdio.h>
#include <cs50.h>

int collatz(int number, int y);

int main(void)
{

    int number = get_int("Number: ");

    int steps = collatz(number, 0);

    printf("%i", steps);
}

int collatz(int number, int y)
{

    if (number == 0)
    {
        return y;
    }

    else if (number % 2 == 0)
    {
        ++y;
        collatz(number / 2, y);
    }

    else
    {
        ++y
        collatz((3 * number) + 1, y);
    }

    return 0;
}

It compiles fine but after running gives "Segmentation fault (core dumped)". I don't really get why this is happening, can someone please help me with this one?

1 Answer 1

0

When number reaches the value of 4, we enter an infinite loop, hence segfault, it is worth starting to use GDB if you are thinking of being a software developer, it is a critical skill. Check your code carefully.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .