Beruflich Dokumente
Kultur Dokumente
Topics
The Software Development Cycle
Flow Diagrams
Selection
Sequence
Iteration
Development Cycle
Analysis
Design
Implement
Test
debugging
and testing
4
Types of Errors
syntactic: how instructions are written
Example 1: while (i=0; i < 5; i++)
{
printf(%d\n, i);
}
printf(%d\n, i);
}
Example 3: if (choice = Q)
{
break;
}
Flow Diagrams
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 order
Example 1:
Step A: input number
Step B: add 1 to number
Step C: output number
Order of execution
indicated by arrows
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
an algorithm
involving
selection
Arrow labeled
with result of
condition test
YES
Step A
C1 true?
S1
Condition
test
in diamond
NO
S2
Step B
11
input number
NO
is number
negative?
add -1 to number
add 1 to number
output number
12
C1 true?
YES
S1
13
NO
S1
} while ( condition C1 ) ;
C1 true?
YES
14
NO
init
C1 true?
YES
S1
increment
15
NO
is i < 10 ?
NO
YES
input value for x
increment i
ERROR!
Should come before
increment step
NO
is x < 0 ?
17
YES
is i < 10 ?
NO
YES
input value for x
is x < 0 ? YES
NO
increment i
18
is count< totalNumbers?
YES
input value for nextNum
add nextNum to sum
increment count
20
NO
21
What if
totalNumbers
is zero?
22
23
24
Debugging Basics
Know the (C) language well
Examples:
float x, y, z = 3.5;
printf(%d\n, &num);
scanf(%f, x);
scanf(%s, &name);
if (i < N);
{
scanf(%d\n, &i);
}
25
scanf(%d, N);
26
Tracing
Trace execution of 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
27
28
30
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
31
Example: BestMark
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
32
}
output Best mark is , bestMark
33
}
output Best mark is , bestMark
34
How do I validate
the input?
Algorithm
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
35
Valid data
Valid boundary data
Special or unusual cases
Invalid data
36
37
38
39
...
if ((mark < 0) or (mark > 100))
{
output Mark has to be between 0 and 100
}
41
Algorithm: BestMark
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 (cannot get number from markEntered)
{ output Invalid input
}
else if ((mark < 0) or (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
42
best1.c
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");
Input
x,y
NO
YES
x>2?
NO
x>y?
NO
YES
YES
output S1
decrement x
x<y?
output S3
output S2
output S4
43
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");
YES
Input
x,y
x>2?
44
NO
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");
Input
x,y
YES
NO
x>2?
x>y?
YES
output S1
decrement x
45
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");
Input
x,y
YES
NO
x>2?
NO
x>y?
YES
x<y?
YES
output S1
output S3
decrement x
output S4
46
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");
Input
x,y
NO
YES
x>2?
NO
x>y?
NO
YES
YES
output S1
decrement x
x<y?
output S3
output S2
output S4
47
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");
49
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
decrement x
NO
output S3
output S2
output S4
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");
50
x>2?
x>y? NO
NO
NO x<y?
YES
YES
output S1
output S3
decrement x output S2
output S4
51
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
decrement x
NO
output S3
Paths are
determined by
choice points
output S2
output S4
52
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");
53
Input
x,y
YES
x>2?
x>y? NO
NO
NO x<y?
YES
YES
output S1
output S3
decrement x output S2
output S4
54
YES
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
decrement x
NO
output S3
output S2
output S4
55
YES
x>2?
x>y? NO
NO
NO x<y?
YES
YES
output S1
output S3
decrement x output S2
output S4
56
Input
x,y
YES
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
decrement x
NO
output S3
output S2
output S4
57
YES
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
Output: S2, S4
NO
output S3
decrement x output S2
output S4
58
YES
x>2?
x>y? NO
NO x<y?
YES
YES
output S1
NO
output S3
decrement x output S2
output S4
59
while (x > y)
{
printf("S1,");
x--;
}
60
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
Flow diagrams can be used to design the
test data set
62