C语言学习第二章(英文版)课件.ppt

上传人(卖家):晟晟文业 文档编号:5203610 上传时间:2023-02-17 格式:PPT 页数:38 大小:200KB
下载 相关 举报
C语言学习第二章(英文版)课件.ppt_第1页
第1页 / 共38页
C语言学习第二章(英文版)课件.ppt_第2页
第2页 / 共38页
C语言学习第二章(英文版)课件.ppt_第3页
第3页 / 共38页
C语言学习第二章(英文版)课件.ppt_第4页
第4页 / 共38页
C语言学习第二章(英文版)课件.ppt_第5页
第5页 / 共38页
点击查看更多>>
资源描述

1、The number of the group:The number of the group:7716202377162023Chapter 2 Chapter 2 Algorithm and FlowchartAlgorithm and Flowchartn2.1 What is algorithmn2.2 Features of algorithmsn2.3 Pseudocoden2.4 Flowchartn2.5 Control Structuresn2.6 Selection Structuren2.7 Repetition StructureOutlineOutlineKey po

2、intsKey pointsnWhat is algorithmnFeatures of algorithmsnFlowchart2.2.1 What is algorithm1 What is algorithmnBefore writing a program:nHave a thorough understanding of the problem nCarefully plan an approach for solving itnWhile writing a program:nKnow what”building blocks”are availablenUse good prog

3、ramming principles2.2.1 What is algorithm1 What is algorithm A step-by-step problem-solving procedure,especially an established,recursive computational procedure for solving a problem in a finite number of steps.2.2.2 Features of algorithm2 Features of algorithmnDeterminismnAn algorithm is determini

4、stic,if at any stage of the algorithm execution,the next action step is clearly defined.nFinitenessnThe finiteness restricts the definition of the algorithm to a finite long-term operation.nEffectivenessnThe effect of each statement of an algorithm must be clearly defined.2.2.3 Pseudocode3 Pseudocod

5、enPseudocodenArtificial,informal language that helps us develop algorithmsnSimilar to everyday EnglishnNot actually executed on computers nHelps us“think out”a program before writing it nEasy to convert into a corresponding C programnConsists only of executable statements2.2.3 Pseudocode3 Pseudocode

6、nExample of pseudocode nIf students grade is greater than or equal to 60 Print“Passed”nIf students grade is greater than or equal to 60 Print“Passed”else Print“Failed”2.2.4 Flowchart4 FlowchartnFlowchart nGraphical representation of an algorithmnDrawn using certain special-purpose symbols connected

7、by arrows called flowlinesnRectangle symbol(action symbol):nIndicates any type of actionnOval symbol:nIndicates the beginning or end of a program or a section of codenSingle-entry/single-exit control structures nConnect exit point of one control structure to entry point of the next(control-structure

8、 stacking)nMakes programs easy to build2.2.5 Control Structures5 Control StructuresnSequential execution nStatements executed one after the other in the order writtennTransfer of controlnWhen the next statement executed is not the next one in sequencenOveruse of goto statements led to many problemsn

9、 Bohm and JacopininAll programs written in terms of 3 control structuresnSequence structures:Built into C.Programs executed sequentially by defaultnSelection structures:C has three types:if,if/else,and switch(book chapter 4)nRepetition structures:C has three types:while,do/while and forn Single-entr

10、y/single-exit control structures nConnect exit point of one control structure to entry point of the next(control-structure stacking)nMakes programs easy to build2.2.5 Control Structures5 Control Structures2.2.6 Selection Structure(if)6 Selection Structure(if)nSelection structure:nUsed to choose amon

11、g alternative courses of actionnPseudocode:nIf students grade is greater than or equal to 60Print“Passed”nIf condition true nPrint statement executed and program goes on to next statementnIf false,print statement is ignored and the program goes onto the next statementnIndenting makes programs easier

12、 to readnC ignores whitespace characters2.2.6 Selection Structure(if)6 Selection Structure(if)nPseudocode statement in C:if(grade=60)printf(Passedn);nC code corresponds closely to the pseudocodenDiamond symbol(decision symbol)nIndicates decision is to be madenContains an expression that can be true

13、or falsenTest the condition,follow appropriate path2.2.6 Selection Structure(if)6 Selection Structure(if)nif structure is a single-entry/single-exit structureyesnograde=60print“Passed”A decision can be made on any expression.zero-no nonzero-yesExample:3-4 is yes2.2.6 Selection Structure(if)6 Selecti

14、on Structure(if)n Page 32 nIf you type a number less than 10,you get a message“What an obedient servant you are!”on the screen.nOtherwise,the program doesnt do anything.STARTPRINT enter a num less than 10 INPUT num is num=60)printf(Passedn);else printf(Failedn);nConditional operator(?:)nTakes three

15、arguments(condition,value if true,value if false)nOur pseudocode could be written:printf(%sn,grade=60?Passed:Failed);nOr it could have been written:grade=60?printf(“Passedn”):printf(“Failedn”);2.2.6 Selection Structure(if/else)6 Selection Structure(if/else)nFlow chart of the if/else selection struct

