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

CS107: Introduction to Computer Science Lecture 2 Jan 29th

Embed Size (px)

Citation preview

Page 1: CS107: Introduction to Computer Science Lecture 2 Jan 29th

CS107: Introduction to Computer

ScienceLecture 2

Jan 29th

Page 2: 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

Page 3: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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

Page 4: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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…

Page 5: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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)

Page 6: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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.

Page 7: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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.

Page 8: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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

Page 9: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Pseudocode elementsBasic (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

Page 10: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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

am-1 am-2 … a1 a0

a1 a2 … a m-1 am

Page 11: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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: +. -. *, /, …

Examplesset the value of m to 2

set the value of i to 6.set the value of carry to i+2*mset the value of ai to 0

a1 a2 … a m-1 am

Page 12: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Input/Output Statements

Function: transfer values from the user to a variable, or vice-versa

get values for <variable>print value of <variable>

• Examples

– Get the value of n from the user– Get value of the list of elements a1, a2, …,am

from the user– Print the value of result to the user

Page 13: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Sequential statements

The steps of an algorithm are carried out in the order they are written.

Conditional statementsFunction: specifying a statement that may or may not be done:

if <condition> then<statement to be done>

else <statement to be done otherwise>

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

Page 14: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Loop statements

Function: specify a group of statements that may be done several times (repeated):

repeat until <condition>< statements to be repeated >

• 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 <statements to be

repeated> in order, one by one

Page 15: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Example

Step 1: set count to 1Step 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 countStep 5: add 1 to count

Step 6: end

• What does this algorithm do? • Note: indentation

– Not necessary, but makes reading/understanding algorithms easier

Page 16: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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, am-1 … a0 and bm-1 … b0

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 ci to ai + bi + carry

5 if ci ≥ 10 then

subtract 10 from ci and set the value of carry to 1

else set the value of carry to 06 Add 1 to i

7 Set the value of cm to carry

8 Print value of c = cm cm-1 cm-2 … c0

Page 17: CS107: Introduction to Computer Science Lecture 2 Jan 29th

So, how does this work???

For example, the input is m = 4, a = 3276, and b = 7345.

After step 0, the variables m, a, and b have those values:

m

a3 a2 a1 a0 b3 b2 b1 b0

4 3 2 7 6 7 3 4 5

After steps 1 and 2, the variables i and carry are initialized.

i 0 carry 0

Next, steps 4-6 are repeated until the value of i > 3. Each repetition computes a single digit of c.

c4 c3 c2 c1 c0

Page 18: CS107: Introduction to Computer Science Lecture 2 Jan 29th

A model for visualizing an algorithm’s behavior

Algorithm

Computer

Input (keyboard)

Output (screen)

Variables

Page 19: CS107: Introduction to Computer Science Lecture 2 Jan 29th

E.g., Visualizing Fig 1.2

m

a3 a2 a1 a0

b3 b2 b1 b0

4

3 2 7 6

7 3 4 5

i 0 carry 0

c4 c3 c2 c1 c0

0 Get values for ……8 Print value of …

Computer

Input (keyboard)

Output (screen)

432767345

Page 20: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Another example: computing MPG (Fig 2.5)

0 Set response to “Yes”1 Repeat steps 2-10 until response = “No”

2 Get gallons, start, end3 Set distance to end - start4 Set mpg to distance ÷ gallons5 Print mpg6 if mpg > 25.0 then7 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

11 Stop

Page 21: CS107: Introduction to Computer Science Lecture 2 Jan 29th

So, how does this work???

For example, suppose we use 25 gallons, beginning at 12000 and ending at 13000 on the odometer. Then, after step 2, some variables have the following values:

response gallons12000start

Yes 25

After step 4, the variables distance and mpg are computed.

mpg 40

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

13000

1000

Page 22: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Visualizing Fig 2.5

response

end

distance

Yes gallons

start

mpg

0 Set response ……11 Stop

Computer

Input (keyboard)

Output (screen)

251200013000

Page 23: CS107: Introduction to Computer Science Lecture 2 Jan 29th

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

Page 24: CS107: Introduction to Computer Science Lecture 2 Jan 29th

Over the weekend…

1. Get the textbook.

2. Read Chapter 1, 2.1 and 2.2

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