1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
CIS Computer Programming Logic
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 5: Control Structures II (Repetition)
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
C Lecture Notes 1 Structured Program Development.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CS101 Computer Programming I Chapter 4 Extra Examples.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
The Hashemite University Computer Engineering Department
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
SOFTWARE TESTING LECTURE 9. OBSERVATIONS ABOUT TESTING “ Testing is the process of executing a program with the intention of finding errors. ” – Myers.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Software Testing.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 2 Introduction to Programming
Numbering System TODAY AND TOMORROW 11th Edition
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Software Testing (Lecture 11-a)
How to develop a program?
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
Chapter 2- Visual Basic Schneider
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
CHAPTER 6 Testing and Debugging.
Chapter 15 Debugging.
Presentation transcript:

1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging

2 Topics The Software Development Cycle Flow Diagrams –Selection –Sequence –Iteration How to diagnose errors in your program? –Methods of debugging –Methods of testing Test Data - Valid data, invalid data, boundary data and special cases

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

4 Development Cycle AnalysisDesignImplementTest debugging and testing

5 Flow Diagrams Represent flow of control of algorithms: –sequences –selection –iteration Useful for: –Finding semantic errors –Determining test data set

6 Sequence 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

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

8 Step A: input number Step B: if number is negative, then add -1 to number else add 1 to number Step C: output number Example 2: an algorithm involving “selection” Step A: input number Step C: output number Selection

9 input number if number is negative, then add -1 to number else add 1 to number output number input number is number negative? add -1 to number YES NO add 1 to number output number Flow Diagram: Selection Arrow labeled with result of condition test Condition test in diamond Also called “choice points”

10 Flow Diagram: Iteration (while loop) while ( condition C1 ) { } C1 true? S1 YES NO

11 Flow Diagram: Iteration (for loop) for ( init ; condition C1 ; increment ) { } C1 true? S1 YES NO init increment

12 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

