ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.

Slides:



Advertisements
Similar presentations
Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes.
Advertisements

How Are Algorithms Developed?
Chapter 2: Problem Solving
Introduction to Flowcharting
Introduction to Flowcharting
Flow Control Analysis & Design Tool: Flowcharts
Introduction to Programming
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Chapter 2 - Problem Solving
زبانهای برنامه سازی برنامه سازی پیشرفته ارائه دهنده دکتر سيد امين حسيني E.mail: Home page:
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
ITEC113 Algorithms and Programming Techniques
Chapter 2- Visual Basic Schneider
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 2: Input, Processing, and Output
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 1 Program Design
Chapter 3 Planning Your Solution
The Program Design Phases
Review Algorithm Analysis Problem Solving Space Complexity
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
PRE-PROGRAMMING PHASE
CSC103: Introduction to Computer and Programming
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Introduction to Programming Lecture Number:. What is Programming Programming is to instruct the computer on what it has to do in a language that the computer.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Design the program Create a detailed description of program –Use charts or ordinary language (pseudocode) Identify algorithms needed –Algorithm: a step-by-step.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
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.
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.
Programming Concepts Chapter 3.
Input, Output, and Processing
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Flowcharts.
PSEUDOCODE C Programming Technique – Firdaus-Harun.com.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Flowcharts. Problem Solving Computer programs are written to solve problems or perform tasks Programmers translate the solutions or tasks into a language.
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
Algorithms & Flowchart
ITEC113 Algorithms and Programming Techniques
1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
IPC144 Session 4 Flowcharts 1. Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify.
Algorithms and Pseudocode
FLOWCHARTING AND ALGORITHMS
1 CS362 High Level Program Design Tool Structure Chart © 2011, Regis University.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Program Design & Development EE 201 C7-1 Spring
Algorithms and Flowcharts
ALGORITHMS AND FLOWCHARTS
Chapter 2: Input, Processing, and Output
Chapter 2- Visual Basic Schneider
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Unit# 9: Computer Program Development
Chapter 2- Visual Basic Schneider
Chapter 2- Visual Basic Schneider
How Are Algorithms Developed?
Chapter 2: Input, Processing, and Output
Basic Concepts of Algorithm
Programming Logic and Design Eighth Edition
Presentation transcript:

ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp4 MATLAB Programming-1

ENGR-25_Programming-1.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals  Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output  Write Programs that Employ LOOPing Processes For → No. Loops know a priori while → Loop Terminates based on Logic Criteria  Use the MATLAB DeBugger to Correct Errors (time permitting)

ENGR-25_Programming-1.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods What’s an “Algorithm”  A postage stamp issued by the USSR in 1983 to commemorate the 1200th anniversary of Muhammad al-Khowarizmi, after whom algorithms are named.

ENGR-25_Programming-1.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Algorithm Defined  ALGORITHM  an ORDERED SEQUENCE of PRECISELY defined instructions that performs some task in a finite amount of time. ORDERED means that the instructions can be NUMBERED, but an algorithm must have the ability to ALTER the order of its instructions using a CONTROL structure.  There are three categories of algorithmic operations → Next Slide:

ENGR-25_Programming-1.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Algorithmic Operations 1.SEQUENTIAL OPERATIONS → Instructions executed in order 2.CONDITIONAL OPERATIONS → Control structures that first ask a question to be answered with a true/false response and then select the next instruction based on the answer 3.ITERATIVE OPERATIONS (LOOPS): Control structures that repeat the execution of a block of instructions

ENGR-25_Programming-1.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structured Programming  STRUCTURED PROGRAMMING  A technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure withOUT UNconditional branches to higher levels of the structure. Three types of control flow are used: (1) sequential, (2) test, and (3) iteration.

ENGR-25_Programming-1.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structured Program Advantages 1.Structured programs are easier to write because the programmer can study the overall problem first and then deal with the details later. 2.Modules (functions) written for one application can be used for other applications (this is called reusable code). 3.Structured programs are easier to debug because each module is designed to perform just one task and thus it can be tested separately from the other modules.

ENGR-25_Programming-1.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structured Program Advantages 4.Structured programming is effective in a teamwork environment because several people can work on a common program, each person developing one or more modules. 5.Structured programs are easier to understand and modify, especially if meaningful names are chosen for the modules and if the documentation clearly identifies the module’s task.

ENGR-25_Programming-1.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Developing Computer Solns 1.State the problem concisely Use Math Eqns/Relns Whenever possible 2.Specify the data to be used by the program. This is the “input.” In physical Problems These are typically –Boundary Conditions –Initial Conditions –Constraints 3.Specify the information to be generated by the program. This is the “output.”

ENGR-25_Programming-1.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Developing Computer Solns cont 4.Work through the solution steps by hand or with a calculator; Use a simpler set of data if necessary Test for EXTREME cases with Input or Output = ±∞, 0, ±1 5.Write and Run the program Correct Compile/Execution Time Errors Missing Comma or Parens, MisTyped Var names, etc. Correct RunTime Errors e.g., NaN ’s, inf ’s, Divide-by-Zero, etc.

