The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

CS101: Introduction to Computer programming
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Flow Charts, Loop Structures
PSEUDOCODE & FLOW CHART
Session Objectives# 24 COULD code the solution for an algorithm
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Chapter 2- Visual Basic Schneider
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
CS101- Lecture 11 CS101 Fall 2004 Course Introduction Professor Douglas Moody –Monday – 12:00-1:40 – – –Web Site: websupport1.citytech.cuny.edu.
Chapter 1 Program Design
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Chapter 3 Planning Your Solution
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 8 Linear Algebraic Equations PowerPoint to accompany Copyright © 2010.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
General Programming Introduction to Computing Science and Programming I.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Flowcharts.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
กระบวนการแก้ปัญหาด้วย คอมพิวเตอร์ 3 พฤษภาคม :00-17:00.
1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Review, Pseudocode, Flow Charting, and Storyboarding.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Agenda Basic Logic Purpose if statement if / else statement
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
The Hashemite University Computer Engineering Department
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alexandria University Faculty of Science Computer Science Department Introduction to Programming C++
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Algorithms and Flowcharts
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
SKMM1013 Programming for Engineers
Introduction to Programming for Mechanical Engineers (ME 319)
CS1001 Programming Fundamentals 3(3-0) Lecture 2
CS1371 Introduction to Computing for Engineers
Scripts & Functions Scripts and functions are contained in .m-files
Lesson 9: "if-else-if" and Conditional Logic
ALGORITHMS AND FLOWCHARTS
Learning to Program in Python
ALGORITHMS AND FLOWCHARTS
SME1013 PROGRAMMING FOR ENGINEERS
Design and Implementation
Chapter 2- Visual Basic Schneider
SME1013 PROGRAMMING FOR ENGINEERS
ICT Gaming Lesson 2.
CHAPTER 4: Conditional Structures
Tonga Institute of Higher Education IT 141: Information Systems
Basic Concepts of Algorithm
Presentation transcript:

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging, Relational and Logical Operators, Flow Charts, Conditional Statements

Lecture #1 Review Course introduction –course policies –assessment and deadlines MATLAB as a calculator MATLAB variables Script files

Lecture #2 Outline Debugging Relational and logical operators Flow Charts Conditional statements

Debugging Sometimes our program has errors or does not do exactly what we want. –the program has BUGS! If the program is small it may be easy to see what is wrong. If the program is large or complex then it may be hard to find the error. We can then DEBUG the program.

Debugging Can set a break in a script file –MATLAB runs script up to the break Then you control the program (step-by- step.

The Debug Menu

Relational Operators Relational statements test relationships between variables. A == B tests whether A equals B A ~= B tests whether A does not equal B A < B tests whether A is less than B A > B tests whether A is greater than B A <= B tests whether A is less than or equal to B A >= B tests whether A is greater than or equal to B

>> a=3; >> b=4; >> a==b ans = 0 >> a~=b ans = 1 >> a<b ans = 1 Using Relational Operators >> a>b ans = 0 >> a<=b ans = 1 >> a>=b ans = 0 false true

Logical Operators Logical operators are used to test conditions expressed as relationships between variables. “not” ( ~ in MATLAB), “and” ( & in MATLAB) and “or” ( | in MATLAB) are the most common logical operators. ~ ptrue if p is not true p & qtrue if both p and q are true p | qtrue if either p or q are true

Using Logical Operators >> a=3; >> b=4; >> ~(a==b) ans = 1 >> c=5; >> (a<b) & (b<c) ans = 1 >> (a>b) | (a>c) ans = 0 >> (a>b) | (b<c) ans = 1

Conditional Statements Dissecting the conditional: “If it is sunny outside then I will have a picnic.” “If the All Blacks win then I will be happy.” –Using MATLAB “If a < b then disp(a) ” condition dependent condition dependent condition dependent

Pseudocode and Flowcharts Pseudocode –Text description of program steps. May contain fragments of code. Doesn’t contain the nitty-gritty details. –Can use this to program any language. Flowcharts –Geometric symbols to describe program steps. –Captures the “flow” of the program. –Also useful for any programming language.

Flowchart Elements read radius area =  radius 2 print radius, area is radius < 0 ? start main stop main Beginning of algorithm End of algorithm No Yes Input Computation Output Comparisons From Etter Figure 3.1 page 87

if … end Syntax if condition some commands end end lets MATLAB know when the conditional statement is finished dependent

if … end Example File: conditional.m a=2; b=5; if a<b disp(a) end Matlab command prompt >> conditional 2 >>

Flowchart for if statement is a < b ? No Yes display a begin end a=2 b=5

if … else … end Syntax if condition some commands else some other commands end This section is OPTIONAL end lets MATLAB know when the conditional statement is finished

if … else … end Example File: conditional.m a=5; b=4; if a<=b disp(a) else disp(b) end Matlab command prompt >> conditional 4 >>

Flowchart for if.. else statement is a <= b ? No Yes display a begin end display b a=5 b=4

if (a<b) & (b<c) disp(‘b is between a and c’) end Building Conditions Suppose we are interested in 3 variables ( a, b, c ). a < b < c ? In MATLAB we construct two conditions and test whether both are true. Note use of “&” to test whether both conditions are true

Boolean Variables Boolean variables used to store “true” and “false” values. They can be very useful for relational statements and conditional statements. MATLAB uses 1 to represent true and 0 to represent false. –However, MATLAB considers any NONZERO number to be “true”

Boolean Variables Create boolean variables just like other variables: Use boolean variables to store answer to some “question” that controls a conditional statement. isFinished = 1 % true isFound = 0 % false Variable name Variable value

Naming Boolean Variables Should be able to tell the type of a variable from its name. Don’t use boolean status but isSuccessful. Try to write statements which read well. if isSuccessfulif ~isFailuredo somethingend

Friday’s Lecture Examples and exercises. Introduction to the Project.

Lecture #3 Preview (Next week) Loops Arrays Plotting

Optional Reading Lecture 2 “Introduction to MATLAB 7 for Engineers”, William J. Palm III –pages 44 – 48 “Engineering Problem Solving with C”, Delores M. Etter –pages Lecture 3 Palm –pages 48, –pages , 70 – 73 –pages