Lab 6
comp 125-609, Goldwasser
Tuesday, October 5, 1999

Purpose: Using the debugger.

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.


Question 1: Let's begin by getting used to the application (without worrying about the debugger). There is a professor named Peter. What is his last name?

Hint: Download three files onto your machine using a web browser.

  • http://www.cs.luc.edu/~mhg/comp125/labs/search.vbp
  • http://www.cs.luc.edu/~mhg/comp125/labs/search.frm
  • http://www.cs.luc.edu/~mhg/comp125/labs/faculty.txt
  • 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".
    Question 2: What is the value of arraysize immediately after loading the file?

    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.


    Question 3: What is the value of arraysize immediately after the second name is inputted?

    Hint: If you want, you can place a breakpoint at line 11 and then step through the program patiently (use F8).


    Question 4: What is the value of arraysize immediately after the ninth name is inputted?

    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.


    Question 5: What is the value of arraysize immediately after the 23rd name is inputted?

    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".


    Question 6: When the file is being sorted, there is an "If" statement on line 48. What is the value of passNum and of index the very first time this test evaluates to true?

    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.


    Question 7: At the same point in time referred to in question 6, what is the value of firstname(index) at the beginning of the Then clause, and what is the value of it at the end of the Then clause.

    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.


    Question 8: What is the first value of index to make the If statement evaluate to true during the 21st pass of the loop?

    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).


    Question 9: There is a variable named middle in the routine searchFile_Click. Give the sequence of all values that it ever takes, in order, when you search for the name "Ronald".

    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.


    Question 10: If we run the program and search for "Stuart", no match is found. Because I know how the program works, I can tell you that by the end of such an unsuccessful search, the program actually narrowed down what two existing first names "Stuart" would have been between if it existed. These names are equal to firstname(last) and firstname(first) based on the final values of last and first. Tell me what two names Stuart lies between in our list.

    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).


    This lab is due before leaving class today. When you have completed the lab you should do the following:
  • (1) Turn in this sheet with all questions answered.
  • (2) Wait to see if any answers were wrong
  • (3) If some answers were wrong, redo those questions.

  • For convenience, here is the source code:

    ' 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
    


    Answer 1: ____________________________________________________

    Answer 2: ____________________________________________________

    Answer 3: ____________________________________________________

    Answer 4: ____________________________________________________

    Answer 5: ____________________________________________________

    Answer 6: ____________________________________________________

    Answer 7: ____________________________________________________

    Answer 8: ____________________________________________________

    Answer 9: ____________________________________________________

    Answer 10: ____________________________________________________