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

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

A Quick Review of Chapter 1

Algorithm Discovery and Design

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

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

Chapter 1: An Introduction to Computer Science

Invitation to Computer Science 7th Edition TEST BANK Schneider Gersting

In this chapter, you will learn about: The definition of computer science. Algorithms. Invitation to Computer Science, C++ Version, Third Edition

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

SNS COLLEGE OF ENGINEERING,

Chapter 8 Algorithms 1

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

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Chapter 1: An Introduction to Computer Science. Invitation to Computer Science, C++ Version, 6-th Edition

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

CSEN 102 Introduction to Computer Science

Algorithms (Introduction)

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

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

CS187 - Science Gateway Seminar for CS and Math

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

Lecture 1: Introduction

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

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

Method & Tools for Program Analysis & Design

Programming II (CS300)

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

Repetition Structures

Steps to program development

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

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

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

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Homework 1 Due Tuesday, January 30, 2018 at 8pm

CS 103 Unit 8b Slides

REPETITION CONTROL STRUCTURE LOGO

COMP250: Introduction to algorithms The set-intersection problem

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

[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";

ECE 122. Engineering Problem Solving with Java

Algorithms and Flowcharts

CSE 12 Abstract Syntax Trees

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

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

Syntax and Grammars 1 / 21

Flow Chart & Algorithms

Chapter 5: Control Structures

Chapter 1 Program Design

CS 103 Lecture 4 Slides

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

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

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

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

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

Chapter 1 INTRODUCTION TO COMPUTER AND PROGRAMMING

(Refer Slide Time: 1:27)

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

PLD Semester Exam Study Guide Dec. 2018

Chapter 8. Statement-Level Control Structures

INTRODUCTION TO ALGORITHMS

Outline and Reading. Analysis of Algorithms 1

BIL101E: Introduction to Computers and Information systems Lecture 8

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

A Small Interpreted Language

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

Engineering program development. Edited by Péter Vass

Software Design Fundamentals. CSCE Lecture 11-09/27/2016

Conditionals and Recursion. Python Part 4

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

Computer Programming-1 CSC 111. Chapter 1 : Introduction

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Higher Computing Science Software Design and Development - Programming Summary Notes

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

COMP 202 Java in one week

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

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

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

1. Introduction to Programming

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

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

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103. Chapter 2. Sets

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

(Refer Slide Time: 00:50)

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

Data Structures and Algorithms

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

Computer Science 1000: Part #2. Algorithms

CSC172 Lab: State Space Search

Chapter 8 Statement-Level Control Structures

Chapter 4 Introduction to Control Statements

The Size of the Cantor Set

Chapter 5: Control Structures II (Repetition)

Computing and compilers

Requirements. Chapter Learning objectives of this chapter. 2.2 Definition and syntax

Chapter 7. Iteration. 7.1 Multiple assignment

Transcription:

2005/Sep/19 1

Homework #2 Chapter 1: Exercises 7, 9 with modifications: for Exercise 7.a: 20 and 32 are changed as your ID number s last two digits and 60. for Exercise 9: 47x25 are change as 47x(your ID number s last two digits. Chapter 2: If (your ID number s last two digits % 6) = 1: 1, 7, 13 If (your ID number s last two digits % 6) = 2: 2, 8, 14 If (your ID number s last two digits % 6) = 0: 6, 12, 18 2

The Definition of Computer Science Algorithm Dictionary definition (continued) Procedure for solving a mathematical problem in a finite number of steps that frequently involves repetition of an operation A step-by-step method for accomplishing a task Informal description An ordered sequence of instructions that is guaranteed to solve a specific problem 3

The Definition of Computer Science (continued) An algorithm is a list that looks like STEP 1: Do something STEP 2: Do something STEP 3: Do something...... STEP N: Stop, you are finished 4

The Definition of Computer Science (continued) Categories of operations used to construct algorithms Sequential operations Carries out a single well-defined task; when that task is finished, the algorithm moves on to the next operation Conditional operations Ask a question and then select the next operation to be executed on the basis of the answer to that question Iterative operations Tell us to go back and repeat the execution of a previous block of instructions 5

The Definition of Computer Science (continued) Algorithm for Adding Two m-digit Numbers Given : m 1and two positive numbers each containing m digits, a and b Wanted : c Setp 4 Setp 5 m 1 b m 2 m b c m 1 0 c m 2 c, where c steps 4 through 6. Add the two digits ai and bi If c 10, then reset c to Setp 8 Print out the final answer, c Setp 9 Stop. 0 m 2 c 10 otherwise, set the new value of carry to 0. Setp 6 Add1 to i, effectively moving one column to the left. Setp 7 Set c to the value of carry. c m 1 a a a b b b Algorithm : Step1 Set the value of carry to 0. Setp 2 Set the value of i to 0. Setp 3 While the value of i is less than or equal to m 1, repeat the instructions in m i m i m c c i m 1 to the current value of carry to get ci. and reset the value of carry to1; c c m 2 0 c 0. m 1 m 2 0 m 1 a m 2 m 1 a m 2 0 0 6

The Definition of Computer Science (continued) If we can specify an algorithm to solve a problem, we can automate its solution Computing agent: The machine, robot, person, or thing carrying out the steps of the algorithm Does not need to understand the concepts or ideas underlying the solution 7

The Formal Definition of an Algorithm Algorithm A well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time Unambiguous operation An operation that can be understood and carried out directly by the computing agent without needing to be further simplified or explained 8

The Formal Definition of an Algorithm (continued) A primitive operation (or a primitive) of the computing agent Operation that is unambiguous for computing agent Primitive operations of different individuals (or machines) vary An algorithm must be composed entirely of primitives Effectively computable Computational process exists that allows computing agent to complete that operation successfully 9

The Formal Definition of an Algorithm (continued) The result of the algorithm must be produced after the execution of a finite number of operations Infinite loop The algorithm has no provisions to terminate A common error in the designing of algorithms 10

The Importance of Algorithmic Problem Solving Algorithmic solutions can be: Encoded into some appropriate language Given to a computing agent to execute The computing agent Would mechanically follow these instructions and successfully complete the task specified Would not have to understand Creative processes that went into discovery of solution Principles and concepts that underlie the problem 11

Summary Computer science is the study of algorithms An algorithm is a well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time If we can specify an algorithm to solve a problem, then we can automate its solution Computers developed from mechanical calculating devices to modern electronic marvels of miniaturization 12

Chapter 2: Algorithm Discovery and Design Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem solving 13

Introduction This chapter discusses algorithms and algorithmic problem solving using three problems: Searching lists Finding maxima and minima Matching patterns 14

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 15

Representing Algorithms Figure 2.1 The Addition Algorithm of Figure 1.2 Expressed in Natural Language 16

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 17

Representing Algorithms Figure 2.2 The Beginning of the Addition Algorithm of Figure 1.2 Expressed in a High- Level Programming Language 18

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 19

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 20

Sequential Operations Types of algorithmic operations Sequential Conditional Iterative 21

Sequential Operations (continued) Computation operations Example Set the value of variable to arithmetic expression Variable Named storage location that can hold a data value 22

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 23

Sequential Operations (continued) Figure 2.3 Algorithm for Computing Average Miles per Gallon 24

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 25

Conditional and Iterative Operations Conditional operations (continued) 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 26

Conditional and Iterative Operations Iterative operations (continued) Perform looping behavior; repeating actions until a continuation condition becomes false Loop The repetition of a block of instructions 27

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

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

Conditional and Iterative Operations (continued) Components of a loop Continuation condition Loop body Infinite loop The continuation condition never becomes false An error 30

Figure 2.5 Third Version of the Average Miles per Gallon Algorithm 31

Conditional and Iterative Operations Pretest loop (continued) 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 32

Conditional and Iterative Operations Posttest loop (continued) Continuation condition tested at the end of loop body Loop body must be executed at least once Do/While loop 33

Figure 2.6 Summary of Pseudocode Language Instructions 34

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 35

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 36

Example 1: Looking, Looking, Algorithm discovery Looking (continued) 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 37

Example 1: 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 (like a name not found in the collection) Uses the variable Found to exit the iteration as soon as a match is found 38

Figure 2.9 The Sequential Search Algorithm 39

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 40

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 41

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 An important tool in algorithm design and development 42

Example 2: Big, Bigger, Biggest Find Largest algorithm (continued) Uses iteration and indices like previous example Updates location and largest so far when needed in the loop 43

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

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 45

Example 3: Meeting Your Match Abstraction (continued) 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 46

Example 3: Meeting Your Match Top-down design (continued) 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 Repeat until all operations are primitives 47

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 48

Figure 2.12 Final Draft of the Pattern-Matching Algorithm 49

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 50

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 51