CoEPP RC
 

This is the code used to create the fib executable in A walk in the cloud.

// A simple little C program to (inefficiently) compute Fibonacci of a number.
//
// Usage: ./fib <number>
//

#include <stdio.h>

unsigned long fib(int a)
{
    if ((a == 0) || (a == 1))
        return a;

    return fib(a-1) + fib(a-2);
}

int main(int argc, char **argv)
{
    unsigned int n;
    unsigned long result;

    // check args
    if (argc != 2)
    {
        printf("Usage: fib <n>\n");
        return 1;
    }

    // pick up N, compute fib(N)
    sscanf(argv[1], "%d", &n);
    printf("fib(%d)=%ld\n", n, fib(n));

    return 0;
}

Compile with:

gcc -o fib fib.c
cloud/example_code_fib.txt · Last modified: 2013/11/07 16:18 by rwilson
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki