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

Similar documents
Homework #2. If (your ID number s last two digits % 6) = 0: 6, 12, 18

A Quick Review of Chapter 1

9/23/2013. Chapter 2. Objectives. Introduction. Objectives (continued) Representing Algorithms. Representing Algorithms

Algorithm Discovery and Design

Chapter 2. The Algorithmic Foundations of. Computer Science INVITATION TO. Computer Science. Tuesday, September 10, 13

Invitation to Computer Science 7th Edition TEST BANK Schneider Gersting

SNS COLLEGE OF ENGINEERING,

REPRESENTING ALGORITHMS. REPRESENTING ALGORITHMS IB DP Computer science Standard Level ICS3U

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Chapter 8 Algorithms 1

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Method & Tools for Program Analysis & Design

Algorithms (Introduction)

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Chapter 5. Algorithms Pearson Addison-Wesley. All rights reserved

FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT III. 2 Marks PROBLEM SOLVING AND OFFICE AUTOMATION

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Repetition Structures

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 1: An Introduction to Computer Science

CS 103 Unit 8b Slides

Administrativia. CS107 Introduction to Computer Science. Readings. Algorithms. Expressing algorithms

REPETITION CONTROL STRUCTURE LOGO

BIL101E: Introduction to Computers and Information systems Lecture 8

Chapter 1 Program Design

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

CS 103 Lecture 4 Slides

ECE 122. Engineering Problem Solving with Java

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Algorithms and Flowcharts

Steps to program development

CSEN 102 Introduction to Computer Science

C++ Programming: From Problem Analysis to Program Design, Third Edition

PROBLEM SOLVING AND OFFICE AUTOMATION. A Program consists of a series of instruction that a computer processes to perform the required operation.

Programming Languages and Program Development Life Cycle Fall Introduction to Information and Communication Technologies CSD 102

COMP 202 Java in one week

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

컴퓨터의기초 7 th Lecture 김현철 (2009 년여름학기 ) 컴퓨터공학부서울대학교. Copyrights 2008 Eom, Hyeonsang All Rights Reserved

Programming II (CS300)

Problem Solving and Program Design - Chapter 1. Cory L. Strope

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Lecture 1: Introduction

Chapter 8. Statement-Level Control Structures

Answers Investigation 5

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Lecture 7 Tao Wang 1

Outline. Program development cycle. Algorithms development and representation. Examples.

Chapter 2 - Control Structures

Flow Chart & Algorithms

Chapter 8. Statement-Level Control Structures

Introduction to the Analysis of Algorithms. Algorithm

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

1. Introduction to Programming

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

CS187 - Science Gateway Seminar for CS and Math

Control Statements. Musa M. Ameen Computer Engineering Dept.

PLD Semester Exam Study Guide Dec. 2018

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Introduction. C provides two styles of flow control:

CSC 273 Data Structures

UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS

Introduction to Programming

CSE 12 Abstract Syntax Trees

Chapter 5: Control Structures II (Repetition)

Algorithms, Part 2 of 3. Problem Solving. Problem Solving (con t) Topics Problem Solving Examples Pseudocode Control Structures

Computer System and programming in C

Chapter 3: The Efficiency of Algorithms

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

Analog Clock. High School Math Alignment. Level 2 CSTA Alignment. Description

Chapter 8 Statement-Level Control Structures

Conditionals and Recursion. Python Part 4

Algorithms, Part 2 of 3. Topics Problem Solving Examples Pseudocode Control Structures

Syntax and Grammars 1 / 21

Time Complexity for Loops

Higher Computing Science Software Design and Development - Programming Summary Notes

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Chapter 3. Iteration

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

Theory of control structures

S/W Programming & Languages

Chapter 8. Statement-Level Control Structures ISBN

(2 1) What does evaluate to? What does evaluate to? What does evaluate to?

Statement level control structures

A linked list grows as data is added to it. In a linked list each item is packaged into a node.

