DOC PREVIEW
Princeton COS 116 - lecture 3

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Telling a computerhow to behave(via pseudocode -- a workaroundfor Computing’s Tower of Babel.)COS 116: 2/12/2008Sanjeev AroraJan 29, 2008Steps 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.Example: Adding two numbersDiscussion TimeImagine you are describing this task to somebody who has never done it. How would you describe it? Our robot is getting ready for a big date… How would it identify the cheapest bottle?(Say it can scan prices)Discussion TimeSolution 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.How can we describe an algorithm preciselyenough so there is no ambiguity?Recall: Scribbler’s “Language” Several types of simple instructions E.g. “Move forward for 1 s” Two types of compound instructionsIf <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}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.JavaC++BASICPythonComputing’s Tower of BabelSimilar question in different setting Robot has n prices stored in memory Wants to find minimum priceComputer 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 =valuenameExamplesi ← 5Sets i to value 5j ← iSets j to whatever value is in i.Leaves i unchangedi ← j + 1Sets i to j + 1.Leaves j unchangedi ← i + 1Sets i to 1 more than it was.Arrays A is an array of n values, A[ i ] is the i’thvalue Example: A[3] = 52.9940.99 62.99 52.99…22.99A =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.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].Another way to do the samebest ← 1;i ← 1Do while (i < n){i ← i + 1;if ( A[ i ] < A[best] ) then{ best ← i }}New problem for robot: sortingArrange them so prices increase from left to right.SolutionDo 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.Swapping Suppose x and y are variables.How do you swap their values? Need extra variable!tmp ← xx ← yy ← tmpAside: History of Algorithm Named for Abu Abdullah Muhammad binMusa al-Khwarizmi(780-850AD) His book "Al-Jabr wa-al-Muqabilah" evolvedinto today's high school algebra text. Notion of algorithm has existed for atleast 2000 years (in Hindu, Chinese, andGreek traditions) “Variables” in algebra come from thesame tradition.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


View Full Document

Princeton COS 116 - lecture 3

Documents in this Course
Lecture 5

Lecture 5

15 pages

lecture 7

lecture 7

22 pages

Lecture

Lecture

32 pages

Lecture

Lecture

16 pages

Midterm

Midterm

2 pages

Lecture

Lecture

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

24 pages

Lecture

Lecture

22 pages

Lecture

Lecture

28 pages

Lecture

Lecture

21 pages

Lecture

Lecture

50 pages

Lecture

Lecture

19 pages

Lecture

Lecture

28 pages

Lecture

Lecture

32 pages

Lecture

Lecture

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

19 pages

Lecture

Lecture

22 pages

Lecture

Lecture

21 pages

Logic

Logic

20 pages

Lab 7

Lab 7

9 pages

Lecture

Lecture

25 pages

Lecture 2

Lecture 2

25 pages

lecture 8

lecture 8

19 pages

Midterm

Midterm

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

29 pages

Lecture

Lecture

40 pages

Lecture 3

Lecture 3

37 pages

lecture 3

lecture 3

20 pages

Lecture

Lecture

21 pages

Lecture

Lecture

24 pages

Lecture

Lecture

19 pages

Load more
Download lecture 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lecture 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lecture 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?