The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Flow chart and Algorithm. Announcement Exam One – Wednesday October 1st – 100 points – Mixture of short answer, problem solving, matching, and maybe a.
Understanding the Three Basic Structures
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
Chapter 1 - An Introduction to Computers and Problem Solving
An Object-Oriented Approach to Programming Logic and Design
Chapter 2 - Problem Solving
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Reference :Understanding Computers
The Flow of Control Among Statements.  Selection Statements  Iterative Statements  Unconditional Branching  Guarded Commands.
Basics of Computer Programming Web Design Section 8-1.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
KISS Programming. What’s so great about computers? They are fast (so they can accomplish much in a short time… spell check a thesis) They don’t make mistakes.
Program Design and Development
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
Chapter 1 Program Design
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
Chapter 3 Planning Your Solution
Intro to Programming CST JavaScript. Objectives Define software Identify the different types of software Differentiate the different types of programming.
Fundamentals of C programming
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
Programming Logic and Design, Second Edition, Comprehensive
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
1 Modeling interactions and behavior Lecturer Dr. Mai Fadel.
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.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Software to Go with the Flow. Software Engineering Step #1 Analyze the problem. Step #1 Analyze the problem. Step #2 Design the architecture of a solution.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
CPS120: Introduction to Computer Science Decision Making in Programs.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '081 Chapter 2 - Problem Solving 2.1 Program Development Cycle 2.2 Programming Tools.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Flowcharts C++ Lab. Algorithm An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task. Input data A step-by-step.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
 Problem Analysis  Coding  Debugging  Testing.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Algorithms and Flowcharts
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Understand Problem Solving Tools to Design Programming Solutions
More on the Selection Structure
Basics of Computer Programming
Understand Problem Solving Tools to Design Programming Solutions
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Basics of Computer Programming
Programming Fundamentals
Problem-Solving and Control Structures By Faith Brenner
Objectives After studying this chapter, you should be able to:
Understanding the Three Basic Structures
Design and Implementation
` Structured Programming & Flowchart
Algorithm and Ambiguity
Problem-Solving and Control Structures By Faith Brenner
ICT Programming Lesson 3:
Basic Concepts of Algorithm
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Presentation transcript:

The Logical Structure of Algorithms

Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain a list of ingredients –Step-by-step instructions on what to do with those ingredients –A recipe provides an algorithm for baking a cake.

Other algorithms When you learned the process for doing long division you learned an algorithm. The Holtrop and Mennen Algorithm, used by naval architects to design the optimum propellers for a ship, contains thousands of steps.

Algorithms are sequential Usually we can think of the instructions in an algorithm as being executed one at a time. They form what is sometimes called a sequential logic. This is only part of the story, however… As a software developer you need to be able to design, manipulate, and implement sequential algorithms

Common patterns There are a small set of patterns that exist in the design of sequential logic. These patterns fall into categories that form the elements of logical structure. They can be combined in myriad ways to form the logical structure of algorithms.myriad A software developer familiar with the design patterns of logical structure can more easily create and modify software.

Plumbers

In the design of the plumbing for a new building, architects have a selection of parts from which to build the system. –T-joints –Elbow joints –Many kinds of valves –Etc. Architects need to know how the individual parts work. And how they fit together.

Elements of logical structure There are only a handful of basic elements that a software developer need learn. In the 1960s Bohm and Jacopinini showed that algorithms are composed of three major structures: –Linear sequences –Branching routines –Loops Modern computer programming focuses on these three elements of logical structure.

Flowcharts

Bohm and Jacopini were not the first to use flowcharts but they were the first to formalize the process. They used a simple system, two symbols –Rectangles to show each step in an algorithm –Diamond-shaped boxes to show a decision step or condititional. We will use one additional symbol, an oval, to mark the beginning and end of an algorithm.

Flowchart symbols

Terminators There should be only one terminator at the beginning of an algorithm and one at the end. Each algorithm should have one entry point (beginning) and one exit point (end).

Linear sequences The simplest element of logical structure is a linear sequence. One instruction follows another in a straight line. No branching No looping

Linear sequence

Linear Sequences Deceptively simple Must meet the following criteria: –Entry and exit conditions need to be clear: What conditions need to exist before the sequence starts? What can we expect the situation to be when the sequence is finished? –Must be complete; don’t leave out necessary steps. –Steps must be in proper order –Each individual instruction must be correct; if one step is wrong, the entire algorithm is wrong. Example: driving directions” –What if you leave out a turn? –What if you say to turn left, when you should have said right?

In short Linear sequences must… –Have clearly stated entry and exit conditions –Be complete –Be organized in the proper order

Selection Sequences Sometimes an algorithm reaches a point where the sequence of steps can go one direction or another (a fork in the road). This is called a selection sequence. Also called a conditional or branching routine

Selection Sequences Consider a student who has a chemistry lab at 2pm on Fridays only: Start If (Today is Friday) Then (Get to lab by 2pm)

Selection Sequence Example

In Summary Selection sequence occurs whenever the path of sequential logic (steps) in an algorithm splits into two or more paths. Each path is called a branch.

Binary and Multiple Branching If there are two possible paths, then the routine is known as binary branching. If there are more than two paths, multiple branching. Binary branching: “Would you like vanilla ice cream?” Multiple branching: “What flavor of ice cream would you like?”

Writing multiple as binary It is possible to write a multiple branching routine as a collection of binary branching routines. Instead of “What flavor of ice cream would you like?” we can ask: –“Would you like vanilla ice cream?” –“Would you like chocolate ice cream?” –“Would you like strawberry ice cream?” –…for all 28 flavors.

Writing multiple as binary Alice does not have an instruction for multiple branching. Most programming languages do not. We write all selection sequences as collections of binary branching routines.

Binary Bypass vs. Binary Choice Two kinds of binary branching: –Binary bypass –Binary choice In binary bypass, an instruction is either executed or bypassed. In binary choice, one of two instructions is chosen.

Binary Bypass

Binary Choice

Binary Choice vs. Binary Bypass The difference is subtle but significant. In binary bypass, it is possible that nothing will occur. In binary choice, one of the two instructions, but not both will occur.

Binary Bypass

Binary Choice

Pseudocode Sometimes we use more formal language to describe algorithms - pseudocode. It looks something like the code in a computer programming language. But it’s only a tool to help understand algorithms.

If/Then/Else In pseudocode, a binary bypass is equivalent to an If/Then instruction: If (today is Friday) Then (get to chemistry lab by 2pm) A binary choice is equivalent to an If/Then/Else instruction: If (Today is Monday, or today is Wednesday, or today is Friday) Then (go to math class) Else (go to history class)

If/Then/Else Basic form: If (condition) Then (one or more instructions) Else (one or more instructions) Must have a condition Any number of instructions can be executed along either branch.

Repetition Sequences - Looping A repetition sequence forms a loop in an algorithm. It forms a branch backward to a previous instruction A part of the algorithm is then repeated.

Repetition Flowchart

Explaining the pseudocode In this algorithm, the word While is used for looping instead of the word If that was used for branching. This tells the computer to loop back to the conditional expression when the block of code following the While is finished. Each time the condition is true the computer will execute the block of code When it is no longer true the block of code will be bypassed and the computer will move on to whatever comes next in the algorithm.

Repetition Flowchart

Control Variables A variable holds a value that can change. This loop has a control variable as its condition. A control variable is a variable whose value controls whether or not a selection sequence will be executed.