Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the last iteration of this code is not working properly?

Tags:

c

loops

iteration

I am writing a program that prints the first 50 Fibonacci numbers, starting with 1 and 2. The numbers must be separated by a comma, followed by a space except the last number, it should end with a new line instead.

the code I tried:

int main(void)
{
    int fib[50];
    fib[0] = 1;
    fib[1] = 2;
    int i;

    for (i = 0; i < 50; i++)
    {
        if (i == 0)
        {
            printf("%d ,", fib[0]);
        }
        else if (i == 1)
        {
            printf("%d ,", fib[1]);
        }
        else if (i == 49)
        {
            fib[i] = fib[i-1] + fib[i-2];
            printf("%d", fib[i]);
        }
        else 
        {
            fib[i] = fib[i-1] + fib[i-2];
            printf("%d ,", fib[i]);
        }
    }
    printf("\n");
    return (0);
}

the result I get:

1 ,2 ,3 ,5 ,8 ,13 ,21 ,34 ,55 ,89 ,144 ,233 ,377 ,610 ,987 ,1597 ,2584 ,4181 ,6765 ,10946 ,17711 ,28657 ,46368 ,75025 ,121393 ,196418 ,317811 ,514229 ,832040 ,1346269 ,2178309 ,3524578 ,5702887 ,9227465 ,14930352 ,24157817 ,39088169 ,63245986 ,102334155 ,165580141 ,267914296 ,433494437 ,701408733 ,1134903170 ,1836311903 ,2971215073 ,512559680 ,3483774753 ,3996334433 ,3185141890 ,
like image 600
Bemin Dawoud Avatar asked Dec 12 '25 16:12

Bemin Dawoud


2 Answers

There are multiple problems in your code:

  • you must include <stdio.h> to use printf correctly. You might have forgotten the #include line in the question code, but as posted, the code has undefined behavior.

  • on the vast majority of current systems, the type int is not large enough to accommodate the values of Fibonacci numbers up to 49. You should use type long long int or unsigned long long int that are guaranteed to handle numbers up to 263-1 (resp. 264-1).

  • the format string "%d ," is incorrect: the space should appear after the comma, not before.

  • the output posted is inconsistent with the code: some of the numbers are larger than 231-1. Here is the output I get with your code:

    1 ,2 ,3 ,5 ,8 ,13 ,21 ,34 ,55 ,89 ,144 ,233 ,377 ,610 ,987 ,1597 ,2584 ,4181 ,6765 ,10946 ,17711 ,28657 ,46368 ,75025 ,121393 ,196418 ,317811 ,514229 ,832040 ,1346269 ,2178309 ,3524578 ,5702887 ,9227465 ,14930352 ,24157817 ,39088169 ,63245986 ,102334155 ,165580141 ,267914296 ,433494437 ,701408733 ,1134903170 ,1836311903 ,-1323752223 ,512559680 ,-811192543 ,-298632863 ,-1109825406
    

    Note however that the behavior on signed integer overflow is undefined so other behaviors are possible albeit unlikely.

  • there is no need for an array, you can just use 3 variables and cycle them.

  • it is easier to make a special case of the first 2 numbers and output the , and a space before each of the next numbers.

  • whether you want to print the Fibonacci sequence starting with 1, 2 or 0, 1 or even 1, 1 is a question of convention. The Italian mathematician Fibonacci himself used 1, 2 in his time (1202 AD) as he was discussing rabbit population growth in his Liber Abaci. As a matter of fact, Fibonacci is credited for introducing the base 10 notation in Europe and the concept of zero, imported from the Arab world where he grew up. The numbers in the picture of the manuscript show the sequence numbers using a variant of Indo-Arabic numerals. In modern times, it is more conventional to start the sequence at 0, 1 and to name these as F0 and F1.

Here is a modified version:

#include <stdio.h>

int main(void) {
    unsigned long long a = 1, b = 2;

    printf("%llu, %llu", a, b);

    for (int i = 2; i < 50; ++i) {
        unsigned long long c = a + b;
        printf(", %llu", c);
        a = b;
        b = c;
    }
    printf("\n");
    return 0;
}
like image 68
chqrlie Avatar answered Dec 14 '25 09:12

chqrlie


I can only assume you are experiencing int overflow. There is a finite (platform specific) range to an int value. Try a long long. Also, you could simplify the logic here quite a bit. For example, you could print the first two values before the loop; after that the logic is regular and requires no if(s). Like,

int i;
long long fib[50];
fib[0] = 1;
fib[1] = 2;

printf("%lld, %lld", fib[0], fib[1]);
for (i = 2; i < 50; i++)
{
    fib[i] = fib[i-1] + fib[i-2];
    printf(", %lld", fib[i]);
}
printf("\n");
like image 20
Elliott Frisch Avatar answered Dec 14 '25 10:12

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!