16、urenNested if/else structures nTest for multiple cases by placing if/else selection structures inside if/else selection structuresnOnce condition is met,rest of statements skippednDeep indentation usually not used in practiceyesnoprint“Failed”print“Passed”grade=602.2.6 Selection Structure(if/else)6

17、Selection Structure(if/else)nPseudocode for a nested if/else structureIf students grade is greater than or equal to 90 Print“A”else If students grade is greater than or equal to 80 Print“B”else If students grade is greater than or equal to 70 Print“C”else If students grade is greater than or equal t

18、o 60 Print“D”else Print“F”2.2.6 Selection Structure(if/else)6 Selection Structure(if/else)nCompound statement:nSet of statements within a pair of bracesnExample:if(grade=60)printf(Passed.n);else printf(Failed.n);printf(You must take this course again.n);nWithout the braces,the statementprintf(You mu

19、st take this course again.n);would be executed automatically2.2.6 Selection Structure(if/else)6 Selection Structure(if/else)nBlock:nCompound statements with declarationsnSyntax errorsnCaught by compilernLogic errors:nHave their effect at execution timenNon-fatal:program runs,but has incorrect output

20、nFatal:program exits prematurely2.2.6 Selection Structure(switch)6 Selection Structure(switch)nswitch(switch-case-default)allows us to make a decision from a number of choices.nThe switch statement most often appears as follows:switch(integer expression)case constant1:do this;case constant2:do this;

21、case constant3:do this;default:do this;2.2.6 Selection Structure(switch)6 Selection Structure(switch)nswitch(choice)case 1:statement1;break;case 2:statement2;break;case 3:statement3;break;case 4:statement4;break;/*in Page 86 of the textbook*/STARTcase 1case 2case 3case 4nonononoyesyesyesyesstatement

22、1statement2statement3statement4 STOP2.2.7 Repetition Structure(while)7 Repetition Structure(while)nRepetition structurenProgrammer specifies an action to be repeated while some condition remains truenPsuedocode:While there are more items on my shopping list Purchase next item and cross it off my lis

23、t nwhile loop repeated until condition becomes false 2.2.7 Repetition Structure(while)7 Repetition Structure(while)nExample:int product=2;while(product=1000)product=2*product;product=1000product=2*product yesno2.2.7 Repetition Structure(for)7 Repetition Structure(for)nThe for is probably the most po

24、pular looping instruction in C.nThe for allows us to specify three things in a loopnSetting a loop counter to an initial valuenTesting whether the loop counter reaches the desired number nIncreasing the value of loop counter2.2.7 Repetition Structure(for)7 Repetition Structure(for)nThe general form

25、of the for loop:for(initialise counter;test;increment)body of the loop;2.2.7 Repetition Structure(for)7 Repetition Structure(for)nThe flowchart of the for loop:initialise START test STOP False True body of loop increment2.2.7 Repetition Structure(for)7 Repetition Structure(for)nExample program(Page

26、67)#include void main()int p,n,count;float r,si;for(count=1;count=3;count=count+1)printf(”Enter Values of p,n and r”);scanf(”%d%d%f”,&p,&n,&r);si=p*n*r/100;printf(”Simple Interest=Rs.%fn”,si);2.2.7 Repetition Structure(for)7 Repetition Structure(for)nFlowchart of the program START count=1count=count

27、+1 iscount=3 STOPNoYesINPUT p,n,r si=p*n*r/100PRINT si2.2.7 Repetition Structure(for)7 Repetition Structure(for)nThe for statement gets executed as follows:nThe value of count is set to an initial value 1.nThe condition count=3 is tested.The body of the loop is executed for the first time.nUpon reac

28、hing the closing brace of for,control is sent back to for,the value of count gets incremented by 1.nAgain the test count=3 is performed.nThe body of the loop continues to get executed till count doesnt exceed the value 3.nWhen count reaches 4,the control exits from the loop and transferred to the ne

29、xt statement.SummarySummarynAn algorithm is a step-by-step problem-solving procedure in a finite number of steps.nThe features of algorithm are determinism,finiteness and effectiveness.nFlowchart is a graphical representation of an algorithm.nC has three types of selection structures:if,if/else,and

30、switch.nC has three types of repetition structures:while,do/while and for.AssignmentAssignmentnRepresent the following Pseudocode as a flowchart.If students grade is greater than or equal to 90 Print“A”else If students grade is greater than or equal to 80 Print“B”else If students grade is greater th

31、an or equal to 70 Print“C”else If students grade is greater than or equal to 60 Print“D”else Print“F”AssignmentAssignmentnInput three integers,find out and output the maximum.Please present the algorithm of this problem,and describe it by flowchart.nInput the ages of ten students one by one,calculat

32、e the average age of them and output the average age finally.Please present the algorithm of this problem,and describe it by flowchart.nInput the ages of ten students one by one,find out the minimum age of them and output it finally.Please present the algorithm of this problem,and describe it by flowchart.Thank you!Thank you!See you next time!

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(C语言学习第二章(英文版)课件.ppt)为本站会员(晟晟文业)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|