Docsity

Understanding Algorithms and Problem Solving: Part 2 - Algorithms, Study notes of Computer Science

Toggle sidebar
bg1
INTRODUCTIONTOPROBLEM SOLVING PART2NOTE.
ALGORITHMS
Incomputerprogrammingterm,analgorithmisusefulforyoutosolvethe
problem inthemostefficientway;Computerprogrammingcontainsan
enormouspart,ofgeneratingunambiguous,sequence-wiseproceduresfor
thecomputertofollowinordertoproduceparticularresults.Algorithms
areconsideredtobethefoundationofcomputerprogramming.
Therearemanyalgorithmsavailabletoday,eachwithitsownadvantages
anddisadvantages.Eachalgorithmhasitsownbestcase,worstcase,and
averagerunningtime.Youralgorithmdetermineshowefficientcanyour
systemsolvetheproblem.
Algorithmcanbedefineasastepbystepproceduretosolveagiven
problem.YouCanappliedtheconceptofalgorithminourdailyactivities,
notjustincomputeralone.Arecipeinacookbookisanexcellentexample
ofanalgorithm.Therecipeincludestherequirementsforthecookingor
ingredientsandthemethodofcookingthemuntilyouendupwithanice
cookeddish.
Inthesameway,algorithmsexecutedbyacomputercancombinemillions
of elementary steps,such as additions and subtractions,into a
complicatedmathematicalcalculation.Alsobymeansofalgorithms,a
computercan controla manufacturing process orco-ordinate the
reservationsofanairlineastheyarereceivedfrom theticketofficesall
overthecountry.Algorithmsforsuchlarge-scaleprocessesare,ofcourse,
verycomplex,buttheyarebuiltupfrom pieces.
Oneoftheobstaclestoovercomeinusing acomputertosolveyour
problemsisthatoftranslatingtheideaofthealgorithm tocomputercode
(program).Peoplecannotnormallyunderstandtheactualmachinecode
thatthecomputerneedstorunaprogram,soprogramsarewrittenina
programminglanguagesuchasCorPascal,whichisthenconvertedinto
machinecodeforthecomputertorun.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Algorithms and Problem Solving: Part 2 - Algorithms and more Study notes Computer Science in PDF only on Docsity!

INTRODUCTIONTOPROBLEM SOLVING PART 2 NOTE.

ALGORITHMS

Incomputerprogrammingterm,analgorithm isusefulforyoutosolvethe problem inthemostefficientway;Computerprogrammingcontainsan enormouspart,ofgeneratingunambiguous,sequence-wiseproceduresfor thecomputertofollowinordertoproduceparticularresults.Algorithms areconsideredtobethefoundationofcomputerprogramming. Therearemanyalgorithmsavailabletoday,eachwithitsownadvantages anddisadvantages.Eachalgorithm hasitsownbestcase,worstcase,and averagerunningtime.Youralgorithm determineshowefficientcanyour system solvetheproblem. Algorithm canbedefineasastepbystepproceduretosolveagiven problem.YouCanappliedtheconceptofalgorithm inourdailyactivities, notjustincomputeralone.Arecipeinacookbookisanexcellentexample ofanalgorithm.Therecipeincludestherequirementsforthecookingor ingredientsandthemethodofcookingthem untilyouendupwithanice cookeddish. Inthesameway,algorithmsexecutedbyacomputercancombinemillions of elementary steps,such as additions and subtractions,into a complicatedmathematicalcalculation.Also bymeansofalgorithms,a computercan controla manufacturing process orco-ordinate the reservationsofanairlineastheyarereceivedfrom theticketofficesall overthecountry.Algorithmsforsuchlarge-scaleprocessesare,ofcourse, verycomplex,buttheyarebuiltupfrom pieces. Oneoftheobstaclesto overcomeinusingacomputerto solveyour problemsisthatoftranslatingtheideaofthealgorithm tocomputercode (program).Peoplecannotnormallyunderstandtheactualmachinecode thatthecomputerneedstorunaprogram,soprogramsarewrittenina programminglanguagesuchasCorPascal,whichisthenconvertedinto machinecodeforthecomputertorun.

