Invitation to Computer Science, Java Version, Second Edition.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
CS107 Introduction to Computer Science Lecture 2.
Program Design and Development
Chapter 2 The Algorithmic Foundations of Computer Science
Chapter 2: Design of Algorithms
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 3: The Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Chapter 1 Program Design
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
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 3 Planning Your Solution
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Simple Program Design Third Edition A Step-by-Step Approach
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
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.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
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
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Introduction To Repetition The for loop
Control Structure Senior Lecturer
Understanding the Three Basic Structures
Algorithm Discovery and Design
Coding Concepts (Basics)
Algorithm Discovery and Design
Three Special Structures – Case, Do While, and Do Until
Algorithm and Ambiguity
ICT Gaming Lesson 2.
The structure of programming
Discovery and Design of Algorithms
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Invitation to Computer Science, Java Version, Second Edition

Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem solving 2

Introduction This chapter discusses algorithms and algorithmic problem solving using three problems: Searching lists Finding smallest and largest items in lists Matching patterns 3

Representing Algorithms What is an algorithm? A series of steps to perform a task – that’s it. A recipe is an algorithm for how to cook chicken enchiladas Directions are an algorithm for how to get to someone’s house 4

Representing Algorithms Natural language Language spoken and written in everyday life English, Spanish Problems with using natural language for algorithms Imprecise Relies on context and experience of the person you are talking to “I saw the man looking through the telescope” 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 6

Pseudocode English language constructs modeled to look like statements available in most programming languages Ex: ADD X + Y No fixed syntax for most operations is required Everyone knows what you are talking about 7

Pseudocode (continued) Less ambiguous and more readable than natural language Emphasis is on the process, not the specific notation Can be easily translated into a programming language 8

Operations Types of algorithmic operations Sequential – input / output, assignment, printing, etc. Conditional – doing one thing or another based on a certain condition Iterative – looping – doing something more than once 9

Sequential Operations (continued) Computation operations Example Set the value of “variable” to “arithmetic expression” X = 10 (set x equal to 10) Variable Named storage location that can hold a data value X in the above example 10

Sequential Operations (continued) Input operations To receive data values from the outside world 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 11

Figure 2.3 Algorithm for Computing Average Miles per Gallon 12 6 Go to gas station 7 Take out loan

Conditional and Iterative Operations Sequential algorithm Executes its instructions in a straight line from top to bottom and then stops Control operations – allow us to get more complex Conditional operations IF X=10 THEN … do some stuff Iterative operations WHILE X < 100 … do some other stuff 13

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, Java Version, Second Edition14

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 15

Conditional and Iterative Operations (continued) Examples while j > 0 do set s to s + a j set j to j - 1 repeat print a k set k to k + 1 until k > n 16

Figure 2.4 Second Version of the Average Miles per Gallon Algorithm 17

Conditional and Iterative Operations (continued) Components of a loop Continuation condition – when do we stop? Loop body – what we want to repeat Infinite loop The continuation condition never becomes false and the loop runs forever This is usually an error Invitation to Computer Science, Java Version, Second Edition18

Figure 2.5 Third Version of the Average Miles per Gallon Algorithm Invitation to Computer Science, Java Version, Second Edition 19

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 Ex: While loop Invitation to Computer Science, Java Version, Second Edition20

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 Ex: Do - While loop Invitation to Computer Science, Java Version, Second Edition21

Figure 2.6 Summary of Pseudocode Language Instructions 22

One other operation - functions Someone (possibly not you) writes a function. This function performs some action, like averaging three numbers, and returns a result to you. You call the function, and it returns a result A function accepts parameters (the numbers to average in this case) and you include these parameters when you call the function. 23

One other operation - functions Average (98, 77, 100) then gives me the average of the 3 numbers Assuming the function was written correctly Essential for creating re-usable code, not reinventing the wheel, etc. Many, many built in functions (methods) in Java. 24

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 25

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 26

Example 1: Looking, Looking, Looking (continued) Correct sequential search algorithm Uses iteration (loops) to simplify the task Handles special cases (like a name not found in the collection) 27

Figure 2.9 The Sequential Search Algorithm 28 Uses the variable Found to exit the iteration as soon as a match is found

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 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, Java Version, Second Edition30

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 – created (usually) by someone else. Don’t reinvent the wheel – many solutions to common problems are available, particularly in Java An important tool in algorithm design and development 31

Figure 2.10 Algorithm to Find the Largest Value in a List 32

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, Java Version, Second Edition33

Example 3: Meeting Your Match (continued) Abstraction Separating high-level view from low-level details Key concept in computer science – “LAYERS” Makes difficult problems intellectually manageable Allows piece-by-piece development of algorithms 34

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 35

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, Java Version, Second Edition36

Figure 2.12 Final Draft of the Pattern-Matching Algorithm Invitation to Computer Science, Java Version, Second Edition 37

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 38

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 39