Top Banner
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring 2005
25

Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Chapter 2: Algorithm Discovery and Design

Invitation to Computer Science,

C++ Version, Third Edition

Slides added or modified by Shannon Steinfadt, Spring 2005

Page 2: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 2

Objectives

In this chapter, you will learn about:

Representing algorithms

Examples of algorithmic problem solving

Page 3: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 3

Introduction

This chapter discusses algorithms and algorithmic problem solving using three problems:

Searching lists

Finding maxima and minima

Matching patterns

Page 4: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 4

Figure 1.2: Algorithm for Adding Two m-digit NumbersGiven: m ≥ 1 and two positive numbers each

containing m digits, am-1 am-2 … a0 and bm-1 bm-2 … b0

Wanted: cm cm-1 cm-2 … c0, where cm cm-1 cm-2 … c0 = (am-1 am-2 … a0) + (bm-1 bm-2 … b0)

Page 5: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 5

Figure 1.2:Alg. for Adding Two m-digit Numbers (con’t)Step 1 Set the value of carry to 0.Step 2 Set the value of i to 0.Step 3 While the value of i is less than or equal to m-1, repeat the instructions in steps 4 through 6

Step 4 Add the two digits ai and bi to the current value of carry to get ci.

Step 5 If ci ≥ 10, then reset ci to (ci - 10) and reset the value of carry to 1; otherwise, set the new value of carry to 0.

Step 6 Add 1 to i, effectively moving one column to the left.

Step 7 Set cm to the value of carry.

Step 8 Print out the final answer, cm cm-1 cm-2 … c0.Step 9 Stop.

Page 6: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 6

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

Page 7: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 7

Figure 2.1The Addition Algorithm of Figure 1.2 Expressed in Natural Language

Page 8: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 8

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

Page 9: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 9

Figure 2.2The Beginning of the Addition Algorithm of Figure 1.2 Expressed

in a High-Level Programming Language

Page 10: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 10

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

Page 11: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 11

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

Page 12: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 12

Types of Algorithmic Operations

Sequential

Conditional

Iterative

Page 13: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 13

Sequential Operations

3 types of sequential operations: Computation, Input, and Output

Computation operations

Example

Set the value of “variable” to “arithmetic expression”

Variable

Named storage location that can hold a data value

Page 14: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 14

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

Page 15: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 15

Figure 2.3Algorithm for Computing Average Miles per Gallon

Page 16: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 16

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

Page 17: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 17

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

print x

else

print x times 100

Page 18: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 18

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

Page 19: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 19

Conditional and Iterative Operations (continued)

Examples while j > 0 do

set s to s + aj

set j to j - 1

repeat

print ak

set k to k + 1

until k > n

Page 20: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 20

Figure 2.4Second Version of the Average Miles per Gallon Algorithm

Page 21: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 21

Conditional and Iterative Operations (continued) Components of a loop

Continuation condition

Loop body

Infinite loop

The continuation condition never becomes false

An error

Page 22: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 22

Figure 2.5Third Version of the Average Miles per Gallon Algorithm

Page 23: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 23

Conditional and Iterative Operations Pre-test 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

Page 24: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 24

Conditional and Iterative Operations (continued)

Post-test loop

Continuation condition tested at the end of loop body

Loop body must be executed at least once

Do/While loop

Page 25: Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Slides added or modified by Shannon Steinfadt, Spring.

Invitation to Computer Science, C++ Version, Third Edition 25

Figure 2.6Summary of Pseudocode Language Instructions