CS107: Introduction to Computer Science Lecture 2 Jan 29th.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
CSci 107 Introduction to Computer Science Lecture 1.
Csci 107 Introduction to Computer Science. Administrativia See class webpage for –Office hours –Grading policy –Syllabus –Lab assignments –Readings.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
ITEC113 Algorithms and Programming Techniques
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
Chapter 2: Algorithm Discovery and Design
CSci 107 Introduction to Computer Science Lecture 1.
Chapter 2: Algorithm Discovery and Design
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
Program Design and Development
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
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
Computer Science 1620 Programming & Problem Solving.
Designing Algorithms Csci 107 Lecture 4.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Pseudocode.
Review Algorithm Analysis Problem Solving Space Complexity
Adapted from slides by Marie desJardins
计算机科学概述 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 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.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
ITEC113 Algorithms and Programming Techniques
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
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.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
Introduction to Algorithms
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Algorithm and Ambiguity
Lecture 2: Introduction to Algorithms
COMS W1004 Introduction to Computer Science and Programming in Java
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
Introduction to Algorithms and Programming
Repetition Control Structure
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Introduction to Algorithms - 1
The structure of programming
Discovery and Design of Algorithms
Presentation transcript:

CS107: Introduction to Computer Science Lecture 2 Jan 29th

Last time –What is not Computer Science –What is Computer Science –Algorithms, properties and examples Today –Expressing algorithms: Pseudocode –Examples Algorithm for adding two m-digit numbers Algorithm for computing miles-per-gallon

What is Computer Science? The study of algorithms: –their formal properties correctness, limits efficiency/cost (Chapters 2, 3, 10) –their hardware realizations computer design (Chapters 4-6) –their linguistic realizations programming languages (Chapters 7-9) –their applications network design, ocean modeling, bioinformatics,...

What is an algorithm? Algorithm = well-defined procedure that allows an agent to solve a problem. Algorithms must: 1. Be well-ordered and unambiguous 2. Be executable (understandable), 3. Solve the problem, and 4. Terminate. Note: often the agent is a computer or a robot…

Example Algorithms Cooking a dish Making a peanut-butter jelly sandwich Shampooing hair Adding two numbers (p 7) Programming a VCR (p 6) Making a pie (p 13)

Designing Algorithms We need: A problem for which an effective algorithm can be written (i.e., one that is tractable), A language (e.g., “pseudocode” or C++), A methodology for translating the problem into an algorithm, and A computing “agent” that can understand and execute such algorithms.

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 (primitive) 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 Sequential operations Repeat operations –Execute a block of operation multiple times until a certain condition is met

Variables Variable –A named memory location that can store a value –Think of it as a box into which you can store a value, and from which you can retrieve a value Examples: –List variables: used when the input comes as a list of elements i carry a m-1 a m-2 … a 1 a 0 … a 1 a 2 … a m-1 a m …

Expression and assignment statements Function: change the value of a variable by evaluating arithmetic expressions can use any arithmetic expression, just like on a typical calculator: *, /, … Examples set the value of m to 2 set the value of i to 6. set the value of carry to i+2*m set the value of a i to 0 a 1 a 2 … a m-1 a m …

Input/Output Statements Function: transfer values from the user to a variable, or vice- versa get values for print value of Examples –Get the value of n from the user –Get value of the list of elements a 1, a 2, …,a m from the user –Print the value of result to the user

Sequential statements The steps of an algorithm are carried out in the order they are written. Conditional statements Function: specifying 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 Function: 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 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

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

Another example: computing MPG (Fig 2.5) 0 Set response to “Yes” 1Repeat steps 2-10 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 7 print “You are getting good gas mileage” 8 else print “You are NOT getting good gas mileage” 9 Print “Do you want to do this again, Yes or No?” 10 Get response 11Stop

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)

Summary Pseudocode –Get/print statements (input/output statements) –Conditional statements –Sequential statements –Loops Algoritms –Compute sum of two m-digit numbers –Compute miles-per-gallon

Over the weekend… 1. Get the textbook. 2. Read Chapter 1, 2.1 and Discuss and solve the problems in the Lab 1 (in groups or individually). Lab 1 is due on Monday in class. If you have questions we’ll discuss them in class on Monday. 4.Bring a digital picture for the lab tomorrow. You will need it for your website.