Skip to content
Snippets Groups Projects
Commit 1b2b55d6 authored by Andreas Schmidt's avatar Andreas Schmidt :seedling:
Browse files

+= fib_tail_rec

parent e45921ee
No related branches found
No related tags found
No related merge requests found
OPT = "-O3"
fib: fib.c test_fib.c
cc -o test_fib.o -c test_fib.c
cc -o fib.o -c fib.c
cc $(OPT) -o fib.o -c fib.c
cc test_fib.o fib.o -o fib
fib_rec: fib_rec.c test_fib.c
cc -DNDEBUG -o test_fib.o -c test_fib.c
cc -o fib_rec.o -c fib_rec.c
cc $(OPT) -o fib_rec.o -c fib_rec.c
cc test_fib.o fib_rec.o -o fib_rec
fib_tail_rec: fib_tail_rec.c test_fib.c
cc -DNDEBUG -o test_fib.o -c test_fib.c
cc $(OPT) -o fib_tail_rec.o -c fib_tail_rec.c
cc test_fib.o fib_tail_rec.o -o fib_tail_rec
fib_dp: fib_dp.c test_fib.c
cc -DNDEBUG -o test_fib.o -c test_fib.c
cc -o fib.o -c fib_dp.c
cc $(OPT) -o fib.o -c fib_dp.c
cc test_fib.o fib.o -o fib_dp
fib_lv: fib_lv.c test_fib.c
cc -DNDEBUG -o test_fib.o -c test_fib.c
cc -o fib.o -c fib_lv.c
cc $(OPT) -o fib.o -c fib_lv.c
cc test_fib.o fib.o -o fib_lv
all: fib fib_rec fib_dp fib_lv
all: fib fib_rec fib_tail_rec fib_dp fib_lv
clean:
rm *.o
rm fib
rm fib_rec
rm fib_tail_rec
rm fib_dp
rm fib_lv
#include <stdio.h>
#include <stdlib.h>
#include "test_fib.h"
unsigned int fib_rec(unsigned int i, unsigned int a, unsigned int b)
{
if (i == 0)
{
return a;
}
else
{
return fib_rec(i - 1, b, a + b);
}
}
unsigned int fib(unsigned int n)
{
return fib_rec(n, 0, 1);
}
int main(int argc, char **argv)
{
test_fib();
if (argc != 2)
{
printf("Usage: fib <number>\n");
return 1;
}
unsigned int n = atoi(argv[1]);
unsigned int f = fib(n);
printf("fib(%d) = %d\n", n, f);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment