Math 171 Spring 2005, Lab 14 (double lab)

Due Thursday, March 31, 11:59 P.M.

  1. Overview
  2. Assignment
  3. Hints

Overview

The purpose of this lab is to introduce you to functions and recursion in Fortran.

Top

Assignment

Write a Fortran program called fib that reads in an integer n from standard input, and then prints out the nth Fibonacci number on standard output, caclulated two different ways:

  1. Using a function called iterfib that calculates the nth Fibonacci number using a DO loop.
  2. Using a recursive function called recfib that performs the same calculation using recursion, with no loops.

Top

Hints

Recall that the Fibonacci numbers fn are given by the formula:

Note that you do not need to worry about timing your code. However, if you want to try this, you can use the intrinsic subroutine SYSTEM_CLOCK as follows:


INTEGER :: time, timerate, timemax

...

CALL SYSTEM_CLOCK(time, timerate, timemax)

This will store the current number of clock ticks in the variable time, the number of ticks per second in timerate, and the number of ticks before the clock rolls over in timemax. You can use this to determine how long each of your functions takes to complete.

Top

Back to the course information page