home Sandy, UT
Tuesday, 2017.11.21
10:26 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 8.8°C (47.8°F)
Barometer: 1028 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for Discrete Math class, short for Discrete Structures.
This one, although easy, has been nice to write. I had alot of fun writing the long double recurFib( int limit) function.
It was made to demonstrate the great difference in computing time between the iterative and the recursive methods.
To accomplish the task, come taken the enter and exit times of the routines in milliseconds, then subtracted the former from the latter.
Do not input number > greater than 39-40 (about 20 sec.s) unless you have a cluster of CPUs...
Download the executable: fibo.exe
October 7, 2004

Difference in Computing Time between
the Iterative and the Recursive Methods
in the Calculus of a Fibonacci Number

/**
  *  @title: fibo.cpp
  *  @date: 2004.10.07 - 2:54 pm
  *  @author: vinnie
  *  @source for fibo.exe
  */

#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>

struct myTime
{
  clock_t beginIterTime;  clock_t endIterTime;
  clock_t beginRecurTime; clock_t endRecurTime;
} mt;

//  =^=^=^=^=->>> ITERATIVE version
long double iterFib( int limit)
{
long double result = 0;
long double x, z;

  mt.beginIterTime = clock();
  if ( limit == 0) goto ABORT;

  x = 0;
  result = 1;
  --limit;
  for (int i = 0; i < limit; i++)
  {
     z = x + result;
     x = result;
     result = z;
  }

ABORT:
mt.endIterTime = clock();
return result;
}

//  =^=^=^=^=->>> RECURSIVE version
long double recurFib( int limit)
{
  if ( limit == 0 ) return 0;
    else if ( limit == 1 ) return 1;
      else return recurFib( limit-1) + recurFib( limit-2);
}
//  ===== *** ===== *** ===== *** ===== *** ===== *** ====

int main( )
{
system("color 1f");

// menu' label
MYMENU:   //  begin

//  print intro & menu
system("cls");
printf(" SLCC - CS 2310-01 - Fall '04 - Fibonacci - Assign. #5\n");
printf("\n Fibonacci Test\n\n");

//  ask user input
char cUI[10];
printf(" Input the number of the first X positive integers\n");
printf(" for which you want the Fibonacci' number: ");
gets( cUI);
int iUI = atoi( cUI);

//  ---   calculus start :    ---

//  iterations
long double res1 = iterFib( iUI);

//  recursion
mt.beginRecurTime = clock();
long double res2 = recurFib( iUI);
mt.endRecurTime = clock();

//  ===   ***   ===  PRINT RESULTS  ===== *** ===== *** ===== ***
int diff1, diff2;
diff1 = mt.endIterTime - mt.beginIterTime;
diff2 = mt.endRecurTime - mt.beginRecurTime;

gotoxy( 8, 8);  printf("Iter.s     Result");
gotoxy( 48, 8); printf("entryT    exitT    diff.in ms");

gotoxy( 1, 10);   printf("iter   %d", iUI );
gotoxy( 19, 10);  printf("%LE", res1);
gotoxy( 48, 10);  printf("%d", mt.beginIterTime);
gotoxy( 58, 10);  printf("%d", mt.endIterTime);
gotoxy( 68, 10);  printf("%d", diff1);

gotoxy( 1, 11);   printf("recur  %d", iUI);
gotoxy( 19, 11);  printf("%LE", res2);
gotoxy( 48, 11);  printf("%d", mt.beginRecurTime);
gotoxy( 58, 11);  printf("%d", mt.endRecurTime);
gotoxy( 68, 11);  printf("%d", diff2);

//  *_*   ask for an other round    *_*
printf("\n\n Another one [Y/y/N/n]? ");
while ( !kbhit() );
char c = getchar( );
if ( c == 'Y' || c == 'y' ) goto MYMENU;

//  ===== *** ===== *** ===== *** ===== *** ===== *** ====

printf("\n Thanks for using Fibonacci Test\n");
printf(" Vincenzo Maggio CodeŠ - released under GPL\n");
system("color 0f");
return 0;
}

2004.10.07

Vincenzo Maggio