You will be asked to examine a program I have written with the debugger, and to answer several questions based on your experience. Write all of your answers on the last page attached to this lab.
The program in question lets you load a file of names, sort the list by first name, and then search for particular first names in the list. You are not responsible for fully understanding how the code works (although we will see how later in the course). It is just a sample program for designing this lab.
Hint: Download three files onto your machine using a web browser.
Load the "search.vbp" project into Visual Basic and run the application. After setting the correct filename and path in the textbox, click on "1" to load the file. Then click on "2" to sort the file, and then enter "Peter" in the search box and click on "3".Hint: Place a breakpoint at the end of the LoadFile_Click routine (line 41), and then either use the immediate window or the mouse to get the variable's value.
Hint: If you want, you can place a breakpoint at line 11 and then step through the program patiently (use F8).
Hint: To save time, rather then stepping through line by line, place a breakpoint at the end of the loop (line 25), and then start running the program as normal. Each time the breakpoint is reached, check the value of n with the mouse, and if n<9 continue (use F5). When n=9 you have just read the ninth name. Check the value of arraysize.
Hint: This is taking too long. Let's see a new method. Go to the "Debug" pulldown menu and select "Add Watch". For "Expression" enter "n=23" (without the quotes). For "Procedure" select "LoadFile_Click". For "Watch Type" select "Break When Value is True". Hit OK. Now remove all previous breakpoints and then run the program and load the file. It will automatically stop when n=23 and at that point you can step a few lines and find the value of arraysize. To remove this Watch if you no longer need it, click the right mouse button in the Watch Window and select "Delete Watch".
Hint: Set a breakpoint inside the Then clause (at line 49). This breakpoint will only interrupt the program when the line is about to be executed, and hence only when the If statement is about to evaluate to true.
Hint: Get to the same place in the execution as in Question 6, and then step(F8) through the execution of the Then clause. While doing so, use either the mouse, the immediate window, or the watch window to monitor the value of firstname(index). By the way, if you are using the mouse, notice the difference between pointing to the word index in the expression firstname(index) and pointing to the word firstname in the same expression.
Hint: We want to do the same thing as in Question 6, but it takes too long to go step-by-step to the 21st pass. So let's use the watch window again. Start by clearing all breakpoints. Then "Add Watch" and set it appropriately for "passNum=21" and run the program, load the file and sort. When the program is interrupted, verify that passNum=21. Now simply add back in the breakpoint within the Then clause (at line 49) and continue(F5).
Hint: The Watch window will make this very easy for us. Put the cursor over one of the occurrences of the variable (line 62, for instances). Click the right mouse button once and select Add Watch. For the "Watch Type" select "Break When Value Changes". Now run the program, search for "Ronald", and every time the program breaks, record the current value of middle.
Hint: This is actually pretty easy. Pick a place to set a breakpoint which you are sure lies after the unsuccessful search is complete (but still within the searchFile_Click routine as we need to see the values of last and first before the routine ends. Now run the program, and when it breaks, use the immediate window to display the two names firstname(last) and firstname(first).
' Much of this code is modified from
' Chapter 4.2 on sorting and searching.
' You don't need to understand it now
' (but we will see it again later)
Option Explicit ' 1
Dim firstname() As String ' 2
Dim lastname() As String ' 3
Dim numPeople As Integer ' 4
Dim arraysize As Integer ' 5
Private Sub Form_Load() ' 6
arraysize = 8 ' 7
ReDim firstname(1 To arraysize) ' 8
ReDim lastname(1 To arraysize) ' 9
End Sub '10
Private Sub LoadFile_Click() '11
If Dir(filename.Text) <> "" Then '12
' open the file '13
Dim n As Integer '14
Open filename.Text For Input As #1 '15
n = 0 '16
Do While Not EOF(1) '17
n = n + 1 '18
If (arraysize < n) Then '19
arraysize = arraysize * 2 '20
ReDim Preserve firstname(1 To arraysize) '21
ReDim Preserve lastname(1 To arraysize) '22
End If '23
Input #1, lastname(n), firstname(n) '24
Loop '25
If (n < arraysize / 2) Then '26
' wasting too much space '27
ReDim Preserve firstname(1 To n) '28
ReDim Preserve lastname(1 To n) '29
arraysize = n '30
End If '31
Close (1) '32
numPeople = n '33
statusNum = Str(n) '34
statusFile = filename.Text '35
statusSorted = "(not sorted)" '36
Else '37
' file not found '38
MsgBox "File Not Found", , "File Not Found" '39
End If '40
End Sub '41
Private Sub SortFile_Click() '42
Dim passNum As Integer, index As Integer '43
Dim temp As String '44
' bubble sort array by first name '45
For passNum = 1 To numPeople - 1 '46
For index = 1 To numPeople - passNum '47
If firstname(index) > firstname(index + 1) Then '48
temp = firstname(index) '49
firstname(index) = firstname(index + 1) '50
firstname(index + 1) = temp '51
temp = lastname(index) '52
lastname(index) = lastname(index + 1) '53
lastname(index + 1) = temp '54
End If '55
Next index '56
Next passNum '57
statusSorted.Caption = "(sorted)" '58
End Sub '59
Private Sub searchFile_Click() '60
Dim foundFlag As Boolean '61
Dim first As Integer, middle As Integer, last As Integer '62
foundFlag = False '63
first = 1 '64
last = numPeople '65
Do While (first <= last) And (Not foundFlag) '66
middle = Int((first + last) / 2) '67
Select Case UCase(firstname(middle)) '68
Case UCase(searchstring.Text) '69
foundFlag = True '70
Case Is > UCase(searchstring.Text) '71
last = middle - 1 '72
Case Is < UCase(searchstring.Text) '73
first = middle + 1 '74
End Select '75
Loop '76
Picture1.Cls '77
If (foundFlag) Then '78
Picture1.Print firstname(middle); " "; lastname(middle) '79
Else '80
Picture1.Print "(none found)" '81
End If '82
End Sub '83