ENGR-25_Programming-1.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Developing Computer Solns cont 6.Check the output of the program with your hand solution(s) 7.Run the program with your input data and perform a reality check on the output. Output MUST NOT Violate the Laws of Physics; e.g., in Statics (ENGR36) –a CABLE canNOT support NEGATIVE Tension (you can’t PUSH something with a ROPE) –Objects canNOT have NEGATIVE Weight or Mass (at least not until we figure out AntiGravity methods) In ENGR43 Pnysical Resistors, Capacitors, and Inductors can NOT have NEGATIVE Values

ENGR-25_Programming-1.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Developing Computer Solns cont 8.If you will use the program as a general tool in the future, test it by running it for a range of reasonable data values; Perform a Reality Check on the results

ENGR-25_Programming-1.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Effective Documentation 1.Proper selection of variable names to reflect the quantities they represent Use DESCRIPTIVE and/or LONG names 2.Use of comments within the program Also helps CLARIFY the WRITING of the Code 3.Use of structure charts Like an ORG Chart for your Software Program –Structure charts show how variables pass between modules in a computer program.

ENGR-25_Programming-1.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anatomy of a Structure Chart Module the called module

ENGR-25_Programming-1.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example: Pizza Pricing Problem  Write a program to find the UNIT price for a given pizza. The size of the pizza will be provided in inches. The result must be cost/inch 2

ENGR-25_Programming-1.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Effective Documentation cont 4.Use of flowcharts Each flowchart has one starting point and one or more ending points that are drawn with a rounded rectangle or oval. –Steps, actions, or tasks are drawn with rectangles. It helps to use verbs in describing the steps. –Decisions are drawn with diamonds with labels at the exits. The arrows show the order the steps are taken. The shapes in the flowchart are often numbered to make it easier to refer to them Drawing a flowchart of a software component makes the flow of control easier to understand

ENGR-25_Programming-1.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowCharting Example  Basic House Painting Note the Cascade of Decision-Diamonds

ENGR-25_Programming-1.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Effective Documentation cont 5.A verbal description of the program, often in pseudocode. Generic way of describing an algorithm, without use of any specific programming language Helps programmers to PLAN an algorithm Not an actual programming Language, but may borrow syntax from popular programming languages Styles vary

ENGR-25_Programming-1.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PseudoCode Example  Example Problem: Calculate the bill when someone buys a specific no. of some item:  PseudoCode: PROMPT for number of items being purchased READ number of items being purchased PROMPT for price per item READ price per item CALCULATE subtotal CALCULATE tax CALCULATE total DISPLAY total

ENGR-25_Programming-1.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PsuedoCode: Common Terms  To Describe input, output, computations, etc., the following terms are often used: Input: INPUT, READ, GET Output: PRINT, DISPLAY, SHOW Compute: COMPUTE, CALCULATE, DETERMINE Initialize: SET, INIT Add one: INCREMENT, BUMP, STEP Decisions: TEST, IF/THEN/ELSE, WHILE/DO

ENGR-25_Programming-1.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PsuedoCode Algorithms  Ensure that the the task is completely specified  Issues to Consider What data is known before the program runs? What data must be input by the user? What computations will be performed on the data? What data will be output (displayed) to the user?

ENGR-25_Programming-1.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PsuedoCode Example  Real World Problem: Calculate the allowance for two children, based upon 75¢ per year-of-age  Known Values Pay Rate = 75 ¢/year  Inputs Child Age  Calculations Allowance = Age x Rate  Outputs Allowances for each child

ENGR-25_Programming-1.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PsuedoCode Example cont  Careful PseudoCode Algorithm: PROMPT for Age of Child1 READ Age of Child1 PROMPT for Age of Child2 READ Age of Child2 CALCULATE –Allowance for Child1 = [Age of Child1] x Rate CALCULATE –Allowance for Child2 = [Age of Child2] x Rate DISPLAY Allowance for Child1 DISPLAY Allowance for Child2

ENGR-25_Programming-1.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Program Design - FlowChart  Flowchart  a graphical representation of algorithms Rectangle is used for calculations Parallelogram is used for input and output Circle is used as connector Diamond is used as decision Symbols are connected by arrows to represent the order of the operations; i.e., the direction of program flow

ENGR-25_Programming-1.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowChart Symbols – Calc’s  Calculations (e.g. arithmetic expressions) are shown in rectangles Example: Total = Cost + Tax Example Num = Num + 1 –add one to the current value of Num and make that the new value of Num Total = Cost + TaxNum = Num + 1

ENGR-25_Programming-1.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowChart Symbols – In/Out Put  Data input and output are shown in parallelograms  Input  a read operation of data from a peripheral device to memory e.g., a user typing in data at a keyboard READ Num WRITE Num  Output  a write operation of data to a peripheral device from memory e.g., data displayed to the monitor

