Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.

Similar presentations


Presentation on theme: "BIL 104E Introduction to Scientific and Engineering Computing Lecture 5."— Presentation transcript:

1 BIL 104E Introduction to Scientific and Engineering Computing Lecture 5

2 Algorithm Development Decomposition outlines provide the first definition of a problem solution. This outline is written sequential steps and can be shown in a diagram or a step- by-step outline. For very simple problems we can go from the decomposition outline directly to the C statements. However, for most problems the decomposition outline needs to be refined into a description with more detail by breaking the problem solution into smaller and smaller portions. The refinement of an outline into more detailed steps can be done with pseudocode or a flowchart. Pseudocode uses English-like statements to describe the steps in an algorithm; a flowchart uses a diagram to describe the steps in an algorithm. 13.06.2016Lecture 52

3 Algorithm Development Basic OperationPseudocode Notation InputRead radius ComputationSet area to π x radius 2 OutputPrint radius, area Comparisonsif radius < 0 then … Beginning of algorithmmain: End of algorithm 13.06.2016Lecture 53 Read radius area= π x radius 2 Print radius, area radius < 0 Yes No start main

4 Structured Program A structured program is one written using simple control structures, – sequence, – selection – repetition to organize the solution into a problem. A sequence structure contains steps that are performed one after another. 13.06.2016Lecture 54

5 Structured Program A selection structure contains one set of steps that is performed if a condition is true and another set of steps that is performed if the condition is false. 13.06.2016Lecture 55

6 Structured Program A repetition structure contains a set of steps that is repeated as long as a condition is true. 13.06.2016Lecture 56 set time to 0 while time≤10 compute velocity print velocity increment time by 1

7 if Statement General Form: if the condition is true then statement 1 is executed. If the condition is false then statement 1 is skipped. if (condition) statement 1; A compound statement or block, which is composed of a set of statements enclosed in braces, can also be used. if (condition) { statement 1; statement 2; … statement n; } 13.06.2016Lecture 57 Example if (a < 50) { ++count; sum += a; }

8 if Statement if statements can also be nested: Example: if (a < 50) { ++count; sum += a; if (b > a) b = 0; } 13.06.2016Lecture 58

9 if Statement example 1

10 Enter three integers: 22 85 17 Maximum is: 85 Enter three integers: 85 22 17 Maximum is: 85 Enter three integers: 22 17 85 Maximum is: 85 if Statement example 1

11 13.06.2016Lecture 511 if Statement example 2

12 13.06.2016Lecture 512 if Statement example 2

13 13.06.2016Lecture 513 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7

14 if/else Statement The if/else statement allows to execute one set of statements if a condition is true and a different set if the condition is false. General Form: if (condition) statement1; else statement2; Statements 1 and 2 can also be an empty statement, which is a semicolon. if (a < b) ; else count++; 13.06.2016Lecture 514 if (a >= b) count++;

15 if/else Statement Example: if (x > y) if (y < z) k++; else m++; else j++; 13.06.2016Lecture 515

16 if/else Statement if (x > y) if (y < z) k++; else j++; if (x > y) if (y < z) k++; else j++; if (x > y) { if (y < z) k++; } else j++; 13.06.2016Lecture 516 Identation is just for style and it does not change interpretation.

17 if/else Statement In general do not use equality operator when comparing floating-point numbers. For example, instead of comparing denominator to zero, the absolute value of the denominator can be compared with a very small value. Example: if (fabs(denominator) < 0.0001) printf("Denominator of x is equal to zero\n"); else { fraction = numerator/denominator; printf("fraction = %f \n",fraction); } 13.06.2016Lecture 517

18 if/else Statement example if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } 13.06.2016Lecture 518

19 else-if Statement If's and else's can be used to construct logic that branches one of several ways and then rejoins, a common programming structure, in this way: The conditions are tested in order, and exactly one block is executed; either the first one whose if is satisfied, or the one for the last else. When this block is finished, the next statement executed is the one after the last else. If no action is to be taken for the ``default'' case, omit the last else. 13.06.2016Lecture 519 If (...) {...} else if (...) {...} else if(...) {...} else {...}

20 else-if Statement #include int main( ) { int let, dig, other, c; let = dig = other = 0; while( (c=getchar( )) != '\0' ) if( ('A'<=c && c<='Z') || ('a'<=c && c<='z') ) ++let; else if( '0'<=c && c<='9' ) ++dig; else ++other; printf("%d letters, %d digits, %d others\n", let, dig, other); return 0; } 13.06.2016Lecture 520

21 Ternary conditional operator (?:) Takes three arguments (condition, value if true, value if false) Example: printf( "%s\n", grade >= 60 ? "Passed" : "Failed" ); OR grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” ); OR if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n"); 13.06.2016Lecture 521

22 Switch-Case The switch statement can be used to make unlimited decisions or choices based on the value of a conditional expression and specified cases. The general form: switch (expression) { case expression1: statement1; break; case expression2: statement2; break;... default: statement-default; break; } 13.06.2016Lecture 522

23 Switch-Case Example: İnt day; switch (day){ case 1: printf("Day 1\n"); break; case 2: printf("Day 2\n"); break; default: printf("Invalid option selected"); } 13.06.2016Lecture 523

24

25

26

27 Enter the letter grades. Enter the EOF character to end input. a b c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1

28 #include int main() { float numb1 = 0, numb2 = 0; /* the two numbers to work on */ int menu = 1; /* add or substract or divide or multiply */ float total = 0; /* the result of the calculation */ char calType; /* what type of calculation */ printf("Please enter in the first of the two numbers\n\t"); scanf("%f", &numb1); /* READ first number */ printf("\n\nPlease enter the second of the two numbers\n\t"); scanf("%f", &numb2); /* READ second number */ printf("\n\nWhat would you like to do?\n\n"); /* WRITE instructions */ printf("\t1 = add\n"); printf("\t2 = substract\n"); printf("\t3 = multiply\n"); printf("\t4 = divide\n"); printf("\n\nPleas make your selection now:\n\t"); scanf("%d",&menu); /* READ calculation type */ 13.06.2016Lecture 528

29 switch (menu) /* select the type of calculation */ { case 1: total = numb1 + numb2; calType = '+'; /* assign a char to symbolise calculation type */ break; case 2: total = numb1 - numb2; calType = '-'; break; case 3: total = numb1 * numb2; calType = '*'; break; case 4: total = numb1 / numb2; calType = '/'; break; default: printf("Invalid option selected\n"); } if (menu == 3 && numb2 == 0) /* cannot divide by 0 */ printf("\n\n\tYou cannot divide by 0\n\n"); /* display result to 2 decimal places */ printf("\n\n*************************"); printf("\n\n\t%.3f %c %.3f = %.2f", numb1, calType, numb2, total); printf("\n\n*************************\n\n"); return 0; } 13.06.2016Lecture 529


Download ppt "BIL 104E Introduction to Scientific and Engineering Computing Lecture 5."

Similar presentations


Ads by Google