CT243 lab1
http://geminga.it.nuigalway.ie/~gettrick/courses/CT243/labs/l1.html
-
For this lab you must submit
-
Handwritten development notes, including statement of the problem, analysis of the problem, algorithm design (which may include flow charts).
-
A printout of form, image and code of your program, and any numerical answers requested.
-
The above material should be given to David or left in the cardboard box marked CT243 in the mail room (room IT415) on the 4th floor of the IT Building
before
the deadline of 5pm
Monday 28th January 2008. You will lose 20% for each day
(or part of day) the lab is late.
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).
-
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
-
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. ]
-
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)
-
10 seconds
-
2 minutes
(Use your watch, or just mobile phone to do the approximate timing).
-
What is the largest value of n for which you can calulate fn using
-
fibiterate
-
fibARRAYiterate
©
NUI, Galway