ENGR-25_Programming-1.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowChart Symbols – Decisions  Decisions are shown within Diamonds and specify a condition to be tested  Based on the condition being TRUE or FALSE, the next operation will be determined  A decision is composed of : A condition An operation to be done if condition is TRUE Possibly an operation to be done if condition is FALSE Gross > Rate = 0.31 Rate = 0.28 True False

ENGR-25_Programming-1.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowChart Symbols – Start/End  Every algorithm starts somewhere and terminates somewhere  Every flowchart must have one start symbol and one end symbol  Start and Stop symbols are ovals A start symbol denotes the start of the algorithm An end symbol indicates the algorithm has terminated square = num x num input num print square start stop

ENGR-25_Programming-1.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FlowChart Example  Sequential-FlowChart for the Previous Pseudocode Algorithm: Allow1 = Age1 x Rate input Age1 start Print Allow1 stop prompt Age1 Allow2 = Age2 x Rate Print Allow2 input Age2 prompt Age2 PROMPT Age of Child1 READ for Age of Child1 PROMPT Age of Child2 READ for Age of Child2 CALCULATE –Allowance for Child1 = Age of Child1 x Rate CALCULATE –Allowance for Child2 = Age of Child2 x Rate DISPLAY Allowance for Child1 DISPLAY Allowance for Child2

ENGR-25_Programming-1.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Common FlowChart Symbols Terminator. Shows the starting and ending points of the program. A terminator has flowlines in only one direction, either in (a stop node) or out (a start node). Data Input or Output. Allows the user to inputdata and results to be displayed. Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. Decision. The diamond indicates a decision structure. A diamond always has two flowlines out. One flowlineout is labeled the “yes” branch and the other is labeled the “no” branch. Predefined Process. One statement denotes a group of previously defined statements. For instance, “Calculate m!” indicates that the program executes the necessary commands to compute m factorial. Connector. Connectors avoid crossing flowlines, making the flowchart easier to read. Connectors indicate where flowlines are connected. Connectors come in pairs, one with a flowline in and the other with a flowline out. Off-page connector. Even fairly small programs can have flowcharts that extend several pages. The off-page connector indicates the continuation of the flowchart on another page. Just like connectors, off-page connectors come in pairs. Flowline. Flowlines connect the flowchart symbols and show the sequence of operations during the program execution. Common Flowchart Symbols

ENGR-25_Programming-1.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Top-Down Dsgn: Structure Chrt  As we Have Seen a FlowChart: Details exactly HOW a program will do each task  In contrast, the next design tool, the Structure Chart: Shows only WHAT tasks your program will do (not HOW it will complete them)

ENGR-25_Programming-1.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structure Chart  Structure charts are Hierarchical diagrams → A form of an “Org Chart” They diagram the OverAll program (organization) Structure They show the Relationship between all the Tasks (workers) in the program They indicate which Data (Information) is shared by the tasks (workers)

ENGR-25_Programming-1.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Creating Structure Charts  A large program design is first broken into tasks  Tasks are repeatedly divided into even smaller subtasks Rectangles are used to represent tasks/subtasks within the program Lines are used to hierarchically connect the tasks/subtasks –Subtasks are diagrammed below the task that they are part of Task SubTask1SubTask2

ENGR-25_Programming-1.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structure Charts – Data Flow  Passing of data between tasks is shown by arrows going up from or down to the task’s rectangle An upward arrow indicates that the data value has been set inside the task and is being passed out for use by other tasks A downward arrow indicates a data value previously set in some other task is now being passed into this task Data may also be passed both into a task (downward arrow), modified, and passed back out again (upward arrow). Task SubTask1SubTask2 Data1

ENGR-25_Programming-1.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Sample Structure Chart  Given a generic program that reads some data from the user, runs some calculations using that data, and displays results to the user, the structure chart could look like this: main Get_InputProcess_DataDisplay_Results Data Result Data Result

ENGR-25_Programming-1.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Structure Chart: Sequential Program  Using our previous sequential program design example (calculate the two childrens’ allowances, based upon 75¢ per year-age), the structure chart might be: main Get_AgesCalculate_AllowancesDisplay_Allowances Age1, Age2 Allow1, Allow2 Age1, Age2 Allow1, Allow2

ENGR-25_Programming-1.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Summary – Structure Charting  Put the name of the program in a rectangle root of an upside-down tree.  Determine the main subtasks that must be performed by the program to solve the problem. Put main subtasks in rectangles below the root, and draw a connecting line from the root to each subtask.

ENGR-25_Programming-1.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Summary – Structure Charting  Examine each subtask individually, and break them down into even smaller tasks Put subtasks in rectangles below the task that they are part of, and draw a connecting line from task to subtask.  Repeat until the BOTTOM TASKS of the tree are VERY SIMPLE to complete

ENGR-25_Programming-1.ppt 39 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today LBL Org Chart  In Orgs or SW-Programs → Form FOLLOWS Function

ENGR-25_Programming-1.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Appendix

ENGR-25_Programming-1.ppt 41 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Yet More Org Charts