emil sekerinski, mcmaster university, winter term …cs1md3/01 on programming.pdfemil sekerinski,...

34
Emil Sekerinski, McMaster University, WinterTerm 16/17 COMP SCI 1MD3 Introduction to Programming

Upload: others

Post on 28-Mar-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

EmilSekerinski,McMasterUniversity,WinterTerm16/17COMPSCI1MD3IntroductiontoProgramming

Anx-intersectoffunctionfisanxsuchthatf(x)=0.

Supposefismonotonicallyincreasingon[a,b]and:

f(a)≤0,f(b)≥0Todeterminethex-intersectwithprecisionε>0:1.  aslongasb–a>ε,do2.–4.2.  calculatem=(a+b)/23.  iff(m)≤0,setatom,or4.  iff(m)≥0,setbtomTheresultis(a,b)suchthat

f(a)≤0,f(b)≥0,b–a≤ε

Whatarethedifferencesinthedescriptions?a m b

f(m)

¡  Declarativedescriptionsstatethepropertiesoftheresult.Theydescribewhattheresultis.Theyareshort,butcannotbefollowed.§  Inthecaseofx-intersect,therecouldbeseveralpossibleresultsortheircouldbenone

¡  Imperativedescriptionstellhowtheresultisobtainedbygivenasequenceofinstructions.Eachstepissimpleenoughthatitcanbefollowed.§  Inthecaseofx-intersect,restrictionsontheinputareimposed(fmonotonic),additionalinputisneeded(a,b,ε),andtheresultisonlyapproximate(withε).Thereisrepetition(line1)andselection(lines3,4).

Supposefismonotonicallyincreasingon[a,b]and:

f(a)≤0,f(b)≥0Todeterminetheprecisex-intersect:1.  aslongasb–a>0,do2.–4.2.  calculatem=(a+b)/23.  iff(m)≤0,setatom,or4.  Iff(m)≥0,setbtomTheresultis(a,b)suchthat

a=b,f(a)=0

Supposefismonotonicallyincreasingon[a,b]and:

f(a)≤0,f(b)≥0Todeterminethex-intersectwithprecisionε>0:1.  aslongasb–a>ε,do2.–4.2.  calculatem=(a+b)/23.  iff(m)≤0,setatom,or4.  iff(m)≥0,setbtomTheresultis(a,b)suchthat

f(a)≤0,f(b)≥0,b–a≤ε

No,forsomefthe“precisealgorithm”willnotstop:ifa,barerationalandthex-intersectisirrational,itbeneverbereached.

Analgorithmspecifiesasequenceofinstructionstobeexecutedbyamachinethat,whenprovidedwithinput,willeventuallystopandproducesomeoutput.

Awashingmachinefollowsanalgorithmforthesequenceofwashinganddryingcycles.Theinstructionsofthatmachinearetakingwaterin,lettingwaterout,applyingdetergent,rotatingthedrum,andheatingthewater.Theinputistheselectedprogram,water,detergent,andthedirtylaundry.Theoutputisthecleanlaundryandwastewater.

Atrafficlightfollowsanalgorithmforswitchingthelight.Theinstructionsofatrafficlightareturningindividuallightsonandoff.

Drivingdirectionsareanalgorithmtobeexecutedbyahuman.Theinstructionsaredrivingtothenextstop,continuingstraightahead,turningleft,turningright.

Acookbookrecipeisanalgorithmtobeexecutedbyahuman.Instructionsare,puttingingredientsinapot,stirringthesoupforaminute,waitingfiveminutes,andturningheatonandoff.

¡  Thecomputationprescribedbyanalgorithmgoesthroughasequenceofstates,startingfromaninitialstate,andeachinstructionleadingtoanewstate.

¡  Thetraceistheresultingsequenceofstates.

AlgorithmcomesfromtheLatintranslation,AlgoritmidenumeroIndorum,ofthe9th-centuryMuslimmathematicianal-Khowârizmî’sarithmetictreatiseal-KhowârizmîConcerningtheHinduArtofReckoning.

