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

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

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

Algorithm Discovery and Design

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

A Quick Review of Chapter 1

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

Invitation to Computer Science 7th Edition TEST BANK Schneider Gersting

Chapter 8 Algorithms 1

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

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

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

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

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

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

Engineering program development. Edited by Péter Vass

Instructions PLEASE READ (notice bold and underlined phrases)

ENGR 1181 MATLAB 09: For Loops 2

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Programming in C++ PART 2

BIL101E: Introduction to Computers and Information systems Lecture 8

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

SNS COLLEGE OF ENGINEERING,

Instructor s Manual. Perl How to Program

LECTURE 5 Control Structures Part 2

Flow Charts. Visual Depiction of Logic Flow

Chapter 2 - Control Structures

10/9/2012. Comparison and Logical Operators The if Statement The if else Statement Nested if Statements The switch case. switch case Statement

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Answers Investigation 5

SET DEFINITION 1 elements members

Organisation. Assessment

CSC 121 Spring 2017 Howard Rosenthal

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

CSE 230 Computer Science II (Data Structure) Introduction

Loops / Repetition Statements

Name Date Class F 63 H 0.63 B 2.5 D G 6.3 I A 18 C F 60 H 0.6 B 1.8 D 0.018

CSC 273 Data Structures

Repetition Structures

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

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

Test #2 October 8, 2015

Math 1113 Notes - Functions Revisited

Section A. 1. is the alignment where the text is aligned from the left edge and ragged from the right edge.

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

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

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

Lesson Plan -- Multiplying and Dividing Integers

REPETITION CONTROL STRUCTURE LOGO

CPSC 340: Machine Learning and Data Mining. Density-Based Clustering Fall 2016

LEC03: Decisions and Iterations

Class 8 ALGORITHMS AND FLOWCHARTS. The City School

Information Science 1

Computer System and programming in C

Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 6 Programming the LC-3

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction to Algorithms and Complexity Theory

Solve the matrix equation AX B for X by using A.(1-3) Use the Inverse Matrix Calculator Link to check your work

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

