CT243 lab1


http://geminga.it.nuigalway.ie/~gettrick/courses/CT243/labs/l1.html





Last term you wrote a program that uses recursion to calculate the nth fibonacci number. These numbers are 0,1,1,2,3,5,8,13,21,34,55,... and are defined by f(n) = f(n-1) + f(n-2).

  1. Write a VB program that calculates the nth fibonacci number using iteration. Use a while loop, and variables e.g. f for the current fibonacci number, f_oneSTEPback for the previous one, and f_twoSTEPSback for the one before that. You should code this up using a function Function fibiterate(ByVal n) . Note that you should use the data type Double for the actual numbers, as Double in VB can run up to about 10308
  2. Write a new function Function fibARRAYiterate(ByVal n) which uses an array of Double to calculate the nth fibonacci number fn(i.e. A(0)=0, A(1)=1, and A(n) is fn)

    [ Hint: The best way to lay this out might be to have a form with one input box, and three buttons, called e.g. recursion, iterate, ARRAYiterate: The user types in a positive integer in the input box, and depending on which button is clicked, the respective function is called. The answer could be presented in a separate message box. ]

  3. Test your recursion code to see does it slow down as n gets large (it will!). Find for which value of n does the calculation of fn, using recursion, take (approximately)
    1. 10 seconds
    2. 2 minutes
    (Use your watch, or just mobile phone to do the approximate timing).
  4. What is the largest value of n for which you can calulate fn using
    1. fibiterate
    2. fibARRAYiterate


    © NUI, Galway