版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Thenumberofthegroup:77162023Thenumberofthegroup:771620Chapter2AlgorithmandFlowchartChapter2AlgorithmandFlowc2.1Whatisalgorithm2.2Featuresofalgorithms 2.3Pseudocode2.4Flowchart2.5ControlStructures2.6SelectionStructure2.7RepetitionStructureOutline2.1WhatisalgorithmOutlineKeypointsWhatisalgorithmFeaturesofalgorithms FlowchartKeypointsWhatisalgorithm2.1WhatisalgorithmBeforewritingaprogram:Haveathoroughunderstandingoftheproblem
CarefullyplananapproachforsolvingitWhilewritingaprogram:Knowwhat”buildingblocks”areavailableUsegoodprogrammingprinciples2.1WhatisalgorithmBeforewr2.1Whatisalgorithm
Astep-by-stepproblem-solvingprocedure,especiallyanestablished,recursivecomputationalprocedureforsolvingaprobleminafinitenumberofsteps.2.1WhatisalgorithmA2.2FeaturesofalgorithmDeterminismAnalgorithmisdeterministic,ifatanystageofthealgorithmexecution,thenextactionstepisclearlydefined.FinitenessThefinitenessrestrictsthedefinitionofthealgorithmtoafinitelong-termoperation.EffectivenessTheeffectofeachstatementofanalgorithmmustbeclearlydefined.2.2FeaturesofalgorithmDeter2.3Pseudocode PseudocodeArtificial,informallanguagethathelpsusdevelopalgorithmsSimilartoeverydayEnglishNotactuallyexecutedoncomputersHelpsus“thinkout”aprogrambeforewritingitEasytoconvertintoa
corresponding
CprogramConsistsonlyofexecutablestatements2.3Pseudocode Pseudocode2.3PseudocodeExampleofpseudocode
Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”else
Print“Failed”2.3PseudocodeExampleofpseud2.4FlowchartFlowchart
GraphicalrepresentationofanalgorithmDrawnusingcertainspecial-purposesymbolsconnectedbyarrowscalledflowlinesRectanglesymbol
(actionsymbol):IndicatesanytypeofactionOvalsymbol:IndicatesthebeginningorendofaprogramorasectionofcodeSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.4FlowchartFlowchartC语言学习第二章(英文版)课件C语言学习第二章(英文版)课件2.5ControlStructuresSequentialexecution
StatementsexecutedoneaftertheotherintheorderwrittenTransferofcontrolWhenthenextstatementexecutedisnotthenextoneinsequenceOveruseofgotostatementsledtomanyproblems2.5ControlStructuresBohmandJacopiniAllprogramswrittenintermsof3controlstructuresSequencestructures:BuiltintoC.ProgramsexecutedsequentiallybydefaultSelectionstructures:Chasthreetypes:if,if/else,andswitch(bookchapter4)Repetitionstructures:Chasthreetypes:while,do/whileandforSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructuresBohmandJacopini2.5ControlS2.6SelectionStructure(if)Selectionstructure:UsedtochooseamongalternativecoursesofactionPseudocode:Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”IfconditiontruePrintstatementexecutedandprogramgoesontonextstatementIffalse,printstatementisignoredandtheprogramgoesontothenextstatementIndentingmakesprogramseasiertoreadCignoreswhitespacecharacters2.6SelectionStructure(if)Se2.6SelectionStructure(if)PseudocodestatementinC:
if(grade>=60)
printf("Passed\n");
CcodecorrespondscloselytothepseudocodeDiamondsymbol(decisionsymbol)IndicatesdecisionistobemadeContainsanexpressionthatcanbetrueorfalseTestthecondition,followappropriatepath2.6SelectionStructure(if)Ps2.6SelectionStructure(if)ifstructureisasingle-entry/single-exitstructureyesnograde>=60print“Passed”Adecisioncanbemadeonanyexpression.
zero-nononzero-yesExample:3-4isyes2.6SelectionStructure(if)if2.6SelectionStructure(if)
Page32Ifyoutypeanumberlessthan10,yougetamessage“Whatanobedientservantyouare!”onthescreen.Otherwise,theprogramdoesn’tdoanything.
STARTPRINTenteranumlessthan10
INPUTnum
isnum<10PRINTWhatanobedientservantyouare!STOPyesno2.6SelectionStructure(if)P2.6SelectionStructure(if/else)ifOnlyperformsanactioniftheconditionistrueif/elseSpecifiesanactiontobeperformedbothwhentheconditionistrueandwhenitisfalsePsuedocode:Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”else
Print“Failed”Notespacing/indentationconventions
2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Ccode:if(grade>=60)printf("Passed\n");elseprintf("Failed\n");
Conditionaloperator(?:)Takesthreearguments(condition,valueiftrue,valueiffalse)Ourpseudocodecouldbewritten:printf("%s\n",grade>=60?"Passed":"Failed");
Oritcouldhavebeenwritten:grade>=60?printf(“Passed\n”):printf(“Failed\n”);2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Flowchartoftheif/elseselectionstructureNestedif/elsestructuresTestformultiplecasesbyplacingif/elseselectionstructuresinsideif/elseselectionstructuresOnceconditionismet,restofstatementsskippedDeepindentationusuallynotusedinpracticeyesnoprint“Failed”print“Passed”grade>=602.6SelectionStructure(if/el2.6SelectionStructure(if/else)Pseudocodeforanestedif/elsestructure
Ifstudent’sgradeisgreaterthanorequalto90
Print“A”
else
Ifstudent’sgradeisgreaterthanorequalto80
Print“B”
else
Ifstudent’sgradeisgreaterthanorequalto70
Print“C”
else
Ifstudent’sgradeisgreaterthanorequalto60
Print“D”
else
Print“F”2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Compoundstatement:SetofstatementswithinapairofbracesExample:if(grade>=60)printf("Passed.\n");else{printf("Failed.\n");printf("Youmusttakethiscourse
again.\n");}Withoutthebraces,thestatementprintf("Youmusttakethiscourse
again.\n");wouldbeexecutedautomatically2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Block:CompoundstatementswithdeclarationsSyntaxerrorsCaughtbycompilerLogicerrors:HavetheireffectatexecutiontimeNon-fatal:programruns,buthasincorrectoutputFatal:programexitsprematurely2.6SelectionStructure(if/el2.6SelectionStructure(switch)switch
(switch-case-default)allowsustomakeadecisionfromanumberofchoices.Theswitchstatement
mostoftenappearsasfollows:switch(integerexpression){caseconstant1:dothis;caseconstant2:dothis;caseconstant3:dothis;default:dothis;
}2.6SelectionStructure(switc2.6SelectionStructure(switch)switch(choice){case1:statement1;break;case2:statement2;break;
case3:statement3;break;case4:statement4;break;
}/*inPage86ofthetextbook*/STARTcase1case2case3case4nonononoyesyesyesyesstatement1statement2statement3statement4STOP2.6SelectionStructure(switc2.7RepetitionStructure(while) RepetitionstructureProgrammerspecifiesanactiontoberepeatedwhilesomeconditionremainstruePsuedocode:Whiletherearemoreitemsonmyshoppinglist
Purchasenextitemandcrossitoffmylist
whilelooprepeateduntilconditionbecomesfalse
2.7RepetitionStructure(while2.7RepetitionStructure(while)Example:intproduct=2;while(product<=1000)
product=2*product;product<=1000product=2*productyesno
2.7RepetitionStructure(while2.7RepetitionStructure(for)TheforisprobablythemostpopularloopinginstructioninC.TheforallowsustospecifythreethingsinaloopSettingaloopcountertoaninitialvalueTestingwhethertheloopcounterreachesthedesirednumberIncreasingthevalueofloopcounter
2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Thegeneralformoftheforloop:
for(initialisecounter;test;increment){bodyoftheloop;}
2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Theflowchartoftheforloop:initialise
STARTtest
STOPFalseTruebodyofloopincrement2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Exampleprogram(Page67)#include<stdio.h>voidmain(){intp,n,count;floatr,si;for(count=1;count<=3;count=count+1){printf(”EnterValuesofp,nandr”);scanf(”%d%d%f”,&p,&n,&r);si=p*n*r/100;printf(”SimpleInterest=Rs.%f\n”,si);}}
2.7RepetitionStructure(for)E2.7RepetitionStructure(for)FlowchartoftheprogramSTARTcount=1count=count+1iscount<=3STOPNoYesINPUTp,n,rsi=p*n*r/100PRINTsi2.7RepetitionStructure(for)F2.7RepetitionStructure(for)Theforstatementgetsexecutedasfollows:Thevalueofcountissettoaninitialvalue1.Theconditioncount<=3istested.Thebodyoftheloopisexecutedforthefirsttime.Uponreachingtheclosingbraceoffor,controlissentbacktofor,thevalueofcountgetsincrementedby1.Againthetestcount<=3isperformed.Thebodyoftheloopcontinuestogetexecutedtillcountdoesn’texceedthevalue3.Whencountreaches4,thecontrolexitsfromtheloopandtransferredtothenextstatement.2.7RepetitionStructure(for)TSummaryAnalgorithmisastep-by-stepproblem-solvingprocedureinafinitenumberofsteps.Thefeaturesofalgorithmaredeterminism,finitenessandeffectiveness.Flowchartisagraphicalrepresentationofanalgorithm.Chasthreetypesofselectionstructures:if,if/else,andswitch.Chasthreetypesofrepetitionstructures:while,do/whileandfor.SummaryAnalgorithmisastep-AssignmentRepresentthefollowingPseudocodeasaflowchart.
Ifstudent’sgradeisgreaterthanorequalto90
Print“A”
else
Ifstudent’sgradeisgreaterthanorequalto80
Print“B”
else
Ifstudent’sgradeisgreaterthanorequalto70
Print“C”
else
Ifstudent’sgradeisgreaterthanorequalto60
Print“D”
else
Print“F”AssignmentRepresentthefollowAssignmentInputthreeintegers,findoutandoutputthemaximum.Pleasepresentthealgorithmofthisproblem,anddescribeitbyflowchart.Inputtheagesoftenstudentsonebyone,calculatetheaverageageofthemandoutputtheaverageagefinally.Pleasepresentthealgorithmofthisproblem,anddescribeitbyflowchart.Inputtheagesoftenstudentsonebyone,findouttheminimumageofthemandoutputitfinally.Pleasepresentthealgorithmofthisproblem,anddescribeitbyflowchart.AssignmentInputthreeintegersThankyou!!!Seeyounexttime!Thankyou!!!Seeyounexttime!Thenumberofthegroup:77162023Thenumberofthegroup:771620Chapter2AlgorithmandFlowchartChapter2AlgorithmandFlowc2.1Whatisalgorithm2.2Featuresofalgorithms 2.3Pseudocode2.4Flowchart2.5ControlStructures2.6SelectionStructure2.7RepetitionStructureOutline2.1WhatisalgorithmOutlineKeypointsWhatisalgorithmFeaturesofalgorithms FlowchartKeypointsWhatisalgorithm2.1WhatisalgorithmBeforewritingaprogram:Haveathoroughunderstandingoftheproblem
CarefullyplananapproachforsolvingitWhilewritingaprogram:Knowwhat”buildingblocks”areavailableUsegoodprogrammingprinciples2.1WhatisalgorithmBeforewr2.1Whatisalgorithm
Astep-by-stepproblem-solvingprocedure,especiallyanestablished,recursivecomputationalprocedureforsolvingaprobleminafinitenumberofsteps.2.1WhatisalgorithmA2.2FeaturesofalgorithmDeterminismAnalgorithmisdeterministic,ifatanystageofthealgorithmexecution,thenextactionstepisclearlydefined.FinitenessThefinitenessrestrictsthedefinitionofthealgorithmtoafinitelong-termoperation.EffectivenessTheeffectofeachstatementofanalgorithmmustbeclearlydefined.2.2FeaturesofalgorithmDeter2.3Pseudocode PseudocodeArtificial,informallanguagethathelpsusdevelopalgorithmsSimilartoeverydayEnglishNotactuallyexecutedoncomputersHelpsus“thinkout”aprogrambeforewritingitEasytoconvertintoa
corresponding
CprogramConsistsonlyofexecutablestatements2.3Pseudocode Pseudocode2.3PseudocodeExampleofpseudocode
Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”else
Print“Failed”2.3PseudocodeExampleofpseud2.4FlowchartFlowchart
GraphicalrepresentationofanalgorithmDrawnusingcertainspecial-purposesymbolsconnectedbyarrowscalledflowlinesRectanglesymbol
(actionsymbol):IndicatesanytypeofactionOvalsymbol:IndicatesthebeginningorendofaprogramorasectionofcodeSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.4FlowchartFlowchartC语言学习第二章(英文版)课件C语言学习第二章(英文版)课件2.5ControlStructuresSequentialexecution
StatementsexecutedoneaftertheotherintheorderwrittenTransferofcontrolWhenthenextstatementexecutedisnotthenextoneinsequenceOveruseofgotostatementsledtomanyproblems2.5ControlStructuresBohmandJacopiniAllprogramswrittenintermsof3controlstructuresSequencestructures:BuiltintoC.ProgramsexecutedsequentiallybydefaultSelectionstructures:Chasthreetypes:if,if/else,andswitch(bookchapter4)Repetitionstructures:Chasthreetypes:while,do/whileandforSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructuresBohmandJacopini2.5ControlS2.6SelectionStructure(if)Selectionstructure:UsedtochooseamongalternativecoursesofactionPseudocode:Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”IfconditiontruePrintstatementexecutedandprogramgoesontonextstatementIffalse,printstatementisignoredandtheprogramgoesontothenextstatementIndentingmakesprogramseasiertoreadCignoreswhitespacecharacters2.6SelectionStructure(if)Se2.6SelectionStructure(if)PseudocodestatementinC:
if(grade>=60)
printf("Passed\n");
CcodecorrespondscloselytothepseudocodeDiamondsymbol(decisionsymbol)IndicatesdecisionistobemadeContainsanexpressionthatcanbetrueorfalseTestthecondition,followappropriatepath2.6SelectionStructure(if)Ps2.6SelectionStructure(if)ifstructureisasingle-entry/single-exitstructureyesnograde>=60print“Passed”Adecisioncanbemadeonanyexpression.
zero-nononzero-yesExample:3-4isyes2.6SelectionStructure(if)if2.6SelectionStructure(if)
Page32Ifyoutypeanumberlessthan10,yougetamessage“Whatanobedientservantyouare!”onthescreen.Otherwise,theprogramdoesn’tdoanything.
STARTPRINTenteranumlessthan10
INPUTnum
isnum<10PRINTWhatanobedientservantyouare!STOPyesno2.6SelectionStructure(if)P2.6SelectionStructure(if/else)ifOnlyperformsanactioniftheconditionistrueif/elseSpecifiesanactiontobeperformedbothwhentheconditionistrueandwhenitisfalsePsuedocode:Ifstudent’sgradeisgreaterthanorequalto60
Print“Passed”else
Print“Failed”Notespacing/indentationconventions
2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Ccode:if(grade>=60)printf("Passed\n");elseprintf("Failed\n");
Conditionaloperator(?:)Takesthreearguments(condition,valueiftrue,valueiffalse)Ourpseudocodecouldbewritten:printf("%s\n",grade>=60?"Passed":"Failed");
Oritcouldhavebeenwritten:grade>=60?printf(“Passed\n”):printf(“Failed\n”);2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Flowchartoftheif/elseselectionstructureNestedif/elsestructuresTestformultiplecasesbyplacingif/elseselectionstructuresinsideif/elseselectionstructuresOnceconditionismet,restofstatementsskippedDeepindentationusuallynotusedinpracticeyesnoprint“Failed”print“Passed”grade>=602.6SelectionStructure(if/el2.6SelectionStructure(if/else)Pseudocodeforanestedif/elsestructure
Ifstudent’sgradeisgreaterthanorequalto90
Print“A”
else
Ifstudent’sgradeisgreaterthanorequalto80
Print“B”
else
Ifstudent’sgradeisgreaterthanorequalto70
Print“C”
else
Ifstudent’sgradeisgreaterthanorequalto60
Print“D”
else
Print“F”2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Compoundstatement:SetofstatementswithinapairofbracesExample:if(grade>=60)printf("Passed.\n");else{printf("Failed.\n");printf("Youmusttakethiscourse
again.\n");}Withoutthebraces,thestatementprintf("Youmusttakethiscourse
again.\n");wouldbeexecutedautomatically2.6SelectionStructure(if/el2.6SelectionStructure(if/else)Block:CompoundstatementswithdeclarationsSyntaxerrorsCaughtbycompilerLogicerrors:HavetheireffectatexecutiontimeNon-fatal:programruns,buthasincorrectoutputFatal:programexitsprematurely2.6SelectionStructure(if/el2.6SelectionStructure(switch)switch
(switch-case-default)allowsustomakeadecisionfromanumberofchoices.Theswitchstatement
mostoftenappearsasfollows:switch(integerexpression){caseconstant1:dothis;caseconstant2:dothis;caseconstant3:dothis;default:dothis;
}2.6SelectionStructure(switc2.6SelectionStructure(switch)switch(choice){case1:statement1;break;case2:statement2;break;
case3:statement3;break;case4:statement4;break;
}/*inPage86ofthetextbook*/STARTcase1case2case3case4nonononoyesyesyesyesstatement1statement2statement3statement4STOP2.6SelectionStructure(switc2.7RepetitionStructure(while) RepetitionstructureProgrammerspecifiesanactiontoberepeatedwhilesomeconditionremainstruePsuedocode:Whiletherearemoreitemsonmyshoppinglist
Purchasenextitemandcrossitoffmylist
whilelooprepeateduntilconditionbecomesfalse
2.7RepetitionStructure(while2.7RepetitionStructure(while)Example:intproduct=2;while(product<=1000)
product=2*product;product<=1000product=2*productyesno
2.7RepetitionStructure(while2.7RepetitionStructure(for)TheforisprobablythemostpopularloopinginstructioninC.TheforallowsustospecifythreethingsinaloopSettingaloopcountertoaninitialvalueTestingwhethertheloopcounterreachesthedesirednumberIncreasingthevalueofloopcounter
2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Thegeneralformoftheforloop:
for(initialisecounter;test;increment){bodyoftheloop;}
2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Theflowchartoftheforloop:initialise
STARTtest
STOPFalseTruebodyofloopincrement2.7RepetitionStructure(for)T2.7RepetitionStructure(for)Exampleprogram(Page67)#include<stdio.h>voidmain(){intp,n,count;floatr,si;for(count=1;count<=3;count=count+1){printf(”EnterValuesofp,nandr”);scanf(”%d%d%f”,&p,&n,&r);si=p*n*r/100;printf(”SimpleInter
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2027届中国飞机强度研究所校园招聘笔试参考题库及答案详解
- 2026江苏南京大学YJ20260080政府管理学院博士后招聘1人考试备考试题及答案详解
- 2026福建省花卉盆景有限公司招聘2人考试参考题库及答案详解
- 上海市莘光学校教师招聘(2027学年第一批)(教师编)笔试备考题库及答案详解
- 2026年下半年新疆维吾尔自治区招聘事业单位工作人员4789人考试参考题库及答案详解
- 2026河北沧州市科技创业中心大学生就业见习考试参考题库及答案详解
- 八年级物理沪科版《机械效率》导学案教学设计与实施
- 2026年贵州磷化农资有限责任公司第二批次营销岗位招聘79人考试参考试题及答案详解
- 2026上海浦东新区人大常委会办公室文员公开招聘1人笔试备考题库及答案详解
- 2026广东珠海国际仲裁院招聘7人考试备考试题及答案详解
- 2026年餐饮企业食材检测合同协议
- 国家层面“十五五”产业规划与布局:产业研究专题系列报告之一规划篇
- 过剩空气系数的计算方法
- 建筑工程设计规范
- 能源管理体系培训课件教学
- TJSTJXH5-2022高延性混凝土加固技术规程
- DB31∕T 618-2022 电网电能计量装置配置技术规范
- GB/T 21387-2025供水系统用轴流式止回阀
- 设备除锈与刷漆标准规范手册
- 铁路工务安全教育课件
- 2025-2030年中国药食同源行业市场现状调查及未来趋势研判报告
评论
0/150
提交评论