Fibonacci program drives me nuts
I don't wanna look like [I'm lazy] and just asking to do a program for me, but I have been trying to figure it out for a total of 4 hours. The exact requirements are:
Code:
2) Write a program that will calculate the Fibonaci sequence up to a given element. Write a program that will allow the user to enter the 10, and the 34 will be calculated. Remember the Fibonaci sequence is generated by giving yourself the first two values, 0 and 1, and then adding the two previous values to get the next value.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
For example:
Fibo(3) = Fibo(1) + Fibo(2) = 0 + 1 = 1
Fibo(4) = Fibo(2) + Fibo(1) = 1 + 1 = 2
Fibo(5) = Fibo(3) + Fibo(4) = 1 + 2 = 3
Etc.
Hint: You will need to preserve the TWO previous values in variables, not just the ONE previous value like we have done with all the rest of the loops.
Just throw suggestions out there. By the way, I use vb 6.0
Re: Fibonacci program drives me nuts
This would work as long as they enter a number of 2 or greater.
Code:
function calcFibonacci(byval iVal as integer) as integer
' iVal is the number of times to loop
dim iNum1 as integer
dim iNum2 as integer
dim iNum3 as integer
iNum1 = 0
iNum2 = 1
for iLoop = 2 to iVal
iNum3 = iNum2 + iNum1
iNum1 = iNum2
iNum2 = iNum3
next
calcFibonnaci = iNum2
end function
Re: Fibonacci program drives me nuts
Quote:
Originally Posted by schaefer
This would work as long as they enter a number of 2 or greater.
Code:
function calcFibonacci(byval iVal as integer) as integer
' iVal is the number of times to loop
dim iNum1 as integer
dim iNum2 as integer
dim iNum3 as integer
iNum1 = 0
iNum2 = 1
for iLoop = 2 to iVal
iNum3 = iNum2 + iNum1
iNum1 = iNum2
iNum2 = iNum3
next
calcFibonnaci = iNum2
end function
I am not sure how to put this in my program since we never learned this type of funtion "function calcFibonacci(byval iVal as integer) as integer" and we never learned byval. We usually had some command button to click like "Private Sub Command1_Click()"
Re: Fibonacci program drives me nuts
You should create Functions to perform routines which you may want to use in more than one place in a program. Just put the keyword Public or Private in front of it. Public makes it accessible to other modules in the program while Private will limit its use to the form on which it is saved.
But if you have not learned it yet then do not use the function in your code or they may know that someone else wrote it for you.
Just place the code below inside of your Sub
Code:
' iVal is the number of times to loop
dim iNum1 as integer
dim iNum2 as integer
dim iNum3 as integer
iNum1 = 0
iNum2 = 1
for iLoop = 2 to iVal
iNum3 = iNum2 + iNum1
iNum1 = iNum2
iNum2 = iNum3
next
calcFibonnaci = iNum2
Re: Fibonacci program drives me nuts
of course you will need to retrieve the value the user entered and store it in iVal before you begin the loop
Re: Fibonacci program drives me nuts
Sorry, but I don't see how it should work since there is no variable assigned to a desired user's input value. I mean if I put in any number in a text box, it should calculate sequence up to that many times.
Re: Fibonacci program drives me nuts
Thats what I was saying you would assign iVal to txtUserInput.text or whatever you are calling the text box. Alternatively you could also do this:
Code:
for iLoop = 2 to txtUserInput.text
...
next
but if the user enters a non-numeric value it will crash. Better to do something like this
Code:
iVal = Val(txtUserInput.text)
for iLoop = 2 to ival
...
next
Re: Fibonacci program drives me nuts
oh and replace this line
calcFibonnaci = iNum2
with
txtResult.text = iNum2
where txtResult is the textbox where the result is displayed
Re: Fibonacci program drives me nuts
whoa man it works! With the exception that if I enter 1 it should come out to 0. But i think it could be easily fixed by putting an if statement:
If iVal = 1 then
txtResult.Text = 0
Else
do the code you said
end if
FIXED: MY FINAL CODE
Code:
Option Explicit
'Calculates the Fibonaci sequence up to a given element.
Dim fibo1 As Single, fibo2 As Single, fibo3 As Single, i As Single, num As Single
Private Sub cmdCalculate_Click()
picOutput.Cls
num = Val(txtNumber.Text)
fibo1 = 0
fibo2 = 1
If num = 1 Then
picOutput.Print 0
ElseIf num = 2 Then
picOutput.Print 1
Else
num = Val(txtNumber.Text) - 1
For i = 2 To num
fibo3 = fibo2 + fibo1
fibo1 = fibo2
fibo2 = fibo3
Next i
picOutput.Print fibo2
End If
End Sub
Re: Fibonacci program drives me nuts
You could have taken advantage of the fact that the number of elements is required from the user... you can then size a dynamic array, e.g. ReDim lngArr(9) for 10 elements. You seed lngArr(1) = 1, and loop accordingly
Code:
Public Function GetFibonacci(cnt As Long) As Long
Dim lngArr() As Long
Dim X As Long
Select Case cnt
Case Is < 2
GetFibonacci = cnt-1 'return 1, 0, or negative value which indicates error
Case Else
ReDim lngArr(cnt - 1)
lngArr(1) = 1
For x = 2 to Ubound(lngArr)
lngArr(x) = lngArr(x-1) + lngArr(x-2)
Next
GetFibonacci = lngArr(Ubound(lngArr))
End Select
End Function
You can even modify code to return the array (all values in sequence) instead of just the last value.
Re: Fibonacci program drives me nuts
as long as it works correctly I'm fine. besides, we did not really learn that stuff your talking about yet.
Re: Fibonacci program drives me nuts
All programmers have to learn arrays, better now than later... you can compare implementations (with array vs without) that return the same output to speed up your learning.
Re: Fibonacci program drives me nuts
Just a side comment because this sequence has always fascinated me. Note that:
1) The ratio of the last Fibonacci number in the sequence to the one right before it will eventually converge to to one plus the square root of 5, all divided by 2. (approximately 1.618033989)
2) It makes no difference where you start the Fibonacci sequence. The convergence is always the same.
3) If a rectangle is drawn with sides equal in proportion to the convergence, forming two more by bisecting the long side produces two more of identical proportions. That process can be continued indefinitely to produce more smaller "golden" rectangles, all proportional to each other.
:cool: