Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Efficiency of Algorithms
ISP 121 Algorithmsand Computer Programming. Why Know Simple Programming?  You can create powerful macros in Access / Excel / Word / ??? to manipulate.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
Algorithms and Pseudocode Bioinformatics. Formulating Problems Clarify input and output elements Requires modeling the problem in some concise form Example:
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
CS107 Introduction to Computer Science
Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
CS107 Introduction to Computer Science Lecture 2.
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
CHAPTER 2 CS
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
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
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Algorithms and Efficiency of Algorithms February 4th.
Designing Algorithms Csci 107 Lecture 4.
Searching and Sorting Arrays
Pseudocode.
Chapter 2: Algorithm Discovery and Design
Pseudocode.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
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.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
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.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
CSCI-100 Introduction to Computing
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Counting Loops.
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.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
PROGRAMMING: What’s It All About?
Selection Structures (Part 1)
Standard Algorithms Higher Computing.
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Introduction to pseudocode
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
Algorithm Discovery and Design
Discovery and Design of Algorithms
Presentation transcript:

Designing Algorithms February 2nd

Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun 12-10pm Office hours: –Mon, Wed 1-2, Tue 2:30-3:30, Thu 4-5pm –Anytime I am in the office – to set up appointment

Designing algorithms Last time –Pseudocode –Algorithm 1: adding 2 m-digit numbers –Algorithm 2: computing miles-per-gallon Today –More algorithms –Reading: chapter 2

An example pseudocode algorithm (Fig 1.2) Given m ≥ 1 and two positive numbers a and b, each containing m digits, compute the sum c = a + b. 0 Get values for m, a m-1 … a 0 and b m-1 … b 0 1 Set the value of carry to 0. 2 Set the value of i to 0. 3 Repeat steps 4-6 until i > m-1 4 Set the value of c i to a i + b i + carry 5 if c i ≥ 10 then subtract 10 from c i and set the value of carry to 1 else set the value of carry to 0 6Add 1 to i 7 Set the value of c m to carry 8 Print value of c = c m c m-1 c m-2 … c 0 9 Stop

The final MPG program (Fig 2.5) Write a pseudocode algorithm to compute the distance travelled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer. 0 Set response to “Yes” 1 Repeat steps 2-8 until response = “No” 2 Get gallons, start, end 3 Set distance to end - start 4 Set mpg to distance ÷ gallons 5 Print mpg 6 if mpg > 25.0 then print “You are getting good gas mileage” else print “You are NOT getting good gas mileage” 7 Print “Do you want to do this again, Yes or No?” 8 Get response 9Stop

Designing Algorithms: A Methodology 1.Read the problem, identifying the input and the output. 2.What variables are needed? 3.What computations are required to achieve the output? 4.Usually, the first steps in your algorithm bring input values to the variables. 5.Usually, the last steps display the output 6.So, the middle steps will do the computation. 7.If the process is to be repeated, add a loop around it.

This visual model is good for design, too… Algorithm Computer Input (keyboard) Output (screen) Variables

How was the MPG program (Fig 2.5) designed? Problem statement (p 35): Write a pseudocode algorithm to compute the distance travelled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer. Input: number of gallons, starting mileage, ending mileage Output: distance traveled, average miles per gallon Variables: gallons, start, end, distance, mpg Calculate: distance = end - start mpg = distance / gallons Put the steps in order: input, calculate, output (steps 2-8) Determine if a loop is needed (steps 0, 1, 9, 10)

Visualizing the program design response end distance Yesgallons start mpg Computer Input (keyboard) Output (screen)

Designing a Sequential Search Algorithm Problem statement (p 43-44): Write a pseudocode algorithm to find the location of a target value in a list of values. Input: a list of values and the target value Output: the location of the target value, or else a message that the value does not appear in the list. Variables: target, list, found, index Calculate: if the value at index in list = target set found to true else increment the index by 1 Put the steps in order: input, calculate, output Determine if a loop is needed

The final sequential search program (Fig 2.9) Get the value of target, n, and the list of n values Set index to 1 Set found to false Repeat until found = true or index > n If the value of list index = target then Output the index Set found to true else Increment the index by 1 If not found then Output a message that target was not found Stop

Variations of sequential search.. Modify the sequential search algorithm such that –To find all occurrences of target in the list and print the positions where they occur –To count the number of occurrences of target in the list –To count how many elements in the list are larger than target

More algorithms Write algorithms to find –the largest number in a list of numbers (and the position where it occurs) –the smallest number in a list of numbers (and the position where it occurs) –the range of a list of numbers Range= largest - smallest –the average of a list of numbers –the sum of a list of numbers

For next time.. Think about the problems Read Chapter 2 –Except (Pattern matching), which we’ll do next time