13 Example: Code to Flow Diagram (Spot the error!) for ( i=0; i < 10; i++ ) { scanf(“%d\n”, &x); if ( x < 0 ) { break; } is i < 10 ? input value for x YES NO set i to 0 increment i is x < 0 ? NOYES

14 Example: Code to Flow Diagram (Spot the error!) for ( i=0; i < 10; i++ ) { scanf(“%d\n”, &x); if ( x < 0 ) { break; } is i < 10 ? input value for x YES NO set i to 0 increment i is x < 0 ? NOYES Should come before increment step

15 Example: Code to Flow Diagram (correct version) for ( i=0; i < 10; i++ ) { scanf(“%d\n”, &x); if ( x < 0 ) { break; } is i < 10 ? input value for x YES NO set i to 0 increment i is x < 0 ? NO YES

16 Algorithm to Flow Diagram Example: AddNumbers input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count input value for totalNumbers set sum to 0 set count to 0

17 is count< totalNumbers? Example: AddNumbers (cont) input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count input value for nextNum YES NO increment count add nextNum to sum

18 Example: AddNumbers (cont) input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count output value for sum output value for sum/count

19 What happens if totalNumbers is 0? Modify the algorithm to output the mean only when count is positive. Exercise: AddNumbers (cont) input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count

20 Use of Flow Diagrams Pseudo-code  flow diagram Flow diagram  code Flow Diagrams are useful to show: -the order of steps in a loop -the order of selection -semantic errors in programs and algorithms

21 Debugging and Testing Debugging: the process of finding and correcting errors (a.k.a “bugs”) –Tracing through the program –Checking for correct values of variables –Finding syntactic and semantic errors Testing: executing the program on a test data set

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

23 Debugging Basics Know the (C) language well – watch out for common errors printf(“%d\n”, &num); Examples: scanf(“%s”, &name); if (i < N); { scanf(“%d\n”, &i); } float x, y, z = 3.5; scanf(“%f”, x);

24 Pay attention to compiler error and warning messages Examples: if (ch = ‘Q’) { break; } Debugging Basics (cont) “Possible incorrect assignment” int N; … scanf(“%d”, N); “Possible use of ‘N’ before declaration”

25 Debugging: Tracing Trace the execution of a program: –location in the program –status/contents of variables Tools: –within the programming environment E.g,. “step”, “breakpoints”, “watch” –debugging statements in the code E.g., print the 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 (cont) TIP: make debugging statements conditional on a boolean variable const int debugging = 1;... for (i=0; i<N; i++) { scanf(“%s”, name); if (debugging) { printf("for: i=%d, name=%s\n", i, name); }

28 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=%s\n", i, name); #endif }

29 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 flow diagram TIP: build your programs incrementally, testing small components as you go along

30 Example: BestMark Problem: Write a program which reads a list of marks, and prints out the best mark Example: –Input: –Output: Best mark is 96.

31 Example: BestMark (cont) set bestMark to 0 loop { input mark if (end of input) then exit loop } output “Best mark is “, bestMark Algorithm

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

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

34 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: BestMark –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: BestMark –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: BestMark –What if someone is absent or the mark is withheld (special consideration)? input markEntered if (markEntered is “Abs” or “WH”) { output “No mark for this student” set mark to 0 } else { set mark to numerical value of markEntered }

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

39 Test Data: Invalid Data (cont)... if ((mark 100)) { output “Mark has to be between 0 and 100” } input markEntered... set mark to numerical value of markEntered if (markEntered is not an integer) { output “Invalid input” }

40 loop { input markEntered if (end of input) break loop if ( markEntered is “Abs” or “WH” ) { output “No mark for this student” } else { set mark to numerical value of markEntered if (markEntered is not an integer) { output “Invalid input” } else if ((mark 100)) { output “Mark has to be between 0 and 100” } else /* valid input! */ { if (mark > bestMark) set bestMark to mark } output “Best mark is “, bestMark Example: BestMark best1.c

41 Example int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x

42 Example (cont) int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); YESNO x>2? Input x,y

43 int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); else if (x < y) { printf("S3,"); } printf("S4"); YES NO x>2? Input x,y YES x>y? output S1 decrement x Example (cont)

44 int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); YES NO x>2? Input x,y x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x Example (cont)

45 int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x Example (cont)

46 Example (cont): Valid Data Which lines of code indicate what is valid data? int x,y; scanf("%d %d\n", &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 input? int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); What does this say about the output? – S4 must be output at the end every time

48 int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); Example (cont): Test data for all logical paths YES NO x>2? Input x,y NO output S2 x<y? output S4 output S3 NO YES x>y? output S1 decrement x

49 Example (cont): Choice Points YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x Paths are determined by choice points or conditions Test all paths from “Input” to “Output S4”

50 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("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4");

51 Example (cont): Choice Points YES NO x>2? Input x,y NO output S2 x<y? output S4 output S3 NO YES x>y? output S1 decrement x int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4");

52 Example (cont): Choice Points Test data Case 1: NOT (x>2), NOT (x<y) Output: S4 Specific Values: x==2, y == 2 YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x

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

54 Example (cont): Choice Points int x,y; scanf("%d %d", &x, &y); if (x > 2) { while (x > y) { printf("S1,"); x--; } printf("S2,"); } else if (x < y) { printf("S3,"); } printf("S4"); YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x

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

56 Example (cont): Choice Points Test data Case 4 (Loop body executed): x > 2, x > y Output: S1, S2, S4 Specific Values: x==5, y == 4 YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x

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

58 Testing: Summary Provide a set of test data which tests: –valid data –valid boundary data –invalid data Test all paths through the algorithm Ensure that loops always terminate Flow charts are useful in designing test data

59 Summary Flow diagrams can be used to design the program and the test data set When debugging look out for syntactic and semantic errors Testing is an important part of the software development process Considering all the test data cases can lead to a change in the algorithm