A Small Program

This small C program is a trivial little thing to calculate Fibonacci(N) for a given N. It is meant to be an example of something you could run in the cloud. It is not offered as an example of efficiency or correctness!

Create a directory job_test under your home directory. In this directory create a file fib.c containing:

// 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;

  sscanf(argv[1], "%d", &n);

  printf("Doing fib(%d)=", n);
  result = fib(n);
  printf("%ld\n", result);

  return 0;

We compile and test the program:

bash-3.2$ gcc -o fib fib.c
bash-3.2$ ls -l
total 12
rwxr-xr-x 1 rwilson people 7311 Nov 11 22:13 fib
rw-r--r-- 1 rwilson people  491 Nov 11 22:13 fib.c
bash-3.2$ ./fib 3
Doing fib(3)=2
bash-3.2$ ./fib 4
Doing fib(4)=3
bash-3.2$ ./fib 5
Doing fib(5)=5

Everything looks fine, but what we really want to do is compute Fibonnaci(50), which could take a while. It's just the sort of job that should run in the cloud.

cloud/a_walk_in_the_cloud_test_program.txt · Last modified: 2013/02/15 16:32 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