What you learned so far. Loops & Arrays efficiency for statements while statements. Assignment Plan. Required Reading. Objective 2/3/2018

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

++x vs. x++ We will use these notations very often.

Chapter 5: Control Structures

Searching & Sorting in Java Bubble Sort

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Transcription:

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

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

Introduction This chapter uses four problems to discuss algorithms and algorithmic problem solving Multiplying two numbers Searching lists Finding maxima and minima Matching patterns Invitation to Computer Science, C++ Version, Fourth Edition 3

Representing Algorithms Natural language Language spoken and written in everyday life Examples: English, Spanish, Arabic, and so on 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, Fourth Edition 4

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

Representing Algorithms (continued) 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, Fourth 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, Fourth Edition 7

Pseudocode English language constructs modeled to look like statements available in most programming languages Steps presented in a structured manner (numbered, indented, and so on) No fixed syntax for most operations is required Invitation to Computer Science, C++ Version, Fourth 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, Fourth Edition 9

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

Sequential Operations 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, Fourth 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, Fourth Edition 12

Figure 2.3 Algorithm for Computing Average Miles per Gallon Invitation to Computer Science, C++ Version, Fourth 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, Fourth 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, Fourth Edition 15

Conditional Operations

Figure 2.5 Second Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Fourth Edition 17

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, Fourth Edition 18

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, Fourth Edition 19

Loop Operations while repeat until

Conditional and Iterative Operations (continued) Components of a loop Continuation condition Loop body Infinite loop while j > 0 do { } set s to s + a j set j to j 1 The continuation condition never becomes false An error Invitation to Computer Science, C++ Version, Fourth Edition 21

Figure 2.7 Third Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Fourth Edition 22

Conditional and Iterative Operations (continued) 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, Fourth Edition 23

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, Fourth Edition 24

Figure 2.9 Summary of Pseudocode Language Instructions Invitation to Computer Science, C++ Version, Fourth Edition 25

Examples of Algorithmic Problem Solving 1. Go Forth and Multiply: Multiply two numbers using repeated addition 2. Sequential search: Find a particular value in an unordered collection 3. Find maximum: Find the largest value in a collection of data 4. Pattern matching: Determine if and where a particular pattern occurs in a piece of text Invitation to Computer Science, C++ Version, Fourth Edition 26

Example 1: Go Forth and Multiply Task Implement an algorithm to multiply two numbers, a and b, using repeated addition Algorithm outline Create a loop that executes exactly b times, with each execution of the loop adding the value of a to a running total Invitation to Computer Science, C++ Version, Fourth Edition 27

Figure 2.10 Algorithm for Multiplication via Repeated Addition Invitation to Computer Science, C++ Version, Fourth Edition 28

Example 2: Looking, Looking, Looking 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, Fourth Edition 29

Example 2: 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, Fourth Edition 30

Naïve sequential search algorithm

Example 2: 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 (such as 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, Fourth Edition 32

Figure 2.13 The Sequential Search Algorithm Invitation to Computer Science, C++ Version, Fourth Edition 33

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

Example 3: 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, Fourth Edition 35

Example 3: 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, Fourth Edition 36

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

Figure 2.14 Algorithm to Find the Largest Value in a List Invitation to Computer Science, C++ Version, Fourth Edition 38

Example 4: 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, Fourth Edition 39

Example 4: 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, Fourth Edition 40

Example 4: Meeting Your Match (continued) Top-down design When solving a complex problem Create high-level operations in the 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, Fourth Edition 41

Example 4: 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, Fourth Edition 42

Figure 2.16 Final Draft of the Pattern-Matching Algorithm Invitation to Computer Science, C++ Version, Fourth Edition 43

Summary Algorithm design is a first step in developing an algorithm Algorithm design must 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, Fourth Edition 44

Summary Pseudocode is readable, unambiguous, and able to be analyzed 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, Fourth Edition 45