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

上传人:20****08 文档编号:242237730 上传时间:2024-08-17 格式:PPT 页数:38 大小:228.74KB
返回 下载 相关 举报
C语言学习第二章(英文版)ppt课件_第1页
第1页 / 共38页
C语言学习第二章(英文版)ppt课件_第2页
第2页 / 共38页
C语言学习第二章(英文版)ppt课件_第3页
第3页 / 共38页
点击查看更多>>
资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Chapter 2 Algorithm and Flowchart,School of Software, Nanchang HangKong University,1,Faculty of Computing, Faculty of Software, Nanchang HangKong University, number of the group:,77162023,The number of the group:771620,Chapter 2,Algorithm and Flowchart,Chapter 2 Algorithm and Flowc,2.1 What is,algorithm,2.2 Features of algorithms,2.3,Pseudocode,2.4,Flowchart,2.5 Control Structures,2.6 Selection Structure,2.7 Repetition Structure,Outline,2.1 What is algorithmOutline,Key points,What is algorithm,Features of algorithms,Flowchart,Key pointsWhat is algorithm,2.,1 What is algorithm,Before writing a program:,Have a thorough understanding of the problem,Carefully plan an approach for solving it,While writing a program:,Know what ”building blocks” are available,Use good programming principles,2.1 What is algorithmBefore wr,2.,1 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.1 What is algorithm A,2.,2 Features of algorithm,Determinism,An algorithm is deterministic, if at any stage of the algorithm execution, the next action step is clearly defined.,Finiteness,The finiteness restricts the definition of the algorithm to a finite long-term operation.,Effectiveness,The effect of each statement of an algorithm must be clearly defined.,2.2 Features of algorithmDeter,2.,3 Pseudocode,Pseudocode,Artificial, informal language that helps us develop algorithms,Similar to everyday English,Not actually executed on computers,Helps us “,think out,” a program before writing it,Easy to convert into a,corresponding,C program,Consists only of executable statements,2.3 PseudocodePseudocode,2.,3 Pseudocode,Example of pseudocode,If students grade is greater than or equal to 60,Print “Passed”,If students grade is greater than or equal to 60 Print “Passed”,else Print “Failed”,2.3 PseudocodeExample of pseud,2.,4 Flowchart,Flowchart,Graphical representation of an algorithm,Drawn using certain special-purpose symbols connected by arrows called flowlines,Rectangle symbol,(action symbol):,Indicates any type of action,Oval symbol:,Indicates the beginning or end of a program or a section of code,Single-entry/single-exit control structures,Connect exit point of one control structure to entry point of the next (control-structure stacking),Makes programs easy to build,2.4 FlowchartFlowchart,C语言学习第二章(英文版)ppt课件,C语言学习第二章(英文版)ppt课件,2.,5 Control Structures,Sequential execution,Statements executed one after the other in the order written,Transfer of control,When the next statement executed is not the next one in sequence,Overuse of goto statements led to many problems,2.5 Control Structures,Bohm and Jacopini,All programs written in terms of 3 control structures,Sequence structures: Built into C. Programs executed sequentially by default,Selection structures: C has three types:,if,if,/,else, and,switch(book chapter 4),Repetition structures: C has three types:,while,do,/,while,and,for,Single-entry/single-exit control structures,Connect exit point of one control structure to entry point of the next (control-structure stacking),Makes programs easy to build,2.,5 Control Structures,Bohm and Jacopini2.5 Control S,2.,6 Selection Structure (if),Selection structure:,Used to choose among alternative courses of action,Pseudocode:,If students grade is greater than or equal to 60Print “Passed”,If condition true,Print statement executed and program goes on to next statement,If false, print statement is ignored and the program goes onto the next statement,Indenting makes programs easier to read,C ignores whitespace characters,2.6 Selection Structure (if)Se,2.,6 Selection Structure (if),Pseudocode statement in C:,if ( grade = 60 ) printf( Passedn );,C code corresponds closely to the pseudocode,Diamond symbol,(decision symbol),Indicates decision is to be made,Contains an expression that can be true or false,Test the condition, follow appropriate path,2.6 Selection Structure (if)Ps,2.,6 Selection Structure (if),if structure is a single-entry/single-exit structure,yes,no,grade = 60,print “Passed”,A decision can be made on any expression.,zero - no,nonzero - yes,Example:,3 - 4 is yes,2.6 Selection Structure (if)if,2.,6 Selection Structure (if),Page 32,If you type a number less than 10, you get a message “,What an obedient servant you are!,”on the screen.,Otherwise, the program doesnt do anything.,START,PRINT enter a num,less than 10,INPUT num,is,num= 60 ),printf( Passedn);,else,printf( Failedn);,Conditional operator,(?:),Takes three arguments (condition, value if true, value if false),Our pseudocode could be written:,printf( %sn, grade = 60 ? Passed : Failed );,Or it could have been written:,grade = 60 ? printf( “Passedn” ) : printf( “Failedn” );,2.6 Selection Structure (if/el,2.,6 Selection Structure (if/else),Flow chart of the if/else selection structure,Nested if/else structures,Test for multiple cases by placing if/else selection structures inside if/else selection structures,Once condition is met, rest of statements skipped,Deep indentation usually not used in practice,yes,no,print “Failed”,print “Passed”,grade = 60,2.6 Selection Structure (if/el,2.,6 Selection Structure (if/else),Pseudocode for a nested,if,/,else,structure,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 than or equal to 70 Print “C” else If students grade is greater than or equal to 60 Print “D” else Print “F”,2.6 Selection Structure (if/el,2.,6 Selection Structure (if/else),Compound statement:,Set of statements within a pair of braces,Example:,if ( grade = 60 ),printf( Passed.n );,else ,printf( Failed.n );,printf( You must take this course again.n );,Without the braces, the statement,printf( You must take this course again.n );,would be executed automatically,2.6 Selection Structure (if/el,2.,6 Selection Structure (if/else),Block:,Compound statements with declarations,Syntax errors,Caught by compiler,Logic errors:,Have their effect at execution time,Non-fatal: program runs, but has incorrect output,Fatal: program exits prematurely,2.6 Selection Structure (if/el,2.,6 Selection Structure (switch),switch,(switch-case-default),allows us to make a decision from a number of choices.,The,switch,statement,most often appears as follows:,switch (integer expression),case constant1: do this;,case constant2: do this;,case constant3: do this;,default: do this;,2.6 Selection Structure (switc,2.,6 Selection Structure (switch),switch (choice),case 1: statement1; break;,case 2: statement2; break;,case 3: statement3; break;,case 4: statement4; break;,/* in Page 86 of the textbook*/,START,case 1,case 2,case 3,case 4,no,no,no,no,yes,yes,yes,yes,statement1,statement2,statement3,statement4,STOP,2.6 Selection Structure (switc,2.,7 Repetition Structure(while),Repetition structure,Programmer specifies an action to be repeated while some condition remains true,Psuedocode:,While there are more items on my shopping list Purchase next item and cross it off my list,while loop repeated until condition becomes false,2.7 Repetition Structure(while,2.,7 Repetition Structure(while),Example:,int product = 2;,while ( product = 1000 )product = 2 * product;,product = 1000,product = 2 * product,yes,no,2.7 Repetition Structure(while,2.,7 Repetition Structure(for),The,for,is probably the most popular looping instruction in C.,The,for,allows us to specify three things in a loop,Setting a loop counter to an initial value,Testing whether the loop counter reaches the desired number,Increasing the value of loop counter,2.7 Repetition Structure(for)T,2.,7 Repetition Structure(for),The general form of the,for,loop:,for(initialise counter; test; increment),body of the loop;,2.7 Repetition Structure(for)T,2.,7 Repetition Structure(for),The flowchart of the for loop:,initialise,START,test,STOP,False,True,body of loop,increment,2.7 Repetition Structure(for)T,2.,7 Repetition Structure(for),Example program (Page 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”, ,si=p*n*r/100;,printf(”Simple Interest =Rs. %fn”, si);,2.7 Repetition Structure(for)E,2.,7 Repetition Structure(for),Flowchart of the program,START,count=1,count=count+1,is,count=3,STOP,No,Yes,INPUT,p,n,r,si=p*n*r/100,PRINT,si,2.7 Repetition Structure(for)F,2.,7 Repetition Structure(for),The,for,statement gets executed as follows:,The value of,count,is set to an initial value,1,.,The condition,count=3,is tested. The body of the loop is executed for the first time.,Upon reaching the closing brace of,for, control is sent back to,for, the value of,count,gets incremented by 1.,Again the test,count=3,is performed.,The body of the loop continues to get executed till,count,doesnt exceed the value,3,.,When count reaches,4, the control exits from the loop and transferred to the next statement.,2.7 Repetition Structure(for)T,Summary,An algorithm is a step-by-step problem-solving procedure in a finite number of steps.,The features of algorithm are determinism, finiteness and effectiveness.,Flowchart is a graphical representation of an algorithm.,C has three types of selection structures:,if,if/else, and,switch,.,C has three types of repetition structures:,while,do/while,and,for,.,SummaryAn algorithm is a step-,Assignment,Represent 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 than or equal to 70 Print “C” else If students grade is greater than or equal to 60 Print “D” else Print “F”,AssignmentRepresent the follow,Assignment,Input three integers, find out and output the maximum. Please present the algorithm of this problem, and describe it by flowchart.,Input the ages of ten students one by one, calculate the average age of them and output the average age finally. Please present the algorithm of this problem, and describe it by flowchart.,Input 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.,AssignmentInput three integers,Thank you!,See you next time!,Thank you!See you next time!,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!