Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Programming Types of Testing.
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
General Programming Introduction to Computing Science and Programming I.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Program Errors and Debugging Week 10, Thursday Lab.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Control Structures Repetition or Iteration or Looping Part II.
read and learn from example loop programs develop modular program
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
11/25/2015Slide 1 Scripts are short programs that repeat sequences of SPSS commands. SPSS includes a computer language called Sax Basic for the creation.
1 Program Planning and Design Important stages before actual program is written.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Debuggers in Python. The Debugger Every programming IDE has a tool called a debugger. This application does NOT locate or fix your bugs for you! It slows.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
The Hashemite University Computer Engineering Department
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Beginning Programming for Engineers Matlab Conditional Computation.
Getting Started With Python Brendan Routledge
MATLAB – More Script Files
Computer Application in Engineering Design
Matlab Training Session 4: Control, Flow and Functions
C++ Basic Syntax – Homework Exercises
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
Week 8 - Programming II Today – more features: Loop control
Print slides for students reference
TOPIC 4: REPETITION CONTROL STRUCTURE
MATLAB Programs Chapter 6 Attaway MATLAB 4E.
Conditions and Ifs BIS1523 – Lecture 8.
Programming Funamental slides
How to Run a Java Program
What's wrong with Easter jokes? They crack you up
MATLAB Programs Chapter 6 Attaway MATLAB 5E.
Presentation transcript:

Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using MATLAB’s debugger Text Section: 4.7 Please Download: fun1.m, fun2.m, and invest.m from class website

Exercise: Develop the following guessing game program. 1.Player starts with 10 points 2.The program randomly picks a number from 1 to 100. N.B. code for picking this number is : n = ceil(rand*100) 3.The program asks player to guess a value – player enters a number 4.Program tells player they are high, low or correct. If correct game is over and score is displayed If incorrect they lose one point and go back to #3 Print out a copy of your program and a copy of some game play including validation (how can you be sure this program is working correctly?)

Optimization Logical Errors Syntax Errors Debugging

Syntax Errors: Respond to flags in editor and error messages during execution (you have been doing this for a while.) Logical Errors: 1.Validation: Check Logic Compare final result to reference results (hand calc.) & identify problems. 2.Modular Programming: Build and test small pieces 3.Intermediate Results: make intermediate results visible and compare to expected intermediate results (usually a hand calc.) Optimization: will leave for now. We have made occasional referrals to optimal code

Debugging Logical Errors Key tool  make intermediate results visible and compare to expected intermediate results Remove ; or use disp() run functions as scripts by: –commenting out the function definition line –make sure inputs are in the workspace –run as script –check variables in workspace Use debugger

MATLAB editor debugger We will work through example starting on pg 231 When reading the debugger section it is best to start here, then go back and read the first part of the section 4.7. –we will use these two functions (available on the website) fun1.m function y=fun1(x) avg=sum(x)/length(x); y=fun2(avg,x); fun2.m function above=fun2(x,avg) above=length(find(x>avg)); –These functions are intended to determine how many values are greater than the average in a vector.

Test case x =[1,2,3,4,10] What is the average? = 20/5 = 4 How many values are greater than the average = 1 Run with these functions >> fun1([1,2,3,4,10]) >> ans = 3  not the correct answer Using the debugger –with both files open select Desktop  Dock –Then select a tile arrangement from far right menu bar –set a breakpoint (stopping point) at function call line and at calculation in second program

re run, notice what happens –program stops –look at workspace windows –use “stack” to switch workplace windows –(notice separate workspaces) –notice average is correct = 4 can also print value in command window –can be changed at this point avg = 3 not a problem  continue to finish program remove break and read at line 2 of program 2 –notice work space –x is not a vector, avg is –x and avg reversed in function call Can fix and continue should get the correct answer

Can also use on loops (example pg. 233) Supplementary – not covered in lecture Enter program: invest.m (available on website) function z=invest(x,r) z = 0; y = *r; for k = 1:length(y) z = z*y + x(k); end Program is intended to look at an investment pattern after a number of years. x is a vector of annual dollar amounts invested. r is the % interest rate.

Check with values in table below Run Program Gives the wrong answer Set breakpoint before statement in loop Check values, step, check values, step … YearInvestmentBalance CalculationBalance *(1.1) *(1.1)

Practice Problem: Fibonacci sequence A Fibonacci sequence is one where each successive number is the sum of the previous two numbers. E.g. if a sequences starts with 1, 1 the Fibonacci sequence would be: 1, 1, 2, 3, 5, 8, 13, 21 … Fibonacci sequences can begin with any two numbers. Develop a function that produces a Fibonacci sequence given the first two numbers and the number of elements in the sequence.

Problem Goal (brief): Develop a program that will produce a Fibonacci sequence given two starting numbers and the length of the series. Inputs: NameDescriptionUnits or ValuesInput Source * N1First number in seriesIntegerCommand line N2Second number in seriesIntegerCommand line NNumber of terms in seriesIntegerCommand line Outputs: NameDescriptionUnits or ValuesOutput type * FSFibonacci SeriesNACommand line Test Case: see example case from slide Program Logic : Can you develop a flow chart for this problem?

function FS = Fib(n1, n2, N) % function FS = Fib(n1, n2, N) % This function will create a Fibonacci Series. Fibonacci Sequences are % series where each element is the sum of the previous two elements % S. Scott Moor April 2008 % % Input Variables: n1 = the first number in the series (integer) % n2 = the second number in the series (integer) % N = number of terms in the series (integer) % Output Variable: FS = Resulting Fibonacci Series % Other Variables k = loop index and current term in series % Preallocate the series and place the first two terms in the series. FS = zeros(1,N); FS([1,2]) = [n1, n2]; % Each iteration of this loop creates the next term in the series for % terms 3 through N. for k = 3:N FS(k) = FS(k-1)+FS(k-2); end Validation of function on test case: >> Fib(1,1,8) ans =