Lab 11

Program Testing and Debugging

CS211 Lab Policy:

Instructions:

  1. Save the program high_low.m to a directory in your MATLAB search path.
     
  2. Open high_low.m in the MATLAB editor.  Note that this file contains several intentional errors!  Read the program documentation to determine what the program is supposed to do.
     
  3. Your job is to find and correct errors in high_low.m.  For each error you find and correct, add a comment on the line immediately above the line containing the error.  Your comment should identify the type of error (syntax, run-time, or logic) and how you corrected the error.  For example, you may add comments similar to the following:

    % SYNTAX ERROR - added missing right parenthesis
    or
    % LOGIC ERROR  - changed > to >=
     
  4. Begin debugging high_low.m by examining the code without trying to run it.  Try to identify any code that looks obviously wrong.  Correct any obvious errors you see, adding a comment describing the error and your correction.
     
  5. Continue debugging high_low.m by testing the program with a variety of inputs that run all parts of the code.  If the interpreter identifies a syntax error, correct it.  If the program does not appear to be producing the correct behavior, identify the error causing the incorrect behavior and correct it.  For each error you identify and correct, add a comment describing the error and your correction.  Please use the MATLAB debugger to help you locate errors.
     
  6. Once you believe you have corrected all of the errors in high_low.m, thoroughly test the program to help convince yourself that no errors remain.
     
  7. The program is supposed to display a special message if the user guesses the secret number on their first guess for moderate and advanced levels.  This part of the code is particularly difficult to test since the user must correctly guess the secret number (a 1 in 100 chance, or worse) on their first try for this code to execute.  MATLAB's debugger can help!  In the editor, set a breakpoint on the line of code immediately following the line on which the secret number is set.  Run your program to the breakpoint by pressing the Run button.  Select the moderate or advanced play level.  Once the program pauses at the breakpoint, determine the value of the Secret_number variable by placing the cursor over the variable name in the editor or by displaying the variable in the command window.  Once you know the value of the secret number, continue running the program by pressing the Continue button (the same button as Run).  When prompted for your first guess, enter the secret number value.  You should see the message reporting that you guessed the secret number on your first try.  If you do not, ask your instructor for assistance.
     
  8. To gain experience using MATLAB's debugger to step through a program, re-run high_low.m until the program pauses at the breakpoint.  This time, instead of pressing the Continue button, repeatedly press the Step button to execute your program one line at a time.  Note that you will have to enter a guess each time the input function gets executed.  Check the values of variables as you step through your program.  If you find any more errors while using the debugger, correct them and add a comment describing the error and your correction.
     
  9. Once you believe you have thoroughly tested high_low.m and found all errors, save and submit the corrected program.

Turn-in:

Submit your corrected and commented high_low.m file.

References: (none)