Euclid’salgorithmforcomputingthegreatestcommondivisoroftwointegernumbersgoesbacktotheGreekmathematicianEuclid(circa300bc):

Assumethatpositiveintegersxandyaregiven.1. Letubexandvbey.2. Aslongasuisnotequaltov,dooneof

thefollowing:2.1. Ifu>v,thensubtractvfromu.2.2.Ifv>u,thensubtractufromv.

Finally,uisequaltovandtheirvalueisthegreatestcommondivisorofxandy.

Thestateisgivenbythevaluesofthevariablesu,v.Theinputtothealgorithmarex,yandtheoutputisthegcd(x,y).

Traceforinputx=28andy=42:

Instruction u v

WIKIPEDIA

1. 28 42

2.2. 28 14

2.1. 14 14

Algorithmsaresupposedtobeexecuted“mechanically”:¡  Algorithmsmustbeprecise:eachinstructionandthenext

possibleinstructiontobetakenmustbeunambiguous.

¡  Algorithmmustbeeffective:eachinstructionmustbeexecutablebytheunderlyingmachine.

Twofundamentalquestions:¡  Correctness:Whatistheproducedoutputandforwhich

inputsisitproduced?

¡  Efficiency:Howmanyinstructionsdoneedtobeexecutedintheworstcaseoronaverage?Howmanyresources(likememory)areneededintheprocess?

A.  Thealgorithmisinerrorbecauseitisambiguous

B.  Thealgorithmisnotinerror,buttheinputswhichleadtof(m)=0arenotallowed

C.  Eitherinstruction3or4isexecuted,itdoesn'tmatter

Supposefismonotonicallyincreasingon[a,b]and:

f(a)≤0,f(b)≥0Todeterminethex-intersectwithprecisionε>0:1.  aslongasb–a>ε,do2.–4.2.  calculatem=(a+b)/23.  iff(m)≤0,setatom,or4.  iff(m)≥0,setbtomTheresultis(a,b)suchthat

f(a)≤0,f(b)≥0,b–a≤ε

Follow-upquestion:istheoutputofthisalgorithmalwaysunique?A:yes,B:no

Algorithmsareformulatedtextuallyorasflowcharts:Flowchartsconnectinstructionsandconditions:

S

B

Instruction(statement,command,action),e.g.•  turngreenlighton•  continuestraightahead•  subtractvfromu

+ –Condition(Booleanexpression,test),e.g.•  drumisfull•  waterboils•  uisnotequaltov

Werestricttohierarchicallystructuredflowchartscomposedof:

A

Basic:

SthenTS

T

B

S T

Selection:+ –

B

S

+

– ifBthenS

ifBthenSelseT

Sequence: Repetition:

B

S

+

–whileBdoS

Start(optional):

End(optional):

Thebasicinstructionsofflowchartsarethoseofunderlyingmachinehasforchangingthestate.Ifthemachineisacomputer,thebasicinstructionistheassignmenttoavariable

x:=e xbecomeseThemultipleassignmentupdatesvariablessimultaneously:

x,y:=e,f xbecomeseandybecomesfForexample,ifinitiallyx=3andy=4,thenx,y:=x+y,x–ysetsxto7andyto–1

¡  Flowchartsarehierarchicallycomposed

¡  Thisensuresthateveryflowcharthasasingleentryandsingleexit

¡  Thisavoidsspaghetti-likeflowcharts!

u,v:=x,y

u≠v

u>v

u:=u-v v:=v-u

+

+ –

https://en.wikipedia.org/wiki/Flowchart

http://www.smartdraw.com/flowchart/flowchart-software.htm Patientleaves

https://www.gliffy.com/examples/flow-charts/

Themedianofx,y,z,isthe“middle”number.Themedianof3,7,2is3.Themedianof3,7,3isalso3.Assignthemedianofx,y,ztom!

x<y+ –

z<x+ –

z<y+ –

x<z+ –

z<y+ –

m:=x

m:=z m:=y

m:=x

m:=y m:=z

•  Annotationsexpresswhatholdsatparticularpoints

•  Theyaredeclarativedescriptions

•  Theyhelpunderstandingalgorithmsandarguingtheircorrectness

