Flow Charts, Loop Structures

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Introduction to Flowcharting
Introduction to Flowcharting
UNIT 2. Introduction to Computer Programming
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
CMPS 1371 Introduction to Computing for Engineers
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
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.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Chapter McGraw-Hill/Irwin Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. 6 Discounted Cash Flow Valuation.
Computer Science 1620 Programming & Problem Solving.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Chapter 3 Planning Your Solution
The Program Design Phases
Linear Simultaneous Equations
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
Matrix Mathematics in MATLAB and Excel
Non-Linear Simultaneous Equations
Fundamentals of C programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC103: Introduction to Computer and Programming
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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 6 Finding the Roots of Equations
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
McGraw-Hill/Irwin ©2001 The McGraw-Hill Companies All Rights Reserved 5.0 Chapter 5 Discounte d Cash Flow Valuation.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 2.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 Pseudocode & Flowcharts
Working with Arrays in MATLAB
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Decision Making and Branching (cont.)
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Program Design & Development EE 201 C7-1 Spring
ALGORITHMS AND FLOWCHARTS
REPETITION CONTROL STRUCTURE
Repetition Structures Chapter 9
Topics Introduction to Repetition Structures
Chapter 4 MATLAB Programming
Chapter 4 MATLAB Programming
Introduction to Flowcharting
Introduction To Flowcharting
Chapter 4: Repetition Structures: Looping
Presentation transcript:

Flow Charts, Loop Structures Chapter 4 MATLAB Programming Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Flowcharts Flowcharts are diagrams that illustrate the paths followed in a sequence of computations Flowcharts are a great tool for planning complex algorithms Flowcharts are also very useful for documenting and explaining an algorithm, even relatively simple ones For many of the simple programs we will write, drawing a flowchart may seem to be unnecessary, but learning to create and read flow charts is a valuable skill Engineering Computation: An Introduction Using MATLAB and Excel

Example Flowchart In this example, many subroutines are used Often, a complex program is organized into a series of subroutines Notice the decisions points (diamonds) and loops – several subroutines are repeated many times Engineering Computation: An Introduction Using MATLAB and Excel

Flowcharts Flowcharts are often used to illustrate process sequences in manufacturing operations and decision-making sequences in management Consider the flowchart of a company’s product design process: Engineering Computation: An Introduction Using MATLAB and Excel

Typical Flowchart Symbols These symbols are not always used, but the diamond-shaped Decision Point can be considered a universal standard Engineering Computation: An Introduction Using MATLAB and Excel

The for Loop in MATLAB Also called a do loop in other languages Used when you want the calculations to be performed a defined number of times In this example, the calculations are performed 10 times Engineering Computation: An Introduction Using MATLAB and Excel

