|
-
Oct 29th, 2003, 02:22 PM
#1
Thread Starter
New Member
Looping Question
Hi guys...
Our class just got into looping and list boxes with VB.Net, and I got a question that I am stuck on and appreciate any help!
Write a program that will display all the numbers between 1 and 100 that are part of the Fibonacci sequence. The Fabonacci sequence begins 1,1,2,3,5,8,..., where each new number in the sequence is found by adding the previous two numbers in the sequence. Display your results in a list box or a text box with a multilane property set to True.
Regards
-
Oct 29th, 2003, 02:53 PM
#2
I have no idea on VB.Net so I won't post code...
To start the sequence you need a seed, which is two ones (1,1). Store those in an array at index 0 and 1. Start looping (counter starts at 1, since that's currently the last array index used) . At each loop add array index = counter and array index = counter - 1, if sum is less than 100 then append sum to array index = counter + 1. If sum is equal or greater than 100 then stop the loop.
Transfer the array values to a listbox or multiline textbox.
Up to you if you'll use dynamic arrays.
-
Oct 29th, 2003, 06:19 PM
#3
Frenzied Member
VB Code:
dim LastNumber as integer
dim PreviousNumber as integer
dim CurrentNumber as integer
listbox1.clear
listbox1.additem 1
lastnumber=1
previousnumber=0
currentnumber=0
while currentnumber<=100
currentnumber=previousnumber+lastnumber
listbox1.additem currentnumber
previousnumber=lastnumber
lastnumber=currentnumber
wend
Not in front of VB, so I can't test this, but it looks ok to me.
How many marks do you get for this question - I think you owe me some!
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Oct 29th, 2003, 06:49 PM
#4
The way you had it included 144 
VB Code:
Dim LastNumber As Integer
Dim PreviousNumber As Integer
Dim CurrentNumber As Integer
listbox1.Clear
listbox1.AddItem 1
LastNumber = 1
PreviousNumber = 0
CurrentNumber = 0
While CurrentNumber <= 100
CurrentNumber = PreviousNumber + LastNumber
If CurrentNumber > 100 Then GoTo SkipLoop
listbox1.AddItem CurrentNumber
PreviousNumber = LastNumber
LastNumber = CurrentNumber
Wend
SkipLoop:
I do hope that this'll work in .NET too (ie, it was testing in VB6)
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Oct 29th, 2003, 11:09 PM
#5
So the seed is 0,1?
Why not put the test at the end of the loop since your bound to go through it at least once.
-
Oct 31st, 2003, 11:48 AM
#6
Thread Starter
New Member
Thanx guyz..I really appreciate it! I will give it a shot and see how it turns out.
Buzby, This is actually like 3 points on a worksheet but it was killing me so I had to find the answer!
-
Oct 31st, 2003, 12:29 PM
#7
Frenzied Member
Make sure you dont use GoTos. They are evil
-
Oct 31st, 2003, 02:00 PM
#8
yay gay
Originally posted by jemidiah
The way you had it included 144 
VB Code:
Dim LastNumber As Integer
Dim PreviousNumber As Integer
Dim CurrentNumber As Integer
listbox1.Clear
listbox1.AddItem 1
LastNumber = 1
PreviousNumber = 0
CurrentNumber = 0
While CurrentNumber <= 100
CurrentNumber = PreviousNumber + LastNumber
If CurrentNumber > 100 Then GoTo SkipLoop
listbox1.AddItem CurrentNumber
PreviousNumber = LastNumber
LastNumber = CurrentNumber
Wend
SkipLoop:
I do hope that this'll work in .NET too (ie, it was testing in VB6)
Wends are gone in .net if i am not wrong
\m/  \m/
-
Oct 31st, 2003, 03:12 PM
#9
Originally posted by PT Exorcist
Wends are gone in .net if i am not wrong
I can't find any reference to that keyword in my book, so yeah they arn't their anymore.
Though from the code that was posted using it, it seems very useless......
-
Oct 31st, 2003, 03:17 PM
#10
yay gay
just change the wend by next
i think that'll make the trick
\m/  \m/
-
Oct 31st, 2003, 03:24 PM
#11
Originally posted by PT Exorcist
just change the wend by next
i think that'll make the trick
Why??
What does Wend even do? Does it go back to the top of the loop? Or stop it?
-
Oct 31st, 2003, 03:53 PM
#12
yay gay
does the same as next in a for loop
\m/  \m/
-
Oct 31st, 2003, 04:05 PM
#13
Originally posted by PT Exorcist
does the same as next in a for loop
But it's a while loop, next and wend are both useless for a while loop....
meh
-
Nov 1st, 2003, 09:56 AM
#14
yay gay
omg
its just a FLAG to the compiler know where the while beggins and ends
\m/  \m/
-
Nov 3rd, 2003, 05:56 AM
#15
Frenzied Member
While..Wend are my loop of choice (probably just being stubborn to be honest).. the replacement is Do While.. Loop
VB Code:
dim LastNumber as integer
dim PreviousNumber as integer
dim CurrentNumber as integer
listbox1.clear
listbox1.additem 1
lastnumber=1
previousnumber=0
currentnumber=0
do while currentnumber<=100
currentnumber=previousnumber+lastnumber
listbox1.additem currentnumber
previousnumber=lastnumber
lastnumber=currentnumber
loop
or you could do
VB Code:
do
currentnumber=previousnumber+lastnumber
listbox1.additem currentnumber
previousnumber=lastnumber
lastnumber=currentnumber
loop until currentnumber>100
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|