general computer science for engineers cisc 106 lecture 21 dr. john cavazos computer and information...

13
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 21 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

General Computer General Computer Science Science

for Engineersfor EngineersCISC 106CISC 106

Lecture 21Lecture 21

Dr. John CavazosComputer and Information Sciences

04/10/2009

Page 2: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Lecture OverviewLecture OverviewCommon ErrorsDebugging Tips (pgs 72 – 76)Symbolic Debugger (137-140)

Page 3: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Different Kinds of ErrorsDifferent Kinds of ErrorsSyntax Errors x = (y + 3) / 2);

Run-time Errors x = n / 0;

Logical ErrorsRuns correctly but wrong

answer!

Page 4: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Common ProblemsCommon ProblemsNot initializing all variablesStarting array index variable at 0Incorrect evaluation order in

equationOff by one errorInfinite loop

Page 5: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Some easy fixesSome easy fixesBreak up equation into smaller

piecesAdd parenthesis to make

equation clearInitialize all variables!

Page 6: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

DebuggingDebuggingAdd print statements!Display value of all variablesRun through a very simple test

caseIncreasingly make test case

longerFor example, sorting an arraytry an empty array, try with one

element,try an array of size 2, then size 5,

so on…

Page 7: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Finding an number in array

- Pass into function array and number

- Initialize array index to 1- Initialize number found boolean

variable- while number not found- if current array position has

num- set number found - end- If number found report!

Page 8: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Finding a number in an array

function found = findNumber(array, value)

x = 1;found = false;while (found == false) if (a(x) == value) found = true; end end

What is the problem?

Page 9: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Add some print statementsfunction found = findNumber(array,

value) x = 1;found = false;while (found == false) if (a(x) == value) found = true; end found xend

Note x does not change!

Page 10: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Add some print statementsfunction found = findNumber(array,

value) x = 1;found = false;while (found == false) if (a(x) == value) found = true; end x = x + 1;end

Problem if value not found! Should test for this.

Page 11: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Add some print statementsfunction found = findNumber(array,

value) x = 1;found = false;while (found == false && x <=

length(a)) if (a(x) == value) found = true; end x = x + 1;end

Page 12: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Symbolic DebuggerSymbolic DebuggerOpen m-file to debug in Matlab

editorCan set breakpoints

Where you want execution to suspend

Execute in command window as normal

, 2009 from 1:00 PM to 1:30 PM

Page 13: General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009

Symbolic DebuggerSymbolic DebuggerExecution suspends in editor at

breakpointCan view variables in command

windowCan step through the codeCan continue to next

breakpoint17, 2009 from 1:00 PM to 1:30 PM