Tuesday, December 14, 2010

review of first practice test

first sample test
1) b
2) b
3) d
4) c
5) not necessary, but (a)
6) c
7) d
8) d
9) B
10) b
11) wasn't covered, but it is called a recursive function
12) d
13) True
14) d
15) b
16) c

Debugging:
1) a logic error. shouldv'e used ReDim Preserve
2) a logic error. you can't swap if these are passed byval. change to byref
3) syntax error. should be Next, rather than End For

Programming:
1) skip this. it won't be this high-level.
2)
For I = 1 to 10
Debug.Print(I^3)
Next

3) don't worry about it. but it would be a nested loop.

Monday, December 13, 2010

Final review

general format:

60 points of m/ch and short answer
40 points of programming (choose 4 of 5)
20 points of tracing

Tracing example:
from 6.1, q 5

focus on arrays and loops
a LITTLE on functions and subs
general knowledge from previous sections

review of second practice exam
m/ch:
1) c
2) c
3) b
4) b
5) a
6) d
2D arrays
Dim myVar(8, 8) As Double
81 slots
7.5 two dimensional arrays

7) a
8) c
9) c
10) c
11) c
12) a

Part III
1) Write a SubRoutine called NumbersInSequence that takes two integer parameters, I and J, and prints out all the numbers from I to J.
Sub NumbersInSequence(ByVal I as Integer, ByVal J as Integer)
    For K = I to J
        ListBox1.Items.Add(K)
    Next
End Sub

2)
Sub PrintUntilSeven(ByVal x() as Integer)
    For i = 0 to UBound(x)
        Debug.Print(x(i))
        If x(i) = 7 Then
            Exit For
        Endif
    Next
End Sub

Sub PrintUntilSeven(ByVal x() as Integer)
    For each t in x
        Debug.Print(t)
        If t = 7 Then
            Exit For
        Endif
    Next
End Sub


Sub PrintUntilSeven(ByVal x() as Integer)
    i = 0
    Do While x(i) <> 7
        Debug.Print(x(i))
    Loop
End Sub

Select Case letter
    Case "A"
        Debug.Print("Apple")
    Case "B"
        Debug.Print("Banana")
    Case "C"
        Debug.Print("Cookie")
    Case "D"
        Debug.Print("Deer")
End Select

Wednesday, December 8, 2010

lecture notes

sorted array
linear search - can speed up; we can end prematurely
binary search

search algorithm

why "Or n = 9"?
do not want OutOfArrayBounds exception

  Dim name2Find As String
  Dim n As Integer = -1
  name2Find = txtName.Text.ToUpper
  ' start as pessimist
  txtResult.Text = "Not found."
'  for i = 0 to 9
'    if nom(i) = name2Find then...
'  next
  For Each x in nom
    if x = name2Find Then
      txtResult.Text = "Found."
    Elseif x = name2Find Then
      exit for
    Endif
  Next

' that was my version of linear search as found in book
 Dim list1() As String = {"Al", "Carl", "Don", "Greg", "Jack"}
  Dim list2() As String = {"Bob", "Carl", "Eric", "Greg", "Herb"}

to merge, I COULD first copy contents of both, and then sort
Bad idea!

do while ia <= Ubound(a) and ib <= Ubound(B)
if A(ia) <= B(ib) Then
C(ic) = A(ia)
ia+= 1
else
C(ic) = B(ib)
ib +=1
endif
ic +=1
loop
for ia = ia to Ubound(A)
   C(ic) = A(ia)
   ic += 1
next
for ib = ib to Ubound(B)
   C(ic) = A(ib)
   ic += 1
next

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