Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition



Similar documents
Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Algorithms are the threads that tie together most of the subfields of computer science.

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

COMPUTER SCIENCE (5651) Test at a Glance

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Sources: On the Web: Slides will be available on:

Total Quality Management (TQM) Quality, Success and Failure. Total Quality Management (TQM) vs. Process Reengineering (BPR)

JavaScript: Control Statements I

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Chapter 13: Program Development and Programming Languages

ALGORITHMS AND FLOWCHARTS

Pseudo code Tutorial and Exercises Teacher s Version

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Moving from CS 61A Scheme to CS 61B Java

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Flowchart Techniques

Computer Programming I

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

PYTHON Basics

Exploring Algorithms with Python

Computer Programming I

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

KS3 Computing Group 1 Programme of Study hours per week

COWLEY COLLEGE & Area Vocational Technical School

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 13: Program Development and Programming Languages

Two-way selection. Branching and Looping

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Domains and Competencies

Chapter 12 Programming Concepts and Languages

NEW YORK CITY COLLEGE OF TECHNOLOGY/CUNY Computer Systems Technology Department. COURSE: CST2403 C++ Programming Part 1 ( 4 hours, 3 credits )

Modeling, Computers, and Error Analysis Mathematical Modeling and Engineering Problem-Solving

Computational Mathematics with Python

MATLAB Programming. Problem 1: Sequential

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

6. Control Structures

VHDL Test Bench Tutorial

Chapter One Introduction to Programming

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Computational Mathematics with Python

Texas Essential Knowledge and Skills Correlation to Video Game Design Foundations 2011 N Video Game Design

Software development and programming. Software

Excel: Introduction to Formulas

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Programming and Software Development CTAG Alignments

Chapter 14. Programming and Languages. McGraw-Hill/Irwin. Copyright 2008 by The McGraw-Hill Companies, Inc. All rights reserved.

Python Programming: An Introduction to Computer Science

El Dorado Union High School District Educational Services

Basic Programming and PC Skills: Basic Programming and PC Skills:

Curriculum Map. Discipline: Computer Science Course: C++

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Higher Computing Science Course Assessment Specification (C716 76)

Computational Mathematics with Python

Fundamentals of Programming and Software Development Lesson Objectives

Regular Expressions and Automata using Haskell

I PUC - Computer Science. Practical s Syllabus. Contents

MICHIGAN TEST FOR TEACHER CERTIFICATION (MTTC) TEST OBJECTIVES FIELD 050: COMPUTER SCIENCE

VB.NET Programming Fundamentals

Notes on Algorithms, Pseudocode, and Flowcharts

Semester Review. CSC 301, Fall 2015

Using Visual Logic with Pseudocode to Teach an Introductory Programming Course

Problem Solving Basics and Computer Programming

Notes on Assembly Language

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Programming in Java Course Technology, a part of Cengage Learning.

Python for Rookies. Example Examination Paper

#820 Computer Programming 1A

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

Software Development Processes. Software Life-Cycle Models. Process Models in Other Fields. CIS 422/522 Spring

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Introduction to Programming System Design. CSCI 455x (4 Units)

Software Development Life Cycle

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Visual Logic Instructions and Assignments

CS 40 Computing for the Web

Python Programming: An Introduction to Computer Science

50 Computer Science MI-SG-FLD050-02

Object Oriented Software Design

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Lecture 2 Notes: Flow of Control

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

Programming Languages The McGraw-Hill Companies, Inc. All rights reserved.

Writing Control Structures

Introduction to MIPS Assembly Programming

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

Creating a Simple Macro

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

CS 241 Data Organization Coding Standards

Repetition Using the End of File Condition

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Transcription:

Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition

Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem solving Invitation to Computer Science, C++ Version, Third Edition 2

Introduction This chapter discusses algorithms and algorithmic problem solving using three problems: Searching lists Finding maxima and minima Matching patterns Invitation to Computer Science, C++ Version, Third Edition 3

Representing Algorithms Natural language Language spoken and written in everyday life Examples: English, Spanish, Arabic, etc. Problems with using natural language for algorithms Verbose Imprecise Relies on context and experiences to give precise meaning to a word or phrase Invitation to Computer Science, C++ Version, Third Edition 4

Figure 2.1 The Addition Algorithm of Figure 1.2 Expressed in Natural Language Invitation to Computer Science, C++ Version, Third Edition 5

Representing Algorithms High-level programming language Examples: C++, Java Problem with using a high-level programming language for algorithms During the initial phases of design, we are forced to deal with detailed language issues Invitation to Computer Science, C++ Version, Third Edition 6

Figure 2.2 The Beginning of the Addition Algorithm of Figure 1.2 Expressed in a High-Level Programming Language Invitation to Computer Science, C++ Version, Third Edition 7

Pseudocode English language constructs modeled to look like statements available in most programming languages Steps presented in a structured manner (numbered, indented, etc.) No fixed syntax for most operations is required Invitation to Computer Science, C++ Version, Third Edition 8

Pseudocode (continued) Less ambiguous and more readable than natural language Emphasis is on process, not notation Well-understood forms allow logical reasoning about algorithm behavior Can be easily translated into a programming language Invitation to Computer Science, C++ Version, Third Edition 9

Sequential Operations Types of algorithmic operations Sequential Conditional Iterative Invitation to Computer Science, C++ Version, Third Edition 10

Sequential Operations (continued) Computation operations Example Set the value of variable to arithmetic expression Variable Named storage location that can hold a data value Invitation to Computer Science, C++ Version, Third Edition 11

Sequential Operations (continued) Input operations To receive data values from the outside world Example Get a value for r, the radius of the circle Output operations To send results to the outside world for display Example Print the value of Area Invitation to Computer Science, C++ Version, Third Edition 12

