Qbasicnews.com

Full Version: Fibonacci numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have absolutly no clue how to do this. I have to write a program that will let you enter a number of minutes. Then it will for every minute increase the number of bacteria produced according to Fibonacci numbers. All I have is this mess:

Code:
DECLARE SUB fibo (time!, total!)
CLS

INPUT "Number of minutes. ", min

FOR time = 1 TO min
        CALL fibo(time, total)
NEXT time

PRINT "The number of bacteia at the end of"; min; " minutes is"; total; "."

SUB fibo (time, total)

DIM f(1 TO time)

SELECT CASE time
        CASE IS = 0
                total = 0
        CASE IS = 1
                total = 1
        CASE IS > 1
                total = f(time - 1) + f(time - 2)
END SELECT


END SUB
Jumpboy,

Most of your code looks good. You are just missing a few details.

1) You never store anything into the f array. See where I modified the sub to store into the array.

2) A Fibonacci series goes 1,1,2,3,5,.... so when your index (time) is 1 the total=1, if time is 2, then the total is also 1, if time is 3, then the total is the total of time1 and time2 which is 2. I changed the CASEs for this reason.

3) Since the default data type is single, I removed the "!" from time and total in the DECLARE.

4) The DIM of array f makes more sense at the top of the program, so I moved it. Had to make it SHARED so that the SUB could reference it. Would not work the way you had it.

5) I inserted an END before the SUB.

6) You have a variable called "time". I'm not positive, but this may be a reserved word, so I changed it to "tim".

I tested the modified version, and it works.

Regards,
Moneo
*****
Code:
DECLARE SUB fibo (tim, total)
CLS

INPUT "Number of minutes. ", min

DIM SHARED f (1 to min)

FOR tim = 1 TO min
        CALL fibo(tim, total)
NEXT tim

PRINT "The number of bacteia at the end of"; min; " minutes is"; total; "."

END

SUB fibo (tim, total)

SELECT CASE tim
        CASE IS = 0            'This is actually impossible
                total = 0
        CASE IS < 3
                total = 1
        CASE ELSE
                total = f(tim - 1) + f(tim - 2)
END SELECT

f(tim) = total

END SUB