CS 221 Hints for Lab Quiz 2

Fall 2011

Here are some strategy hints that may be helpful in tackling problems like the first one in the sample quiz (Ben G. Near's meter-checking code):

  1. CAREFULLY read the problem. Make sure you know what the code is supposed to do and what sort of output (if any) it is supposed to generate or what changes it is supposed to make.

  2. Look at the actual broken code. You can probably spot some errors immediately. Fix them.

  3. Run the code (or make a call to the function).

  4. Do you get an error message? If not, skip to step 8.

  5. Otherwise, find the lines in the code that are causing the error.

  6. Is it a syntax error? Look for missing quotes or parentheses or the wrong kind of quote or the wrong kind of statement. Look for X instead of x (since variables are case sensitive in MATLAB). Is it a range error? Make sure you are not trying to access element 0 or element n+1 of an n-element array. Fix any problems you find. Look closely. Be patient.

  7. Go back to step 3 (run it again).

  8. Does the code generate the exact output that the problem requires? If it does, skip to step 10.

  9. If not, try to isolate what is causing the problem. Maybe %d in an fprintf should really be %8.2f (or whatever). Maybe a loop is not "looking at" all of the elements of the array that it should (maybe it omits the first or last element of an array, or the first or last row of a matrix). Maybe > should really be >=. Maybe the code doesn't work for some input values. Attempt to locate and fix the problem, then go back to step 3.

  10. Double-check your work. Does the code run now? Does it generate the desired output? If so, congratulations! Upload your file as instructed.