This is the first time I am using a library function with "..._t" so I can only guess I might not have wrote the whole thing properly.
The code is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
hrtime_t gethrtime(void);
int recursive_fibonacci(int);
int iterative_fibonacci(int);
int main()
{
int n;
hrtime_t start, end;
int result;
printf("Enter n for which to test the average time of running: ");
scanf("%d", &n);
start = gethrtime();
result = recursive_fibonacci(n);
end = gethrtime();
printf("The average time for a recursive funtion was: %lld nsec.\n", (end - start)/n);
start = gethrtime();
result = iterative_fibonacci(n);
end = gethrtime();
printf("The average time for an iterative function was: %lld nsec.\n", (end - start)/n);
printf("The Fibonacci number was: %d\n", result);
return 0;
}
int recursive_fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}
int iterative_fibonacci(int n)
{
int i;
int next_fib;
int first_fib = 0;
int second_fib = 1;
for(i = 2; i < n; i++)
{
next_fib = first_fib + second_fib;
first_fib = second_fib;
second_fib = next_fib;
}
return next_fib;
}
I already looked for a typo, but I can't see any.
And the error given by the compiler was pasted in full. No line reference at all...
Can you tell me what happened here?