C语言学习第二章(英文版)课件_第1页
C语言学习第二章(英文版)课件_第2页
C语言学习第二章(英文版)课件_第3页
C语言学习第二章(英文版)课件_第4页
C语言学习第二章(英文版)课件_第5页
已阅读5页,还剩71页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论