Wednesday, December 8, 2010

Some sample exams

Topics differ from semester to semester, so there might be some questions you aren't able to answer.
One final.
Another.

Monday, December 6, 2010

last time: redim
resize an array

using a for each loop

  For Each letter in sentence.ToCharArray()
    If (letter >= "A") And (letter <= "Z") Then
      index = Asc(letter) - 65  'The ANSI value of "A"is 65
      charCount(index) += 1
    End If
  Next

how freq table might be useful
huffman trees



http://en.wikipedia.org/wiki/Huffman_coding

AN example of using freq tables

"If arrayOne() and arrayTwo() have been declared with the same data type, then the statement

arrayOne = arrayTwo
makes arrayOne() an exact duplicate of arrayTwo(). It will have the same size and contain the same information.

"

This comment is false. fixed in later versions of the book




so, how DO we copy an array?

two ways:
1)
Dim a(4) As Integer = {6, 8, 2, -8, 0}
Dim b() as Integer
' i want to make b an exact duplicate of a
ReDim b(UBound(a))
For I = 0 to UBound(a)
    b(i) = a(i)
Next

2)
b = a.Clone()

HW: arrays, creating and accessing
q 28, 32

Wednesday, December 1, 2010

Array
a collection

the "problem" with dealing with a collection as a bunch of separate variables.
1) unweildy
2) not easily changable to diff number of variable

Dim score(29) As Double
Dim student(29) As String

score(0) = 87
score(1) = 92

If you write programs involving large arrays
a good idea is to test using small arrays first

For i As Integer = 0 To 29
  lstBox.Items.Add(score(i))
Next


a new loop!
For Each x in score
    lstBox.Items.Add(x)
Next

we can make the array a class-level variable
and fill it in the Form_Load event
Dim teamName(3) As String = {"Packers", "Packers", "Jets", "Chiefs"}

often, we will use loops to iterate thru an array.
easier way: use for each loop
harder way: for i = 0 to whatever the upper bound is

problem: 29 is a "magic number". makes code hard to read. introduces errors. let us say i change to 60 students!

for i = 0 to UBound(scores)
    scores(i) = 90
next

for i = 0 to scores.GetUpperBound(0)
    scores(i) = 90
next


Dim scores() As Double
realize that scores, or any array, is a reference type
ReDim scores(29)
ReDim scores(n)

dynamic memory allocation

Dim scores(3) As Integer
scores(0) = 58
scores(1) = 78
scores(2) = 100
scores(3) = 98

ReDim scores(4)
' alternatively
ReDim Preserve scores(4)

will preserve the values

Monday, November 29, 2010

lecture notes

For Loops
are an example of syntactic sugar

1) how do i start?
2) how do i end?
3) step

For i = 1 to 10
    ListBox1.Items.Add(i*i)
Next

When should I use a For loop?
1) you want to do something a fixed number of times
(In this instance, i don't *use* the variable i inside the loop at all)
2) when you want to generate a series of numbers

ex 1:

Dim year as Integer
pop = 300000
For year = 2006 To 2010
    LstTable.Items.Add(year & "  " & pop)
    pop = pop + pop * 0.03
Next


Alternatively:

pop = 300000
For year as integer = 2006 To 2010
    LstTable.Items.Add(year & "  " & pop)
    pop = pop + pop * 0.03
Next


if I have:
x operator= 6

x = x operator 6

for loops, 30-40 even

row = ""
for i = 1 to 10
    row = row & "*"
next
for i = 1 to 10
    listbox1.items.add(row)
next


for i = 1 to 10
    row = ""
    for i = 1 to 10
        row = row & "*"
    next
    listbox1.items.add(row)
next

Wednesday, November 24, 2010

lecture notes

Nesting: putting one loop in another
Loop thru values of X in (0 thru 2)
    Loop thru values of y in (0 thru 3)
        Print (x, y)
    End Loop
End Loop

LISTS.TXT
    ' the names of other phonebook files
    CLIENTS.TXT
    FRIENDS.TXT
    KINFOLK.TXT


Loop thru filenames in the LISTS.TXT file
    Open the current filename
    Loop thru the entries in the current file
        see if name occurs in current file
    End Loop
End Loop

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
  Dim foundFlag As Boolean
  Dim fileName As String
  Dim name As String = ""
  Dim phoneNum As String = ""
  Dim sr1 As IO.StreamReader = IO.File.OpenText ("LISTS.TXT")

  txtNumber.Text = "Name not found."
  ' by the way, i use a goto to break out multiple
  ' levels when we find the actual word
  Do While sr1.Peek <> -1
    fileName = sr1.ReadLine
    Dim sr2 As IO.StreamReader = IO.File.OpenText(fileName)
    Do While sr2.Peek <> -1
      name = sr2.ReadLine
      phoneNum = sr2.ReadLine
      If name = txtName.Text Then
    txtNumber.Text = name & "     " & phoneNum
        Goto Here
      Endif
    Loop
  Loop
  sr1.Close()
Here:

End Sub

HW:
Processing Lists of Data with Do Loops
question 20-24, even

Write a program to find and display the largest of a collection of positive numbers contained in a text file. (Test the program with the collection of numbers 89, 77, 95, and 86.)

Dim max as Integer
max = 0

Do While sr.Peek <> -1
    current = sr.ReadLine
    ' is my optimistic assumption wrong?
    If current > max Then
        max = current
    EndIf
Loop

Debug.Print("The max value is: " & max)

Monday, November 22, 2010

lecture

Write a function that returns n factorial

txtBox1.Text = fact(5)

1) where do i start?
2) where do i end?
3) how do i get to the next step?

Function fact(byval n as integer) As integer
    dim i as integer
    dim prod as integer
    i = 1
    prod = 1

    Do
        prod = prod * i
        i += 1
    Loop Until i = n + 1
    Return prod
End Function

prod is a accumulator
off by one error
we calculated factorial using "iteration"
"iteration" is a fancy name for loops

write me a function which is the SUM of the nums from 1 to n:

Function sum(byval n as integer) As integer
    dim i as integer
    dim total as integer
    i = 1
    total = 0

    Do
        total = total + i
        i += 1
    Loop Until i > n
    Return total
End Function

sr.Peek will return -1 if reached EOF (end of file)

lets say i want to read firstname, lastname, and print it out.

1) where do i start?
2) where do i end?
3) how do i get to the next step?

Dim sr As IO.StreamReader
sr = IO.File.OpenText("c:\josh\Names.txt")
Dim firstname, lastname as String

Do While sr.Peek <> -1
    firstname = sr.ReadLine
    lastname = sr.ReadLine
    ListBox1.Items.Add("first: " & firstname,  & vbTab & "last" & lastname)
Loop


Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
  Dim name, phoneNum As String
  Dim sr As IO.StreamReader = IO.File.OpenText("PHONE.TXT")
  ' pessimistic assumption
  txtNumber.Text = "Name not found."

  Do While sr.Peek <> -1
    name = sr.ReadLine
    phoneNum = sr.ReadLine
    If name = txtName.Text Then
       ' my pessimistic assumption was proven false
       txtNumber.Text = name & "    " & phoneNum
    ' make things faster; this is optional
    Exit Do
    EndIf
  Loop

  sr.Close()
End Sub

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