Wednesday, November 17, 2010

lecture notes

q 23

c = -40

f = (9/5)*c + 32
ListBox1.Items.Add("c:" & c & vbTab & "f:" & f)


the actual code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        c = -40
        Do
            f = (9 / 5) * c + 32
            ListBox1.Items.Add("c:" & c & vbTab & "f:" & f)
            c += 5
        Loop Until c > 40
    End Sub
End Class

q 25:

1) where do we start (initialization)?
2) where do we end? (terminating condition)
3) how do we get there? each step (increment)

'Dim pop as Double
'pop = 6.5

Dim pop as Long
pop = 6.5 * 10 ^ 9
Const percent = 1.2 / 100
Dim year as Integer = 2006

Do
 ListBox1.Items.Add("year:" & year & vbTab & "pop:" & pop)
 year += 1
 pop = pop + pop * percent
Loop Until pop >= 10 * 10^9

ListBox1.Items.Add("year:" & year & vbTab & "pop:" & pop)

Different roles for variables when it comes to loops
1) loop control variable
2) counter
3) accumulator
4) flag

q 27:
Write a program to display all the numbers between 1 and 100 that are part of the Fibonacci sequence. The Fibonacci 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.

prev = 1
cur = 1

Do
 ListBox1.Items.Add("num:" & vbTab & cur)
 temp = prev
 prev = cur
 cur = temp + cur
Loop Until cur > 100


' now, using mathematic notation
Fn = 1
Fn_1 = 0

Do
 ListBox1.Items.Add("num:" & vbTab & Fn)
 Fn_2 = Fn_1
 Fn_1 = Fn
 Fn = Fn_1 + Fn_2
Loop Until Fn > 100

using iteration to calculate Fibonacci

HW: Do Loops, q 24-36, evens

No comments:

Post a Comment