Figure 2.3 Algorithm for Computing Average Miles per Gallon Invitation to Computer Science, C++ Version, Third Edition 13

Conditional and Iterative Operations Sequential algorithm Also called straight-line algorithm Executes its instructions in a straight line from top to bottom and then stops Control operations Conditional operations Iterative operations Invitation to Computer Science, C++ Version, Third Edition 14

Conditional and Iterative Operations (continued) Conditional operations Ask questions and choose alternative actions based on the answers Example if x is greater than 25 then else print x print x times 100 Invitation to Computer Science, C++ Version, Third Edition 15

Conditional and Iterative Operations (continued) Iterative operations Perform looping behavior; repeating actions until a continuation condition becomes false Loop The repetition of a block of instructions Invitation to Computer Science, C++ Version, Third Edition 16

Conditional and Iterative Operations (continued) Examples while j > 0 do set s to s + a j set j to j - 1 repeat until k > n print a k set k to k + 1 Invitation to Computer Science, C++ Version, Third Edition 17

Figure 2.4 Second Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Third Edition 18

Conditional and Iterative Operations (continued) Components of a loop Continuation condition Loop body Infinite loop The continuation condition never becomes false An error Invitation to Computer Science, C++ Version, Third Edition 19

Figure 2.5 Third Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Third Edition 20

Conditional and Iterative Operations Pretest loop Continuation condition tested at the beginning of each pass through the loop It is possible for the loop body to never be executed While loop Invitation to Computer Science, C++ Version, Third Edition 21

Conditional and Iterative Operations (continued) Posttest loop Continuation condition tested at the end of loop body Loop body must be executed at least once Do/While loop Invitation to Computer Science, C++ Version, Third Edition 22

Figure 2.6 Summary of Pseudocode Language Instructions Invitation to Computer Science, C++ Version, Third Edition 23

Example 1: Looking, Looking, Looking Examples of algorithmic problem solving Sequential search: find a particular value in an unordered collection Find maximum: find the largest value in a collection of data Pattern matching: determine if and where a particular pattern occurs in a piece of text Invitation to Computer Science, C++ Version, Third Edition 24

Example 1: Looking, Looking, Looking (continued) Task Find a particular person s name from an unordered list of telephone subscribers Algorithm outline Start with the first entry and check its name, then repeat the process for all entries Invitation to Computer Science, C++ Version, Third Edition 25

Example 1: Looking, Looking, Looking (continued) Algorithm discovery Finding a solution to a given problem Naïve sequential search algorithm For each entry, write a separate section of the algorithm that checks for a match Problems Only works for collections of exactly one size Duplicates the same operations over and over Invitation to Computer Science, C++ Version, Third Edition 26

Example 1: Looking, Looking, Looking (continued) Correct sequential search algorithm Uses iteration to simplify the task Refers to a value in the list using an index (or pointer) Handles special cases (like a name not found in the collection) Uses the variable Found to exit the iteration as soon as a match is found Invitation to Computer Science, C++ Version, Third Edition 27

Figure 2.9 The Sequential Search Algorithm Invitation to Computer Science, C++ Version, Third Edition 28

Example 1: Looking, Looking, Looking (continued) The selection of an algorithm to solve a problem is greatly influenced by the way the data for that problem are organized Invitation to Computer Science, C++ Version, Third Edition 29

Example 2: Big, Bigger, Biggest Task Find the largest value from a list of values Algorithm outline Keep track of the largest value seen so far (initialized to be the first in the list) Compare each value to the largest seen so far, and keep the larger as the new largest Invitation to Computer Science, C++ Version, Third Edition 30

Example 2: Big, Bigger, Biggest (continued) Once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms Library A collection of useful algorithms An important tool in algorithm design and development Invitation to Computer Science, C++ Version, Third Edition 31

Example 2: Big, Bigger, Biggest (continued) Find Largest algorithm Uses iteration and indices like previous example Updates location and largest so far when needed in the loop Invitation to Computer Science, C++ Version, Third Edition 32

Figure 2.10 Algorithm to Find the Largest Value in a List Invitation to Computer Science, C++ Version, Third Edition 33

Example 3: Meeting Your Match Task Find if and where a pattern string occurs within a longer piece of text Algorithm outline Try each possible location of pattern string in turn At each location, compare pattern characters against string characters Invitation to Computer Science, C++ Version, Third Edition 34

Example 3: Meeting Your Match (continued) Abstraction Separating high-level view from low-level details Key concept in computer science Makes difficult problems intellectually manageable Allows piece-by-piece development of algorithms Invitation to Computer Science, C++ Version, Third Edition 35

Example 3: Meeting Your Match (continued) Top-down design When solving a complex problem: Create high-level operations in first draft of an algorithm After drafting the outline of the algorithm, return to the high-level operations and elaborate each one Repeat until all operations are primitives Invitation to Computer Science, C++ Version, Third Edition 36

Example 3: Meeting Your Match (continued) Pattern-matching algorithm Contains a loop within a loop External loop iterates through possible locations of matches to pattern Internal loop iterates through corresponding characters of pattern and string to evaluate match Invitation to Computer Science, C++ Version, Third Edition 37

Figure 2.12 Final Draft of the Pattern-Matching Algorithm Invitation to Computer Science, C++ Version, Third Edition 38

Summary Algorithm design is a first step in developing an algorithm Must also: Ensure the algorithm is correct Ensure the algorithm is sufficiently efficient Pseudocode is used to design and represent algorithms Invitation to Computer Science, C++ Version, Third Edition 39

Summary Pseudocode is readable, unambiguous, and analyzable Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution Abstraction is a key tool for good design Invitation to Computer Science, C++ Version, Third Edition 40