CS107 Introduction to Computer Science Lecture 2.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
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.
CSci 107 Introduction to Computer Science Lecture 1.
Efficiency of Algorithms
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
CS107 Introduction to Computer Science Lecture 1 Introduction.
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
Chapter 2: Algorithm Discovery and Design
CSci 107 Introduction to Computer Science Lecture 1.
Chapter 2: Algorithm Discovery and Design
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
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
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CHAPTER 2 CS
CS101- Lecture 11 CS101 Fall 2004 Course Introduction Professor Douglas Moody –Monday – 12:00-1:40 – – –Web Site: websupport1.citytech.cuny.edu.
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
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Designing Algorithms Csci 107 Lecture 4.
Structured Program Development in C
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Adapted from slides by Marie desJardins
Muhammad Adnan Talib Lec#4 27 th Sep,2012 Computer Science Department COMSATS Institute of Information Technology Sahiwal Introduction to Computer Programming.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
Flowcharts.
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.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Basic Control Structures
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
CSCI-100 Introduction to Computing
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Chapter 7 Problem Solving with 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.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Introduction to Algorithms
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Selection Structures (Part 1)
Algorithm and Ambiguity
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
CSci 107 Introduction to Computer Science
Algorithm Discovery and Design
3 Control Statements:.
Algorithm Discovery and Design
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Discovery and Design of Algorithms
Presentation transcript:

CS107 Introduction to Computer Science Lecture 2

Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun 12-10pm Office hours –M, W 4-5, Tue after lab –Quick questions: Anytime I am in the office – to set up appointment My office: Searles 219 Study group –Mondays 8-10pm in Searles 224

Last time –What is (not) Computer Science –Algorithms, properties and examples –Expressing algorithms: Pseudocode Who was Alan Turing and what is he famous for? Today –More pseudocode elements –Algorithm examples

Expressing algorithms Is natural language good? –For daily life, yes…but for CS is lacks structure and would be hard to follow –Too rich, ambiguous, depends on context How about a programming language? –Good, but not when we try to solve a problem..we want to think at an abstract level –It shifts the emphasis from how to solve the problem to tedious details of syntax and grammar.

Pseudocode Pseudocode = English but looks like programming Good compromise –Simple, readable, no rules, don’t worry about punctuation. Lets you think at an abstract level about the problem. –Contains only instructions that have a well-defined structure and resemble programming languages

Pseudocode elements Basic operations –Read the input from user –Print the output to the user –Cary out basic arithmetical computations Conditional operations –Execute an operation if a condition is true Repeat operations –Execute a block of operation multiple times until a certain condition is met

Basic operations –Read the input from user Get x Get a, b, c –Print the output to the user Print x Print “Your mileage is ” x –Cary out basic arithmetical computations Set x to 10 Set y to x*x/3

Conditional statements Specify a statement that may or may not be done: if then else Example if the value of carry is 0 then set the value of a to 0 else set the vale of a to a+1

Loop statements specify a group of statements that may be done several times (repeated): repeat until How does this work? –Condition is evaluated –If it is true than the loop terminates and the next instruction to be executed will be the instruction immediately following the loop –If it is false, then the algorithm executes the in order, one by one

Example Variables: count Step 1: set count to 1 Step 2: repeat step 3 to step 5 until count is > 10 Step 3: set square to count *count Step 4: print value of square and value of count Step 5: add 1 to count Step 6: end What does this algorithm do? Note: indentation –Not necessary, but makes reading/understanding algorithms easier

Pseudocode examples Equivalent: –Set the value of a to 1 –Set a to 1 –a=1 Equivalent –Add 1 to count –Set count to count + 1 –Increment the value of count by 1 –count = count + 1 Writing in pseudocode gives you the freedom to choose any of these!

Incorrect –Set 1 to a –Add a + b (what do you do with the result?) –Set a+b +3 Note: not the same –set a to b –set b to a Example: what is the output of the following algorithms?set a to 2set b to 4 set a to bset b to aprint a, b

Problem Adding two n-digit numbers How would you write an algorithm to solve this problem? Assume the basic operation is adding one-digit numbers.

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

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

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

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)

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

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

Over the weekend… Continue reading chapter 2.2 and start on 2.3 Discuss and solve the problems in the Lab 1 –Lab 1 is due on Tuesday in class/lab. –If you have (brief) questions we’ll discuss them in class on Monday –Study group is on Monday evening.