In the problem-solving phase ofcomputerprogramming,you willbe designingalgorithms.Thismeansthatyouwillhavetobeconsciousofthe strategies you use to solve problems in order to apply them to programmingproblems.Thesealgorithmscanbedesignedthroughthe useofflowchartsorpseudocode. Whatistheroleofalgorithm inproblem solving? Thestepstakentosolveaproblem iscalledasalgorithm.Incomputer programmingterm,analgorithm isusefulforyoutosolvetheproblem in themostefficientway.Averysimpleexampleofalgorithm is sortingalgorithm. Analgorithm mustsatisfythefollowingproperties: Input:Thealgorithm musthaveinputvaluesfrom aspecifiedset. Output:Thealgorithm mustproducetheoutputvaluesfrom aspecifiedset of inputvalues.... Effectiveness:Itmustbepossibletoperform eachstepof thealgorithm correctlyandinafiniteamountoftime. Definiteness:Allstepsofthealgorithm mustbepreciselydefined. Finiteness:Foranyinput,algorithm mustterminateafteralimitednumber ofsteps. MainStagesinvolvedincreatinganAlgorithm. Themainstagesinvolvedincreatinganalgorithm: DataInput DataProcessing ResultOutput.

N:BProgrammerandUserscanbedifferentinsomecases. Example( 1 )WriteanAlgorithm tologintoyourFacebookaccount. Solution: Step 1 :Start Step 2 :ConnecttoInternet Step 3 :Gotowww.facebookcom Step 4 :EnterUserID&Password Step 5 :ClickLoginbutton Step 6 :Stop Example( 2 )WriteanAlgorithm foraverageof 3 Integer Solution: Step 1 :Start Step 2 :Outputā€œAverageof 3 Integerā€ Step 3 :Outputā€œInputof 3 Integerā€ Step 4 :Inputx,y,z Step 5 :Ave<(x+y+z)/ 3 Step 6 :Outputā€œAverageā€Ave Step 7 :Stop. Example( 3 )WriteanAlgorithm forthelargestof 2 Integers. Solution. Step 1 :Start Step 2 :Outputā€œLargestof 2 Integersā€

Step 3 :Outputā€œInput 2 Integersā€ Step 4 :Inputx,y Step 5 :If(x> 9 y) Step 6 :Outputā€œlargestisā€x Step 7 :Else Step 8 :Outputā€œlargestisā€y Step 9 :Stop AdvantagesoftheAlgorithm ļ‚· Itidentifiesthesolutionprocess,decisionpointsandvariables requiredtosolvetheproblem. ļ‚· Ithelpsindividingahugeproblem intosmallermanageablestepsof thesolution. ļ‚· Theanalysisandspecificationoftheprocessleadtotheefficiency. ļ‚· Separationofthestepsdivideslabouranddevelopmentexpertise. DisadvantagesoftheAlgorithm ļ‚· Ataspecificpoint,thealgorithm terminates. ļ‚· Inabilitytosolveproblemsthatgeneratenon-computationalresults. ļ‚· Consumesalotoftime. Flowchart Flowchartisatypeofdiagram thatrepresentanalgorithms.Aninformation diagram madeupofdifferentshapesshowsthedataflow. Flowchartisatooldevelopedinthecomputerindustry,forshowingthe stepsinvolvedinaprocess.Thearrowsshowtheorderinwhichtheyoccur. Flowchartingcombinessymbolsandflow lines,toshow figurativelythe operationofanalgorithm. Incomputing,therearedozensofdifferentsymbolsusedinflowcharting (thereareevennationalandinternationalflowchartingsymbolstandards).

GeneralRulesforflowcharting 1 .AllboxesoftheflowchartareconnectedwithArrows.(Notlines) 2 .Flowchartsymbolshaveanentrypointonthetopofthesymbolwith nootherentrypoints.Theexitpointforallflowchartsymbolsison thebottom exceptfortheDecisionsymbol. 3 .TheDecisionsymbolhastwoexitpoints;thesecanbeonthesidesor thebottom andoneside. 4 .Generallyaflowchartwillflow from top to bottom.However,an upwardflowcanbeshownaslongasitdoesnotexceed 3 symbols. 5 .Connectorsareusedtoconnectbreaksintheflowchart.Examples are:

6 .From onepagetoanotherpage. 7 .From thebottom ofthepagetothetopofthesamepage. 8 .Anupwardflowofmorethen 3 symbols 9 .SubroutinesandInterruptprogramshavetheirownandindependent flowcharts.

  1. AllflowchartsstartwithaTerminalorPredefinedProcess(for interruptprogramsorsubroutines)symbol.
  2. Allflowchartsendwithaterminaloracontentiousloop. FlowchartExamples, ( 1 )flowchartusingtheabovefacebookloginalgorithm. Solution.

( 3 )Flowchartforlargestof 2 Integers. ā€˜ā€™Averageof 3 Integer’’ ā€˜ā€™Input 3 Integerā€ Stop ā€œInputx,y,zā€ Ave (x+y+z)/ 3 Outputā€œAveis ā€œAve

AdvantagesoftheFlowChart Start ā€˜ā€™Largestof 2 Integers’’ ā€˜ā€™Input 2 Integerā€ Stop Outputā€œLargestisā€ Y Outputā€œLargest isā€x Inputx,y x>y