Real Numbers. Rational Numbers (0, 3, -1, ½⅔,.524, etc..) Fractions (1/2, -4/3, 10%,.25, etc..) Negative Integers {.

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

TABLE OF CONTENTS. About Finish Line PA Core Math 5. UNIT 1: Big Ideas from Grade 7 7 UNIT 1 REVIEW 38. UNIT 2: The Number System 43 UNIT 2 REVIEW 58

ICT GRAND WORKSHEET- CLASS-5. Section A. 2 nd Term Chapters 5, 6, 7, 8 and 9. Fill in the blanks with the correct answers.

Lecture 1: Introduction

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

12/30/2013 S. NALINI,AP/CSE

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Class Notes, 3/21/07, Operating Systems

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165

ALGORITHMS AND FLOWCHARTS

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Searching Algorithms/Time Analysis

CS 103 Unit 8b Slides

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

SECTION 5.1. Sequences

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Problem Set 9 Solutions

Gateway Regional School District VERTICAL ALIGNMENT OF MATHEMATICS STANDARDS Grades 3-6

Introduction to Analysis of Algorithms

CSCE 314 Programming Languages

Floating Point Numbers

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

2.1 Introduction to Fractions and Mixed Numbers. A Introduction to Fractions SAMPLE. 3 7 Figure 1. The whole candy bar can be represented as 7 7.

c. Show that each unit fraction, with, can be written as a sum of two or more different unit fractions.

CS261: Problem Set #2

Sorting & Growth of Functions

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Lecture #3: PageRank Algorithm The Mathematics of Google Search

Introduction. C provides two styles of flow control:

Introduction to the Analysis of Algorithms. Algorithm

11 Data Structures Foundations of Computer Science Cengage Learning

Question Points Score TOTAL 50

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Why Is Repetition Needed?

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Transcription:

Chapter 2 The Algorithmic Foundations of Computer Science INVITATION TO Computer Science 1

Objectives After studying this chapter, students will be able to: Explain the benefits of pseudocode over natural language or a programming language Represent algorithms using pseudocode Identify algorithm statements as sequential, conditional, or iterative Define abstraction and top-down design, and explain their use in breaking down complex problems Invitation to Computer Science, 6th Edition 2

Objectives (continued) After studying this chapter, students will be able to: Illustrate the operation of sample algorithms multiplication by repeated addition sequential search of a collection of values finding the maximum element in a collection finding a pattern string in a larger piece of text Invitation to Computer Science, 6th Edition 3

Introduction Algorithms for everyday may not be suitable for computers to perform (as in Chapter 1) Algorithmic problem solving focuses on algorithms suitable for computers Pseudocode is a tool for designing algorithms This chapter will use a set of problems to illustrate algorithmic problem solving Invitation to Computer Science, 6th Edition 4

Representing Algorithms Pseudocode is used to design algorithms Natural language is: expressive, easy to use verbose, unstructured, and ambiguous Programming languages are: structured, designed for computers grammatically fussy, cryptic Pseudocode lies somewhere between these two Invitation to Computer Science, 6th Edition 5

Invitation to Computer Science, 6th Edition 6

Invitation to Computer Science, 6th Edition 7

Representing Algorithms (continued) Sequential operations perform a single task Computation: a single numeric calculation Input: gets data values from outside the algorithm Output: sends data values to the outside world A variable is a named location to hold a value A sequential algorithm is made up only of sequential operations Example: computing average miles per gallon Invitation to Computer Science, 6th Edition 8

Invitation to Computer Science, 6th Edition 9

Representing Algorithms (continued) Control operation: changes the normal flow of control Conditional statement: asks a question and selects among alternative options 1. Evaluate the true/false condition 2. If the condition is true, then do the first set of operations and skip the second set 3. If the condition is false, skip the first set of operations and do the second set Example: check for good or bad gas mileage Invitation to Computer Science, 6th Edition 10

11

Invitation to Computer Science, 6th Edition 12

Representing Algorithms (continued) Iteration: an operation that causes looping, repeating a block of instructions While statement repeats while a condition remains true continuation condition: a test to see if while loop should continue loop body: instructions to perform repeatedly Example: repeated mileage calculations Invitation to Computer Science, 6th Edition 13

14

Invitation to Computer Science, 6th Edition 15

Representing Algorithms (continued) Do/while, alternate iterative operation continuation condition appears at the end loop body always performed at least onc Primitive operations: sequential, conditional, and iterative are all that is needed Invitation to Computer Science, 6th Edition 16

17

18

Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply Given two nonnegative integer values, a 0, b 0, compute and output the product (a b) using the technique of repeated addition. That is, determine the value of the sum a + a + a +... + a (b times). Invitation to Computer Science, 6th Edition 19

Examples of Algorithmic Problem Solving Get input values Get values for a and b Compute the answer Loop b times, adding each time* Output the result Print the final value* * steps need elaboration Invitation to Computer Science, 6th Edition 20

Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply (continued) Loop b times, adding each time Set the value of count to 0 While (count < b) do the rest of the loop* Set the value of count to count + 1 End of loop * steps need elaboration Invitation to Computer Science, 6th Edition 21

Examples of Algorithmic Problem Solving Loop b times, adding each time Set the value of count to 0 Set the value of product to 0 While (count < b) do Set the value of product to (product + a) Set the value of count to count + 1 End of loop Output the result Print the value of product Invitation to Computer Science, 6th Edition 22

Invitation to Computer Science, 6th Edition 23

Examples of Algorithmic Problem Solving Example 2: Looking, Looking, Looking Assume that we have a list of 10,000 names that we define as N 1, N 2, N 3,..., N 10,000, along with the 10,000 telephone numbers of those individuals, denoted as T 1, T 2, T 3,..., T 10,000. To simplify the problem, we initially assume that all names in the book are unique and that the names need not be in alphabetical order. Invitation to Computer Science, 6th Edition 24

Examples of Algorithmic Problem Solving Example 2: Looking, Looking, Looking (continued) Three versions here illustrate algorithm discovery, working toward a correct, efficient solution A sequential algorithm (no loops or conditionals) An incomplete iterative algorithm A correct algorithm Invitation to Computer Science, 6th Edition 25

Invitation to Computer Science, 6th Edition 26

Invitation to Computer Science, 6th Edition 27

Invitation to Computer Science, 6th Edition 28

Examples of Algorithmic Problem Solving Example 3: Big, Bigger, Biggest A building-block algorithm used in many libraries Library: A collection of pre-defined useful algorithms Given a value n 1 and a list containing exactly n unique numbers called A 1, A 2,..., A n, find and print out both the largest value in the list and the position in the list where that largest value occurred. Invitation to Computer Science, 6th Edition 29

Invitation to Computer Science, 6th Edition 30

Examples of Algorithmic Problem Solving Pattern-matching: common across many applications word processor search, web search, image analysis, human genome project You will be given some text composed of n characters that will be referred to as T 1 T 2... T n. You will also be given a pattern of m characters, m n, that will be represented as P 1 P 2... P m. The algorithm must locate every occurrence of the given pattern within the text. The output of the algorithm is the location in the text where each match occurred. Invitation to Computer Science, 6th Edition 31

Examples of Algorithmic Problem Solving Example 4: Meeting Your Match (continued) Algorithm has two parts: 1. Sliding the pattern along the text, aligning it with each position in turn 2. Given a particular alignment, determine if there is a match at that location Solve parts separately and use Abstraction, focus on high level, not details Top-down design, start with big picture, gradually elaborate parts Invitation to Computer Science, 6th Edition 32

Invitation to Computer Science, 6th Edition 33

Invitation to Computer Science, 6th Edition 34

Summary Pseudocode is used for algorithm design: structured like code, but allows English and math phrasing and notation Pseudocode is made up of: sequential, conditional, and iterative operations Algorithmic problem solving involves: Step-by-step development of algorithm pieces Use of abstraction, and top-down design Invitation to Computer Science, 6th Edition 35