23
Telling a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora

Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

  • Upload
    ngodien

  • View
    222

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Telling a computerhow to behave(via pseudocode -- a workaroundfor Computing’s Tower of Babel.)

COS 116: 2/12/2008Sanjeev Arora

Page 2: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Jan 29, 2008

Page 3: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,
Page 4: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Steps in solving a computational task

Design an algorithm: A precise,unambiguousdescription for how to compute a solution.

Express algorithm in pseudocode.

Turn pseudocode into computer program.

Page 5: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Example: Adding two numbers

Discussion Time

Imagine you are describing this task to somebody who has never done it. How would you describe it?

Page 6: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Our robot is getting ready for a big date…

How would it identify the cheapest bottle?(Say it can scan prices)

Discussion Time

Page 7: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Solution

Pick up first bottle, check price

Walk down aisle. For each bottle, do this: If price on bottle is less than price in hand,

exchange for one in hand.

Page 8: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

How can we describe an algorithm preciselyenough so there is no ambiguity?

Page 9: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Recall: Scribbler’s “Language”

Several types of simple instructions E.g. “Move forward for 1 s”

Two types of compound instructions

If <condition> Then{

List of instructions}Else{

List of instructions}

Do 5 times{

List of instructions}

Conditional (a.k.a. Branching)Loop (2 types)

Do while (condition){List of instructions}

Page 10: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Scribbler language illustrates essentialfeatures of all computer languages

Fundamental features of human languages:nouns/verbs/adjectives,subjects/objects, pronouns, etc.

Computer languages also share fundamental features, e.g. conditionaland loop statements, variables, ability to perform arithmetic, etc.

Java

C++BASIC

Python

Computing’s Tower of Babel

Page 11: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Similar question in different setting

Robot has n prices stored in memory

Wants to find minimum price

Page 12: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Computer Memory: simplified view

A scratchpad that can be perfectly erasedand re-written any number of times

A variable: a piece of memory with aname; stores a “value”

22.99i =

valuename

Page 13: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Examples

i ← 5 Sets i to value 5

j ← i Sets j to whatever value is in i.Leaves i unchanged

i ← j + 1 Sets i to j + 1.Leaves j unchanged

i ← i + 1 Sets i to 1 more than it was.

Page 14: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Arrays

A is an array of n values, A[ i ] is the i’thvalue

Example: A[3] = 52.99

40.99 62.99 52.99 … 22.99A =

Page 15: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Recall Solution

Pick up first bottle, check price

Walk down aisle. For each bottle, do this: If price on bottle is less than price in hand,

exchange for one in hand.

Page 16: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Procedure findmin(in pseudocode) n items, stored in array A Variables are i, best best ← 1 Do for i = 2 to n

{if ( A[ i ] < A[best] ) then { best ← i }

}Output A[best].

Page 17: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Another way to do the same

best ← 1;i ← 1Do while (i < n){

i ← i + 1;if ( A[ i ] < A[best] ) then

{ best ← i }}

Page 18: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,
Page 19: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

New problem for robot: sorting

Arrange them so prices increase from left to right.

Page 20: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Solution

Do for i=1 to n-1{ Find cheapest bottle among those numbered i to n

Swap that bottle and the i ’th bottle.}

“selection sort”

Task for Thurs: Write pseudocode for selection sort; due at the start of lecture.

Page 21: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Swapping

Suppose x and y are variables.How do you swap their values?

Need extra variable!

tmp ← xx ← yy ← tmp

Page 22: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Aside: History of Algorithm

Named for Abu Abdullah Muhammad binMusa al-Khwarizmi(780-850AD) His book "Al-Jabr wa-al-Muqabilah" evolved

into today's high school algebra text. Notion of algorithm has existed for at

least 2000 years (in Hindu, Chinese, andGreek traditions)

“Variables” in algebra come from thesame tradition.

Page 23: Telling a computer how to behave - Princeton … a computer how to behave (via pseudocode -- a workaround for Computing’s Tower of Babel.) COS 116: 2/12/2008 Sanjeev Arora Jan 29,

Fact: This simple pseudocode is all we need to expressallall possible computations (topic of a future lecture).

“Findmin? Sorting?? Pseudocode???How about something more important?”

Coming upon Thurs:

Extreme Weather