•  Itisgoodpracticetoexplicitlywritethemainannotations

•  Thereisadangerofover-annotating!

x<y+ –

z<x+ –

z<y+ –

m:=x

m:=z m:=y

x<yz<xx<y

z<mm<y

x<yx≤z

x<yy≤z

x<mm≤z

¡  Lettifor0≤i<nbeaseriesofntemperaturereadingswhereeachreadingisanintegerandn>0

¡  Setminandmaxtotheminimalandmaximaltemperature!

i,min,max:=1,t0,t0

i<n

ti<min

max:=ti

i:=i+1

+

+ –

ti>maxmin:=ti+

¡  Howmanycomparisonswithtiwillbemadeinthebestandworstcase?

¡  n–1comparisonsiftisstrictlydecreasing

¡  2x(n–1)comparisonsiftisincreasing

¡  Howmanyassignmentstominandmaxwillbemadeinthebestandworstcase!

¡  1tomin,1tomaxifalltiarethesame

¡  1+nintotaliftstrictlyincreasing

i,min,max:=1,t0,t0

i<n

ti<min

max:=ti

i:=i+1

+

+ –

ti>maxmin:=ti+

1.  Atcheck-incounter:§  Presentid§  Putluggageonscale.Iftoo

heavypayfeeorremoveitemsandputluggagebackonscale

§  Obtainboardingpass2.  Atsecuritycheck:

§  Placenecklace,belt,etc.,intray

§  Passthroughscanner§  Ifscannerbeepscheckfor

furtheraccessoriesandtryagain

3.  Atthegate:§  Presentboardingpass

•  Reformulateasanalgorithm!

•  Verbsindicateinstructions.

•  Conditionssuggestselectionandrepetition

•  Althoughthisispresentedin“structuredEnglish”,lookforsynonymsandimplicitinformation.

1.  Atcheck-incounter:§  Presentid§  Putluggageonscale.Iftoo

heavypayfeeorremoveitemsandputluggagebackonscale

§  Obtainboardingpass2.  Atsecuritycheck:

§  Placenecklace,belt,etc.,intray

§  Passthroughscanner§  Ifscannerbeepscheckfor

furtheraccessoriesandtryagain

3.  Atthegate:§  Presentboardingpass

•  Reformulateasanalgorithm!

•  Verbsindicateinstructions.

•  Conditionssuggestselectionandrepetition

•  Althoughthisispresentedin“structuredEnglish”,lookforsynonymsandimplicitinformation.

Goto

Goto

Goto

1.  Atcheck-incounter:§  Presentid§  Putluggageonscale.Iftoo

heavypayfeeorremoveitemsandputluggagebackonscale

§  Obtainboardingpass2.  Atsecuritycheck:

§  Placenecklace,belt,etc.,intray

§  Passthroughscanner§  Ifscannerbeepscheckfor

furtheraccessoriesandtryagain

3.  Atthegate:§  Presentboardingpass

Gotocheck-incounter

Presentpassport

Putluggageonscale

tooheavyandnotwillingtopay

+

Removeluggagefromscale

Removeitems

Putluggageonscale

tooheavy

+Payfee

¡  Howwouldyouargueforthecorrectnessofthisalgorithm?

¡  Youcannotgothroughthesecuritycheckwithluggage

¡  Atthegateyouhavetohavetheboardingpass

¡  Correctnessisensuredbycheckingifanannotationholdsonallpathsleadingtoit!

Obtainboardingpass

Gotosecuritycheck

Placeaccessoriesintray

Gotogate

Presentboardingpass

scannerbeeps

+

Passthroughscanner

Placeaccessoriesintray

Passthroughscanner

haveboardingpass

havenoluggage

¡  Algorithmscaneitherbetransformational,computingoutputgivensomeinput,orcanbeinteractive(oronlinealgorithms),producingoutputandneedinginputduringcomputation

¡  Schedulingameeting,runningacarrepairshop,(boardandcomputer)gamesareinteractivealgorithms

¡  Inthiscourse,wemainlyconsidertransformationalalgorithms

¡  Algorithmicnotionsarenotonlyapplicabletoprograms

