Title: Additional loop presentation
1Additional loop presentation
2Additional problems requiring loops
- Fibonacci sequence
- Euclids GCD algorithm
- Reading data from a file
- Changing base of numbers base b to base 10.
- Changing base 10 to base b
3Fibonacci numbers
- These numbers used in Biology (and in Computer
Science!) are the sequence - 1, 1, 2, 3, 5 , 8, 13, 21, ?, ?, .
- Using any two adjacent numbers in the sequence,
you can generate the rest of the sequence. How?
4Fibonacci numbers
- Fib(i2)Fib(i)Fib(i1) is the recurrence
relation. - Set old1 and current1. Build a loop for as
many fibonacci values as youd like - Display them in a listbox
5Fibonacci numbers
6Fibonacci numbers
- Should display first two
- Remember to declare a counter for your for loop
- Remember to subtract two from how many loop
iterations
7Fibonacci numbers Button click code
- Dim i, old, cur As Integer
- old 1
- cur 1
- Dim n As Integer
- n Integer.Parse(TextBox1.Text)
- 'print first two fibs
- ListBox1.Items.Add(cur)
- ListBox1.Items.Add(cur)
- For i 1 To n - 2 'need to count the
first two already shown - cur cur old
- old cur - old
- ListBox1.Items.Add(cur)
- Next
8GCDgreatest common divisor
9GCD discussion
- This application gets the greatest common divisor
two ways and counts how many times it has to loop
for each technique. - The crude method set your guess to be the
smaller number. As long as your guess doesnt
evenly divide both numbers, keep subtracting 1
from it.
10Euclids algorithm
- Get the remainder
- Iterate the following
- Rembig mod small note rem is reserved word
- If rem is zero quit, small is the gcd
- If not, set big to small and small to remainder,
and do it again.
11Button click
- Dim a, b, gcd, big, small As Integer
- Try
- a Integer.Parse(TextBox1.Text)
- b Integer.Parse(TextBox2.Text)
- If a gt b Then
- big a
- small b
- Else
- big b
- small a
- End If
- gcd getGCD1(big, small)
- Label3.Text "gcd using crude
method" gcd.ToString() - gcd getGCD2(big, small)
- Label4.Text "gcd using Euclid"
gcd.ToString() - Label5.Text "method looped"
loopct1.ToString() - Label6.Text "method looped"
loopct2.ToString - Catch ex As Exception
- MessageBox.Show("must enter
integers")
12Crude method code
- Function getGCD1(ByVal big, ByVal small) As
Integer - Dim diff As Integer small
- Loopct10
- Do While big Mod diff ltgt 0 Or small Mod
diff ltgt 0 - diff diff - 1
- loopct1 1 loop1ct is a global
integer - Loop
- Return diff 'should be gcd
- End Function
13Euclids method
- Function getGCD2(ByVal big, ByVal small) As
Integer - Dim remainder As Integer
- loopct20
- remainder big Mod small
- Do While remainder ltgt 0
- loopct2 1 loop2ct is a global
integer - big small
- small remainder
- remainder big Mod small
- Loop
- Return small
- End Function
14Reading from a data file
- Nothing here especially requires a loop except
processing a lot of data. - Typically, the application needs to read numbers
or names or whatever until it reaches end of file.
15Notes on text files
- Create a data file by typing numbers (or
something) into a file. - I used Textpad. Wordpad and Notepad editors will
also work. - If you use MS Word, be sure to select save as
option ascii text
16Create a data file save as txt
17Save data file to project/thisproj/bin/debug
18I built an ap with just a button and listbox
19Button click code opens file and reads (strings)
to end of file
- Private Sub btnread_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnread.Click - Dim data As String
- Lstdisplay.Items.Clear()
- Dim sr As IO.StreamReader
IO.File.OpenText("data.txt") - Do While sr.Peek ltgt -1
- data sr.ReadLine()
- Lstdisplay.Items.Add(data)
- Loop
- sr.Close()
- End Sub
20Running form just displays the data
21Improvements
- Sum the values in the data file
- Search for a name or number in the data file
- Allow user to enter a file name
22Base changer
- In base b, only digits 0b-1 may appear
- Remarks on Radix positional notation
- A number in any base consists of legal digits,
each represents how many there are of the base to
some power
23Base changer
- So
- 12345 base 10 represents 110000210003100 and
so on - 101111 in base 2 represents 111214180161
3247
24Base changer
- Convert to base 10
- 33221 in base 4
- 10101 in base 2
- 4210 in base 5
25Base changer
- Putting together an ap that converts numbers from
base b to base 10 - Get input from user, a String (number in base b)
and an integer (the original base). - Set a sum value to 0
- LOOP
- Use the substring function to peel off digits
from the string and multiply by the appropriate
power of the base and add to the running total
26Numbers in base 10 are unchanged
27But if base was 5 22535469
28Handles any base up to including 10
29Buttonclick code
- Dim i, j, ans, base As Integer
- Dim x, y As String
- ans 0 ' will hold answer
- base Integer.Parse(Txtbase.Text)
- x Txtnum.Text
- For i 0 To x.Length - 1
- y x.Substring(i, 1) ' this short
string 1 character long - j Integer.Parse(y)
- ans ans base y
- Next
- Lblans.Text ans.ToString
30To handle larger bases
- Need a function to return proper character value
31Going the other way base 10 to base b
32Use mod and div (\) to build a string
representation of the answer
- Dim num, base As Integer
- Try
- num Integer.Parse(txtnum.Text)
- base Integer.Parse(txtbase.Text)
- Dim ans As String ""
- Do While num ltgt 0
- ans ans num Mod base
- num num \ base
- Loop
- lblans.Text "answer is" ans
- Catch ex As Exception
- End Try
33Still to do
- Handle bases bigger than 10 I havent done
that. - If the remainder is larger than 9, you need to
use alpha representation for the digits A is
10, and so on.