netjunky

Fibos

Nov 7th, 2010
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <limits.h>
  5.  
  6. #define DEBUG 1 // for debuging purpose
  7.  
  8. int fibs( int howMany );
  9.  
  10. int fibs( int howMany )
  11. {
  12.     unsigned long int *dataArr;
  13.     int i = 0;
  14.    
  15.     unsigned long int result = 0;
  16.    
  17.     if( 3 > howMany )
  18.     {
  19.         if( DEBUG )
  20.         {
  21.             printf("The amount of years is less then 3\n");
  22.         }
  23.        
  24.         return 0;
  25.     }
  26.    
  27.     dataArr = ( unsigned long int* )malloc( sizeof( unsigned long int ) * ( howMany + 1 ) );
  28.    
  29.     if( NULL == dataArr )
  30.     {
  31.         if( DEBUG )
  32.         {
  33.             printf("Memory for storing fibs data wasn't allocated\n");
  34.         }
  35.         return 0;
  36.     }
  37.    
  38.     dataArr[1] = dataArr[2] = 1;
  39.    
  40.     if( DEBUG )
  41.     {
  42.         printf("Amount of rabbits in 1 year is: %lu\n", dataArr[ 1 ] );
  43.         printf("Amount of rabbits in 2 year is: %lu\n", dataArr[ 2 ] );
  44.     }
  45.    
  46.     for( i = 3; i <= howMany; i++ )
  47.     {
  48.         dataArr[ i ] = dataArr[ i - 1 ] + dataArr[ i - 2 ];
  49.         if( DEBUG )
  50.         {
  51.             printf("Amount of rabbits in %d year is: %lu\n", i, dataArr[ i ] );
  52.         }
  53.     }
  54.    
  55.     result = dataArr[ howMany ];
  56.     free( dataArr );
  57.    
  58.     return result;
  59. }
  60.  
  61. // Second parameter might be the amount of years
  62. int main( int argc, char **argv )
  63. {
  64.     // Timing
  65.     time_t start, end;
  66.     clock_t start1, end1;
  67.     double dif;
  68.     double dif1;
  69.  
  70.     //unsigned long int years = ULONG_MAX; // by default it should be minimum, so 3
  71.     int years = 3;
  72.     unsigned long int howManyResult = 0;
  73.    
  74.     if( ! DEBUG )
  75.     {
  76.         printf("It's: %d\n", years);
  77.         return 0;
  78.     }
  79.    
  80.     if( argc >= 2 )
  81.     {
  82.         years = atoi( argv[1] );
  83.     }
  84.    
  85.     time( &start );
  86.     start1 = clock();
  87.     //printf("We started at %ld\n", start);
  88.    
  89.     howManyResult = fibs( years );
  90.    
  91.     time( &end );
  92.     end1 = clock();
  93.     //printf("We ended at %ld\n", end);
  94.    
  95.     if( 0 != howManyResult )
  96.     {
  97.         printf("For %d years the amount of rabbits is: %lu\n", years, howManyResult);
  98.     }
  99.    
  100.     dif = difftime( end, start );
  101.     dif1 = ((double)(end1 - start1)) / CLK_TCK;
  102.    
  103.     printf("It took program %.2lf seconds\n", dif);
  104.     printf("It took program %3.3lf seconds\n", dif1);
  105.    
  106.     return 0;
  107. }
Add Comment
Please, Sign In to add comment