Top Banner
CSC 107 - Programming for Science Lecture 16: Debugging
16

CSC 107 - Programming for Science Lecture 16: Debugging.

Jan 02, 2016

Download

Documents

Chad Ross
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 107 - Programming for Science Lecture 16: Debugging.

CSC 107 -Programming for Science

Lecture 16:

Debugging

Page 2: 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)

+ =

Page 3: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 4: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 5: CSC 107 - Programming for Science Lecture 16: Debugging.

Origin of the Word “Bug” (2)

From late 19th century, “bug” meant defect or problem in electronic connections

First computerbug was moreliteral

Page 6: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 7: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 8: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 9: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 10: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 11: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 12: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 13: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 14: CSC 107 - Programming for Science Lecture 16: Debugging.

Your Turn

Get into groups and complete daily activity

Page 15: CSC 107 - Programming for Science Lecture 16: Debugging.

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

Page 16: CSC 107 - Programming for Science Lecture 16: Debugging.

For Next Lecture

Complete week #6 weekly assignment Enjoy the respite

Normal workload resumes next week…