Chapter 2: Algorithm Discovery and Design

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 2.
Advertisements

CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Introduction to Flowcharting
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Programming Logic and Design Seventh Edition
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
CS107 Introduction to Computer Science
Chapter 2: Algorithm Discovery and Design
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
CS107 Introduction to Computer Science Lecture 2.
Program Design and Development
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Chapter 2 The Algorithmic Foundations of Computer Science
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
Chapter 2: Design of Algorithms
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Pseudocode.
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CSCI-100 Introduction to Computing
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Program design Program Design Process has 2 phases:
Chapter 3: Decisions and Loops
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Introduction to pseudocode
Algorithms & Pseudocode
Understanding the Three Basic Structures
Algorithm Discovery and Design
3 Control Statements:.
Algorithm Discovery and Design
Chapter 8: More on the Repetition Structure
Introduction to Problem Solving and Control Statements
Three Special Structures – Case, Do While, and Do Until
Computer Science Core Concepts
ICT Gaming Lesson 2.
Discovery and Design of Algorithms
Presentation transcript:

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

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

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

Figure 1.2: Algorithm for Adding Two m-digit Numbers Given: 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) Invitation to Computer Science, C++ Version, Third Edition

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. Invitation to Computer Science, C++ Version, Third Edition

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

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

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

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

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

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

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

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 Invitation to Computer Science, C++ Version, Third Edition

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 Print the value of Area Invitation to Computer Science, C++ Version, Third Edition

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

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

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 Invitation to Computer Science, C++ Version, Third Edition

Conditional and Iterative Operations (continued) Perform “looping” behavior; repeating actions until a continuation condition becomes false Loop The repetition of a block of instructions Real power of computer comes from repeating a calculation many times, not just once Invitation to Computer Science, C++ Version, Third Edition

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 Invitation to Computer Science, C++ Version, Third Edition

Second Version of the Average Miles per Gallon Algorithm Start building simple algorithm, make sure it works, then add to it once you are sure it is working correctly. Figure 2.4 Second Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Third Edition

Conditional and Iterative Operations (continued) Components of a loop Continuation condition Loop body Infinite loop The continuation condition never becomes false An error Continuation condition: used to control the loop Loop body: block of instructions --> indented for clarity, required in certain languages (Python) Once the body has been executed, evaluate the continutation condition again Invitation to Computer Science, C++ Version, Third Edition

Third Version of the Average Miles per Gallon Algorithm Again took a working algorithm and added to it. Figure 2.5 Third Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, C++ Version, Third Edition

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 Invitation to Computer Science, C++ Version, Third Edition

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 Invitation to Computer Science, C++ Version, Third Edition

Summary of Pseudocode Language Instructions From 3 primitives: computation, I/O, and Iterative statements, any valid algorithm can be represented! Figure 2.6 Summary of Pseudocode Language Instructions Invitation to Computer Science, C++ Version, Third Edition