CSC 107 -Programming for Science
Lecture 16:
Debugging
Question of the Day
What is smallest number of matchsticks moved to make this equation valid (using roman numerals)
+ =
Today’s Goal
Today we are going to be talking about “debugging”What it does means and what it should mean
After today, should have additional strategies to develop working solutions
Origin of the Word “Bug”
KING EDWARD: So, lie thou there. Die thou, and die our fear; For Warwick was a bug that fear'd us all. -- Henry VI. Part III. Act V. Scene II.
bug meant a specter, ghost, other otherworldly source of fright
Origin of the Word “Bug” (2)
From late 19th century, “bug” meant defect or problem in electronic connections
First computerbug was moreliteral
Debugging a Program
Debug program when not working properlyCould be an error in the codeProgram logic could be incorrectDesign may not allow certain features
Better to find a bug as early as possibleEasiest to fix when still on paperUnderstood best immediately after writing it
This is NOT just a programming issueDebugging is best when starting early
Starting the Program
How to start a program1. Determine what the problem is asking
2. Come up with a good set of tests cases Test cases crucial for working programs
After all, how else can you know it works? Include a range of different possible inputs For each input determine the correct output
Test Cases
Round decimal to nearest whole number Input is a single decimal number Many, many possible test cases
I had 5 categories of test cases Positive rounding up -- 3.6. 14.5. 99.9999 Positive rounding down -- 3.2, 12.1, 99.00001 Negative rounding down -- -3.6, -313.9, -334.51 Negative rounding up -- -3.1, -313.001, -334.49 Whole numbers -- 6, -4, 0
Begin to Develop Algorithm
Much easier to start on paperEasier to write, test, and fix
Break problem up into smaller stepsEach step need not be easy or obviousBreak up test cases to create tests for these
stepsDo not worry solving subproblems yet
Use test cases to verify algorithm works If it does not, go back and fix those problems
Solving Subproblems
Solve each subproblem separatelyDo not worry about larger problem or issuesCan break up into even smaller subproblemsWorry about code only when it is obvious
Solving problems in any part of lifeSolving large problems is hardSolving small problems is not
Combining Subproblems
Start coding solution Code each subproblem separatelyAdd comments describing what each section does
Add printfs everywhereUse to testing code immediately after writingFirst, test only the code just writtenThen, test larger subproblem solutionLastly, test the entire program
Combining Subproblems
Repeat tests with each new section of codeErrors will be caught immediatelyLimits region that may contain the error
Compare region with design to spot errorsAdd more printfs and compare against
tracesShould given good idea of where error occurs If uncertain, trace code to discover why
problem occurs
More Debugging Hints
Liberally print out value of variablesGive printf unique prefixes to identify themprintf(“Hi! %d %c\n”, result, ch);printf(“Yo! %d %c\n”, result, ch);printf(“Whoa! %d %c\n”, result, ch);
Once testing done, comment out printfsMakes it easier if more testing needed later
Keep copy of last working versionWhen in doubt, compare new version against
old one
Your Turn
Get into groups and complete daily activity
Midterm Overview
Midterm results were very mixedMax score = 91Mean score = 83Standard deviation = 6.18
May apply curve at end of termWill not curve any single item
Can ask me questions next weekCheck web page for answersEveryone still doing well
For Next Lecture
Complete week #6 weekly assignment Enjoy the respite
Normal workload resumes next week…