The for Loop in MATLAB In MATLAB, a for loop begins with the statement indicating how many times the statements in the loop will be executed A counter is defined within this statement Examples: for k = 1:100 (counter = k, the loop will be executed 100 times) for i = 1:2:7 (counter = i, the counter will be incremented by a value of 2 each time until its value reaches 7. Therefore, the loop will be executed 4 times (i = 1,3,5, and 7) Engineering Computation: An Introduction Using MATLAB and Excel

The for Loop in MATLAB The loop ends with an end statement In M-files, the MATLAB editor will automatically indent text between the for and end statements: Can you determine what the variable x will be after running this M-file? Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Example The first time through the loop, j = 1 Because of the single value in parentheses, x will be a one-dimensional array x(1) will be set equal to 5*1 = 5 The second time through the loop, j = 2 x(2) will be set equal to 5*2 = 10 This will be repeated until j = 10 and x(10) = 50 Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Example x will be a one-dimensional array (a row matrix) with 10 elements: Engineering Computation: An Introduction Using MATLAB and Excel

Condensed Form of for Loop Flowchart Note the use of the connector symbol where paths join Good practice to add connectors to flowcharts of MATLAB programs: a connector corresponds to an end statement for m = 1:10 (Calculations) Engineering Computation: An Introduction Using MATLAB and Excel

for Loop in Interactive Mode Loop commands can be entered directly from the command prompt The calculations are not performed until the end statement is entered Engineering Computation: An Introduction Using MATLAB and Excel

for Loop in Interactive Mode Remember that if you leave off the semi-colon, the results of the calculations will be written to the screen in every loop: Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Examples What result will be output to the screen in each of the following examples? y = 0; for k = 1:5 y = y + k; end y Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Examples y = 0; for k = 2:2:8 y = y + k; end y Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Examples for k = 1:5 y(k)=k^2; end y Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Examples for j = 1:3 for k = 1:3 T(j,k) = j*k; end T Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Example Consider this equation: Plot this equation for values of x from -10 to 10 We will use a for loop to calculate and store x and y values in one-dimensional arrays Engineering Computation: An Introduction Using MATLAB and Excel

for Loop Example for i = 1:21 x(i) = -10 +(i-1); y(i) = 2^(0.4*x(i)) + 5; end After running these lines of code, two one-dimensional arrays, x and y, have been created, each with 21 elements Engineering Computation: An Introduction Using MATLAB and Excel

Plot Command The stored arrays can be plotted with the command: plot(x,y) Any two one-dimensional arrays can be plotted, as long as they are exactly the same size The plot will be created in a new window We will learn how to format MATLAB plots later Engineering Computation: An Introduction Using MATLAB and Excel

for and while Loops in MATLAB A for loop will be executed a fixed number of times A while loop will continue to be repeated until some condition is satisfied Examples: Consider an amount of money deposited in an interest-bearing account If we want to calculate the value in the account after 10 years, then we would use a for loop If we want to determine how long it will take until the account reaches $100,000, then we would use a while loop Engineering Computation: An Introduction Using MATLAB and Excel

Flow Chart of while Loop The first line of this loop is: while (condition) Last line is: end (calculations) Condition true? Yes No Engineering Computation: An Introduction Using MATLAB and Excel

Condensed Form of while Loop Flowchart while [condition] (Calculations) Engineering Computation: An Introduction Using MATLAB and Excel

Example Consider this loop: How many times will the loop be executed? k = 0; while k < 10 k = k + 2 end How many times will the loop be executed? Initially, k = 0, so the loop is entered Pass #1: k = 2, so execution continues Pass #2: k = 4, so execution continues Pass #3: k = 6, so execution continues Pass #4: k = 8, so execution continues Pass #5, k = 10, so k is not less than 10 and execution ends Engineering Computation: An Introduction Using MATLAB and Excel

while Example Suppose you borrow $10,000 at an interest rate of 6%, compounded monthly. Therefore, each month, the amount owed increases by 0.5% (6% divided by 12) and decreases by the amount of the monthly payment that you make How many months will it take to completely pay back the loan, if the monthly payment is $500 $200 $100 Engineering Computation: An Introduction Using MATLAB and Excel

Flow Chart of Example Define Payment P Balance B = 10,000 Months m = 0 while B > 0 Output m, B B = B*(1.005) B = B – P m = m + 1 Engineering Computation: An Introduction Using MATLAB and Excel

m-File Note the use of the input command: P is assigned the value entered at the prompt given in single quotes Engineering Computation: An Introduction Using MATLAB and Excel

Results Payment = $500: The loan would be repaid in 22 months After the 22nd payment, the balance of $437 would be returned to the borrower Engineering Computation: An Introduction Using MATLAB and Excel

Results Payment = $200 Payment = $100 Engineering Computation: An Introduction Using MATLAB and Excel

Results Try Payment = $45: The calculations will continue until you press ctrl+C to stop execution Engineering Computation: An Introduction Using MATLAB and Excel

Results Check m and B after stopping the calculations: After making payments for more than 1,000,000 years, you owe more money than MATLAB can calculate. This won’t look good on your credit report! Engineering Computation: An Introduction Using MATLAB and Excel

Infinite Loops When using a while loop, there is a danger of encountering an infinite loop Since termination of the loop is dependent upon achieving some condition (in this case, a balance of less than or equal to zero), it is possible that the condition will never be reached, and therefore the looping will continue endlessly In our example, the interest after the first month was $50 (1/2% of $10,000). If the payment was less than $50, then the balance increased every month Engineering Computation: An Introduction Using MATLAB and Excel

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 20; while A <= 50 A = A + 5; m = m + 1; end A m Engineering Computation: An Introduction Using MATLAB and Excel

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 100; while A > 15 A = A/2; m = m + 1; end A m Engineering Computation: An Introduction Using MATLAB and Excel

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 10; while A > 0 A = sqrt(A); m = m + 1; end A m Infinite Loop: the square root will never reach zero Engineering Computation: An Introduction Using MATLAB and Excel