




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 静物联考题目大全及答案
- 旅游地产项目绿色规划与设计指南:2025年实现可持续发展的关键要素
- 教育信息化2.0背景下幼儿园教师专业成长路径报告
- 聚焦2025:能源互联网区域能源结构优化与转型研究报告
- 绿色金融债券市场发行环境优化与2025年投资收益预测报告
- 透析血管的保养与护理
- 品牌管理精髓产品概念与产品定位
- 抽烟的面试题及答案
- 地质试题及答案
- 千年前的高考试题及答案
- T-GXAS 768-2024 尿中反-反式粘糠酸的测定 液相色谱-质谱联用法
- 四川省通信产业服务有限公司笔试题库
- 患者医疗信息管理制度
- 罪犯个别教育转化案例、罪犯X某的矫治个案、教育改造案例2023(共5篇)
- 石漠化综合治理人工造林设计方案
- 2024年物联网安装调试员职业技能竞赛考试题库500题(含答案)
- 《建筑施工技术》课件-砌筑工程施工
- 图文制作服务 投标方案(技术方案)
- 高中英语外研版 单词表 必修3
- 第十四届陕西省气象行业职业技能(县级综合气象业务)竞赛理论试题库-下(多选、判断题)
- 2023年新疆克州高校毕业生“三支一扶”计划招募考试真题
评论
0/150
提交评论