CSI 121 Structure Programming Language Lecture 19 Flowcharts

  • Slides: 60
Download presentation
CSI 121 Structure Programming Language Lecture 19 Flowcharts and Debugging 1

CSI 121 Structure Programming Language Lecture 19 Flowcharts and Debugging 1

Topics • The Software Development Cycle • Flowcharts – Selection – Sequence – Iteration

Topics • The Software Development Cycle • Flowcharts – Selection – Sequence – Iteration • How to diagnoze errors in your program? – Methods for debugging – Methods for testing 2

Components of the Software Development Process ØDefine the problem clearly ØAnalyze the problem ØDesign

Components of the Software Development Process ØDefine the problem clearly ØAnalyze the problem ØDesign an algorithm Øtop-down design ØCode (Implement) the algorithm ØTest the code ØDocument the system 3

Development Cycle Analysis Design Implement Test debugging and testing 4

Development Cycle Analysis Design Implement Test debugging and testing 4

Debugging and Testing • Debugging: the process of finding and correcting errors (a. k.

Debugging and Testing • Debugging: the process of finding and correcting errors (a. k. a “bugs”) • Testing: executing the program on a test data set 5

Types of Errors • syntactic: how instructions are written Example 1: while (i=0; i

Types of Errors • syntactic: how instructions are written Example 1: while (i=0; i < 5; i++) { printf(“%dn”, i); } • semantic: what they represent Example 2: n=0; …; n=6; … for (i=n; i != 5; i++); { printf(“%dn”, i); } Example 3: if (choice = ‘Q’) { break; } 6

Flowcharts • Represent flow of control of algorithms: – sequences – selection – iteration

Flowcharts • Represent flow of control of algorithms: – sequences – selection – iteration • Useful for: – Finding semantic errors – Determining test data set 7

Sequence (revision) • Series of instructions to be carried out in a fixed sequential

Sequence (revision) • Series of instructions to be carried out in a fixed sequential order • Example 1: Step A: input number Step B: add 1 to number Step C: output number 8

Flowchart: Sequence • Represented by concatenating instructions (usually vertically) Instruction in rectangular box Step

Flowchart: Sequence • Represented by concatenating instructions (usually vertically) Instruction in rectangular box Step A: input number Step B: add 1 to number Order of execution indicated by arrows Step C: output number 9

Sequence (cont) Example 2: Step A: input number Step B: if number is negative,

Sequence (cont) Example 2: Step A: input number Step B: if number is negative, then add -1 to number else add 1 to number Step C: output number Step A: input number an algorithm involving “selection” Step C: output number 10

Flowchart: Selection Step A if ( condition C 1 ) { <sequence S 1>

Flowchart: Selection Step A if ( condition C 1 ) { <sequence S 1> } else { <sequence S 2> } Step C Arrow labeled with result of condition test YES Step A C 1 true? S 1 Condition test in diamond NO S 2 Step C 11

Example: Algorithm to Flowchart input number if number is negative, then add -1 to

Example: Algorithm to Flowchart input number if number is negative, then add -1 to number else add 1 to number YES output number input number is number negative? add -1 to number NO add 1 to number output number 12

Flowchart: Iteration (while loop) while ( condition C 1 ) { <sequence S 1>

Flowchart: Iteration (while loop) while ( condition C 1 ) { <sequence S 1> } C 1 true? NO YES S 1 13

Flowchart: Iteration (for loop) for ( init ; condition C 1 ; increment )

Flowchart: Iteration (for loop) for ( init ; condition C 1 ; increment ) { <sequence S 1> } init C 1 true? NO YES S 1 increment 14

How to choose which iteration? • Do you know exactly how many times the

How to choose which iteration? • Do you know exactly how many times the loop will execute? – If yes, then use FOR • Is it possible the sequence may never be executed? – If yes, then use WHILE 15

Example: Code to Flowchart (Spot the error!) set i to 0 for ( i=0;

Example: Code to Flowchart (Spot the error!) set i to 0 for ( i=0; i<10; i++ ) { scanf(“%dn”, &x); if ( x < 0 ) { break; } } is i < 10 ? NO YES input value for x increment i NO is x < 0 ? YES 16

Example: Code to Flowchart (correct version) set i to 0 for ( i=0; i<10;

Example: Code to Flowchart (correct version) set i to 0 for ( i=0; i<10; i++ ) { scanf(“%dn”, &x); if ( x < 0 ) { break; } } is i < 10 ? NO YES input value for x is x < 0 ? YES NO increment i 17

Algorithm to Flowchart Example: Calculating Mean input total. Numbers input value for total. Numbers

Algorithm to Flowchart Example: Calculating Mean input total. Numbers input value for total. Numbers set sum to 0 set count to 0 set sum to 0 while (count < total. Numbers) { set count to 0 input next. Num add next. Num to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count 18

Algorithm to Flowchart Example: Calculating Mean (cont) input total. Numbers set sum to 0

Algorithm to Flowchart Example: Calculating Mean (cont) input total. Numbers set sum to 0 set count to 0 while (count < total. Numbers) { input next. Num add next. Num to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count NO is count< total. Numbers? YES input value for next. Num add next. Num to sum increment count 19

Algorithm to Flowchart Example: Calculating Mean (cont) input total. Numbers set sum to 0

Algorithm to Flowchart Example: Calculating Mean (cont) input total. Numbers set sum to 0 set count to 0 while (count < total. Numbers) { input next. Num add next. Num to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count output value for sum/count 20

Algorithm to Flowchart Exercise: Calculating Mean (cont) input total. Numbers set sum to 0

Algorithm to Flowchart Exercise: Calculating Mean (cont) input total. Numbers set sum to 0 set count to 0 while (count < total. Numbers) { input next. Num add next. Num to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count What if total. Numbers is zero? 21

Algorithm to Flowchart Exercise: Calculating Mean (cont) • Modify the flowchart to add an

Algorithm to Flowchart Exercise: Calculating Mean (cont) • Modify the flowchart to add an extra check so that the mean is output only when count is positive 22

Use of Flowcharts • Pseudo-code flowchart • Flowchart code 23

Use of Flowcharts • Pseudo-code flowchart • Flowchart code 23

Debugging Basics • Know the (C) language well Examples: float x, y, z =

Debugging Basics • Know the (C) language well Examples: float x, y, z = 3. 5; printf(“%dn”, &num); scanf(“%f”, x); scanf(“%s”, &name); if (i < N); { scanf(“%dn”, &i); } 24

Debugging Basics (cont) • Pay attention to compiler error and warning messages Examples: if

Debugging Basics (cont) • Pay attention to compiler error and warning messages Examples: if (ch = ‘Q’) { break; } int N; … “Possible incorrect assignment” “‘N’ might be used uninitialized in this function” scanf(“%d”, N); 25

Tracing • Trace execution of a program: – location in the program – status/contents

Tracing • Trace execution of a program: – location in the program – status/contents of variables • Tools: – programming environment • E. g. , “step”, “breakpoints”, “watch” – debugging statements • E. g. , output values of variables, markers at specific locations, etc 26

Example: Debugging Statements. . . for (i=0; i<N; i++) { scanf(“%s”, name); } 27

Example: Debugging Statements. . . for (i=0; i<N; i++) { scanf(“%s”, name); } 27

Example: Debugging Statements (cont) const int debugging = 1; . . . for (i=0;

Example: Debugging Statements (cont) const int debugging = 1; . . . for (i=0; i<N; i++) { scanf(“%s”, name); } if (debugging) { printf("for: i=%d, name=%sn", i, name); } TIP: make debugging statements conditional on a boolean variable 28

Example: Debugging Statements (alternative) #define DEBUG 1 int main(). . . for (i=0; i<N;

Example: Debugging Statements (alternative) #define DEBUG 1 int main(). . . for (i=0; i<N; i++) { scanf(“%s”, name); } #if DEBUG printf("for: i=%d, name=%sn", i, name); #endif 29

Testing Techniques • Test data set should “fully” test the program • All logical

Testing Techniques • Test data set should “fully” test the program • All logical paths of the program should be traversed (i. e. , every line of code should be executed at least once) • Use the design represented by the flowchart TIP: build your programs incrementally, testing small components as you go along 30

Example: Best. Mark Problem: • Write a program which reads a list of marks,

Example: Best. Mark Problem: • Write a program which reads a list of marks, and prints out the best mark • Example: – Input: 18 56 65 96 24 – Output: Best mark is 96 30 31

Example: Best. Mark (cont) Algorithm set best. Mark to 0 loop { input mark

Example: Best. Mark (cont) Algorithm set best. Mark to 0 loop { input mark if (end of input) then exit loop } output “Best mark is “, best. Mark 32

Example: Best. Mark (cont) Algorithm set best. Mark to 0 loop { input mark

Example: Best. Mark (cont) Algorithm set best. Mark to 0 loop { input mark if (end of input) then exit loop if (mark > best. Mark) then { set best. Mark to mark } } output “Best mark is “, best. Mark 33

How do I validate the input? Example: Best. Mark (cont) Algorithm set best. Mark

How do I validate the input? Example: Best. Mark (cont) Algorithm set best. Mark to 0 loop { input mark if (end of input) then exit loop if (mark > best. Mark) then { set best. Mark to mark } } output “Best mark is “, best. Mark 34

Classes of Test Data • • Valid data Valid boundary data Special or unusual

Classes of Test Data • • Valid data Valid boundary data Special or unusual cases Invalid data 35

Test Data: Valid Data • Reasonable data for the problem • Example: Best. Mark

Test Data: Valid Data • Reasonable data for the problem • Example: Best. Mark – What is the test out of? – If mark is out of 100, valid test data is 75, 65, 55 36

Test Data: Valid Boundary Data • Data with extreme values – Example: Best. Mark

Test Data: Valid Boundary Data • Data with extreme values – Example: Best. Mark • minimum of 0 • maximum of 100 • Test selection conditions • Test iteration exit conditions • Test first and last elements of an array 37

Test Data: Special Cases • Example: Best. Mark – What if someone is absent

Test Data: Special Cases • Example: Best. Mark – What if someone is absent or the mark is withheld (special consideration)? input mark. Entered if (mark. Entered is “Abs” or “WH”) { output “No mark for this student” set mark to 0 } else { set mark to numerical value of mark. Entered } 38

Test Data: Invalid Data • Invalid data is – of an incorrect type, or

Test Data: Invalid Data • Invalid data is – of an incorrect type, or – outside the expected range • Use features of the programming language to ensure correct data type – Example: Best. Mark • mark can be restricted to an integer int mark; scanf(“%d”, &mark); 39

Test Data: Invalid Data (cont) input mark. Entered. . . set mark to numerical

Test Data: Invalid Data (cont) input mark. Entered. . . set mark to numerical value of mark. Entered if (cannot get number from mark. Entered) { output “Invalid input” }. . . if ((mark < 0) or (mark > 100)) { output “Mark has to be between 0 and 100” } 40

Algorithm: Best. Mark set best. Mark to 0 loop { input mark. Entered if

Algorithm: Best. Mark set best. Mark to 0 loop { input mark. Entered if (end of input) break loop if ( mark. Entered is “Abs” or “WH” ) { output “No mark for this student” } else { set mark to numerical value of mark. Entered if (cannot get number from mark. Entered) { output “Invalid input” } else if ((mark < 0) or (mark > 100)) { output “Mark has to be between 0 and 100” } else /* valid input! */ { if (mark > best. Mark) set best. Mark to mark } } } output “Best mark is “, best. Mark best 1 41

Example int x, y; scanf("%d %d", &x, &y); if (x > 2) { while

Example int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y NO YES x>2? NO x>y? NO YES x<y? YES output S 1 decrement x output S 3 output S 2 output S 4 42

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) {

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); YES Input x, y x>2? NO 43

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) {

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y YES NO x>2? x>y? YES output S 1 decrement x 44

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) {

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y YES x>y? NO x>2? NO YES x<y? YES output S 1 output S 3 decrement x output S 4 45

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) {

Example (cont) int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y NO YES x>2? NO x>y? NO YES x<y? YES output S 1 decrement x output S 3 output S 2 output S 4 46

Example (cont): Valid Data • Which lines of code indicate what is valid data?

Example (cont): Valid Data • Which lines of code indicate what is valid data? int x, y; scanf("%d %dn", &x, &y); • Valid data is any integer: –positive, –negative, or –zero 47

Example (cont): Test data for all logical paths • What is done for every

Example (cont): Test data for all logical paths • What is done for every input? • What does this say about the output? – S 4 must be output at the end every time int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); 48

Example (cont): Test data for all logical paths Input x, y YES x>2? x>y?

Example (cont): Test data for all logical paths Input x, y YES x>2? x>y? NO NO x<y? YES output S 1 decrement x NO output S 3 output S 2 output S 4 int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); TO DO: Test all paths from “Input” to “Output S 4” 49

Example (cont): Choice Points Input x, y YES x>2? x>y? NO NO x<y? YES

Example (cont): Choice Points Input x, y YES x>2? x>y? NO NO x<y? YES output S 1 decrement x NO output S 3 Paths are determined by choice points output S 2 output S 4 50

Example (cont): Choice Points • What are the highest level choice points? int x,

Example (cont): Choice Points • What are the highest level choice points? int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); 51

Example (cont): Choice Points int x, y; scanf("%d %d", &x, &y); if (x >

Example (cont): Choice Points int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y YES x>2? x>y? NO NO NO x<y? YES output S 1 output S 3 decrement x output S 2 output S 4 52

Example (cont): Choice Points Input x, y Test data Case 1: NOT (x>2), NOT

Example (cont): Choice Points Input x, y Test data Case 1: NOT (x>2), NOT (x<y) Specific Values: x==2, y == 2 Output: S 4 YES x>2? x>y? NO NO x<y? YES output S 1 decrement x NO output S 3 output S 2 output S 4 53

Example (cont): Choice Points Input x, y Test data Case 2: NOT (x>2), x<y

Example (cont): Choice Points Input x, y Test data Case 2: NOT (x>2), x<y Specific Values: x==2, y == 3 Output: S 3, S 4 YES x>2? x>y? NO NO NO x<y? YES output S 1 output S 3 decrement x output S 2 output S 4 54

Example (cont): Choice Points int x, y; scanf("%d %d", &x, &y); if (x >

Example (cont): Choice Points int x, y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S 1, "); x--; } printf("S 2, "); } else if (x < y) { printf("S 3, "); } printf("S 4"); Input x, y YES x>2? x>y? NO NO x<y? YES output S 1 decrement x NO output S 3 output S 2 output S 4 55

Example (cont): Choice Points Input x, y Test data Case 3 (Loop body not

Example (cont): Choice Points Input x, y Test data Case 3 (Loop body not executed): x > 2, NOT(x > y) Specific Values: x==3, y == 4 YES x>2? x>y? NO NO x<y? YES output S 1 Output: S 2, S 4 NO output S 3 decrement x output S 2 output S 4 56

Example (cont): Choice Points Input x, y Test data Case 4 (Loop body executed):

Example (cont): Choice Points Input x, y Test data Case 4 (Loop body executed): x > 2, x > y Specific Values: x==5, y == 4 YES x>2? x>y? NO NO x<y? YES output S 1 Output: S 1, S 2, S 4 NO output S 3 decrement x output S 2 output S 4 57

Notes on Loop Tests • Is it possible that a loop never terminates? –

Notes on Loop Tests • Is it possible that a loop never terminates? – only if the algorithm is incorrect • Example: while (x > y) { printf("S 1, "); x++; } while (x > y) { printf("S 1, "); x--; } 58

Exercise: Changing the Algorithm /* Step 1 */ while (x > 0) { /*

Exercise: Changing the Algorithm /* Step 1 */ while (x > 0) { /* Step 2 */ if (y == 2) { /* Step 2 a */ } else { /* Step 2 b */ } /* Step 3 */ } /* Step 4 */ • Provide a set of test data: – valid boundary – invalid • How would you ensure that the loop always terminates? 59

Summary • Testing is an important part of the software development process • Considering

Summary • Testing is an important part of the software development process • Considering all the test data cases can lead to a change in the algorithm • Flowcharts can be used to design the test data set 60