¡  Theygiveusavocabularytotalkaboutawiderangeofproblems.Theyareatoolforourmind

¡  Increasingly,theyareusedtodescribetheenvironmentinwhichcomputersoperate,e.g.approvalprocessesinbanks,informationflowsinhospitals

¡  Programsarealgorithmsthatcanbeexecutedautomaticallybyacomputer

¡  Fixed-programcomputersserveonlyonepurpose(someearlycomputers,digitalclock,washingmachine)

¡  Oneofthefirstmodernstored-programcomputerswastheManchesterMark-1from1949

¡  Itachieveduniversalitybystoringinstructionsthesamewayasdata,allowingprogramstobechanged–theuniversalityof“machines”wasanticipatedbyAlanTuringin1936

WIKIPEDIA

¡  Eightbits(binarydigits)formabyteandanumberofbytes(say4)formawordthatholdseitherdata(likeaninteger)oraninstruction

¡  InavonNeumannArchitectureprogramsanddataareplacedinthesamememory(typicalforCPUs)

¡  InaHarvardArchitecturetwodistinctmemoriesareusedforinstructionsanddata(typicalforGPUs)

0

…110104328

…110011036

e.g.32bits

bus

Processor(CPU)

variable,e.g.“u”

instructione.g.“minus”

memory

arithmeticunit

register

¡  Instructionsofacomputerarethecomputer’smachinelanguage

¡  Fortran,anearlyhigh-levellanguagefrom1958wasa“formulatranslator”formathematicalexpressionsintomachinelanguage

¡  StartingwithAlgol,Pascalinthe60’sand70’s,programminglanguagesareclosertoalgorithmicnotation.

¡  Whilegraphicalprogramminglanguageshavededicateduse,textuallanguagesaredominantforlargeprograms.

Fortran“arithmeticif”:IF(X)10,20,30Jumptocard10,20,30ifX<0,X=0,X>0

Pascal:functiongcd(x,y:integer):integer;varu,v:integer;beginu:=x;v:=y;whileu<>vdoifu>vthenu:=u–velsev:=v–u;gcd:=uend

Python:defgcd(x,y):u,v=x,ywhileu!=v:ifu>v:u=u–velse:v=v–ureturnu

C,Java:intgcd(intx,inty){intu,v;u=x;v=y;while(u!=v)if(u>v)u=u–v;elsev=v–u;returnu;}

u,v:=x,y

u≠v

u>v

u:=u-v v:=v-u

+–

+ –

¡  Machineinstructionsareveryprimitive:§  Movedatabetweenmemoryandprocessorregisters§  Performanarithmeticoperationonregisters§  Compareregisters§  Jumptoanothermemorylocation

¡  Compilerstranslatehigh-levelprogramstomachinelanguageu:=u–v R1:=u moveutoregister1

R2:=v movevtoregister2 R1:=R1–R2 subtractregister2from1 u:=R1 moveregister1tou

¡  Notation:§  <>or!=for≠§  begin…endor{…}orindentationforbracketing

¡  Safety:§  typechecking(Java:static,Python:dynamic,C:little)

¡  Supportforlargeprograms:§  classes(Java,Python:yes,C:no)§  exceptions(Java,Python:yes,C:no)

¡  Instructionsandstandardlibraries:§  datatypeslikelists,sets,dictionaries(Python:yes)§  interaction,graphics(mixed)

¡  Application:§  dedicated(JavaScript,Flash:webpages)§  general-purpose(Java,Python,Pascal,C:yes)

¡  ThePythonnotationiscompactandintuitive,makingitagoodbeginner’slanguage

¡  Pythoncompilestoanintermediatelanguageandinterpretsthat,whyPythonissometimesreferredtoasaninterpretedlanguage;thismakesinteractionswiththePythonenvironmentquick,allowingeasyexploration

¡  Pythonsupportsmanymathematicaldatatypes,allowingprogramstobeabstractandeasytounderstand

¡  Pythoniswidelyusedinindustry

ThelanguageandtheIDLEenvironmentarenamedaftertheBritishcomediansMontyPython