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.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

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.
Chapter 04 (Part III) Control Statements: Part I.
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.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
CS107 Introduction to Computer Science
Efficiency of Algorithms
Introduction to Computers and Programming Lecture 7:
Chapter 2: Algorithm Discovery and Design
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming Lecture 8: More Loops New York University.
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.
Chapter 2 The Algorithmic Foundations of Computer Science
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.
Algorithms and Efficiency of Algorithms February 4th.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Designing Algorithms Csci 107 Lecture 4.
Chapter 2: Algorithm Discovery and Design
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
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.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Computer Programming Control Structure
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Introduction to Computers and Programming Lecture 7:
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of 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.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Selection Structures (Part 1)
Standard Algorithms Higher Computing.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Introduction to pseudocode
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Algorithm Discovery and Design
EPSII 59:006 Spring 2004.
Presentation transcript:

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 –Variations of the sum algorithm –Computing MPG –List variables –Adding two m-digit numbers –[Search and variations]

Computing the sum Problem: Write an algorithm which reads a positive integer from the user and computes the sum of all positive integers smaller than or equal to te number entered by the user. –Example: if the user enters 10, the algorithm should compute …+10 Algorithm: Variables: n(the number entered by the user), sum, i Print “Please enter a positive integer” Get n Sum=0, i=0 Repeat until i>n –Sum = sum + i –i = i+1 Print “The sum is” sum

An algorithm is not unique!!! There are many ways to solve a problem Moreover, given a certain way to solve a problem, there are many ways to express that into pseudocode!! Etiquette: –Give variables meaningful names –Write explanations/comments of what your code does

Another algorithm for the sum problem Use while instead of repeat loop Algorithm 2: Variables: n (the number entered by the user), sum, i Print “Please enter a positive integer” Get n Sum=0, i=0 While i <= n –Sum = sum + i –i = i+1 Print “The sum is” sum

Variations.. Given a number n from the user, write an algorithm.. To compute the sum of all even numbers <= n To compute the sum of all odd numbers <= n To compute the product of all numbers <= n (starting at 1) To compute the sum of all numbers strictly smaller than n

A model for visualizing an algorithm’s behavior Algorithm Computer Input (keyboard) Output (screen) Variables

Algorithm for computing MPG (Fig 2.5) Write a pseudocode algorithm to compute the distance traveled 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. Repeat this process until user wants to stop. Variables: response, gallons, start, end, distance, mpg 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

So, how does this work??? For example, suppose we use 25 gallons, beginning at and ending at on the odometer. Then, after step 2, some variables have the following values: responsegallons start Yes25 After step 4, the variables distance and mpg are computed. mpg40 Steps 5-9 displays these results on the output screen: 40 You are getting good gas mileage Do you want to do this again, Yes or No? end distance

Visualizing Fig 2.5 response end distance Yesgallons start mpg 0 Set response … … 11 Stop Computer Input (keyboard) Output (screen)

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 loops.

How was the MPG program (Fig 2.5) designed? Problem statement: Write a pseudocode algorithm to compute the distance traveled 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)

List variables How to represent (in pseudocode) inputs of arbitrary size? Suppose that we need to read 100 numbers from the user, or 1000, or.. – we could give each variable a different name…tedious!! Use a list variable: –Variable: list a of size n –This means that a is a list of n elements: a 1, a 2, a 3,…,a n –To read the list from the user use Get n and a 1, a 2, a 3,…,a n –To print the list use Print a 1, a 2, a 3,…,a n –We can treat each element in the list as a variable Set a 3 to 5 Set a 4 to a 3 +2

Problem Adding two m-digit numbers Write an algorithm to solve this problem. Assume the basic operation is adding one-digit numbers.

Algorithm for adding two m-digit numbers (Fig 1.2) Given: m ≥ 1 and two positive numbers a and b, each containing m digits, compute the sum c = a + b. Variables: m, list a 0..a m-1, list b 0 …. b m-1, list c 0 …c m-1 c m, carry, i 0Get values for m, a m-1 … a 0 and b m-1 … b 0 1Set the value of carry to 0. 2Set the value of i to 0. 3Repeat 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 7Set the value of c m to carry 8Print value of c = c m c m-1 c m-2 … c 0

So, how does this work??? For example, the input is m = 4, a = 3276, and b = After step 0, the variables m, a, and b have those values: m a 3 a 2 a 1 a 0 b 3 b 2 b 1 b After steps 1 and 2, the variables i and carry are initialized. i0carry0 Next, steps 4-6 are repeated until the value of i > 3. Each repetition computes a single digit of c. c 4 c 3 c 2 c 1 c 0

E.g., Visualizing Fig 1.2 m a 3 a 2 a 1 a 0 b 3 b 2 b 1 b i0carry0 c 4 c 3 c 2 c 1 c 0 0 Get values for … … 8 Print value of … Computer Input (keyboard) Output (screen)

A Search Algorithm Problem statement: 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:

The (sequential) search algorithm (Fig 2.9) Variables: target, n, list list of n values 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 in order –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