Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. unsigned int fib(unsigned int n);
  6.  
  7. int main() {
  8.     char input[10];
  9.     clock_t start, end;
  10.     unsigned int n, times_to_run = 10;
  11.     unsigned int a, i;
  12.     double average;
  13.    
  14.     while (1) {
  15.         n = 1;
  16.         average = 0;
  17.        
  18.         printf("\nN = ");
  19.         scanf("%u", &n);
  20.        
  21.         if (n == 0) break;
  22.        
  23.         for(i=0; i<times_to_run; i++) {
  24.             start = clock();
  25.             a = fib(n);
  26.             end = clock() - start;
  27.             average += (double)end;
  28.         }
  29.         average /= times_to_run;
  30.    
  31.         printf("Term %u is: %u\nComputation took ~%f clock cycles", n, a, average);
  32.     }
  33.  
  34.     return 0;
  35. }
  36.  
  37. unsigned int fib(unsigned int n) {
  38.     if (n == 1 || n == 2) {
  39.         return 1;
  40.     } else {
  41.         return fib(n - 1) + fib(n - 2);
  42.     }
  43. }