Sunteți pe pagina 1din 26

Chapter 2

Program Development

2.1 Program Development Process


2.2 Problem Definition
2.3 Problem Analysis
2.4 Program Design
2.5 Implementation
2.6 Program Testing
2.7 Documentation

1
Solving Real-World Problems

2
2.1 Program Development
Process
The program development
process generally go
through 6 phases:
1) Problem Definition
2) Problem Analysis
3) Program Design
4) Implementation
5) Program Testing
6) Documentation

3
2.2 Problem Definition
This is a broad statement of the user
requirements, in user terms. It sets
the program goals
the program bounds

Case Study
Design a program to solve the following
problem:
Write a program to read the radius of a
circle, and then compute and output the
circles area and circumference.
4
2.3 Problem Analysis
This phase produces a set of clear statements about
the way the program is to work. These statements
define:
how the user uses the program (program input)
what output the program will generate
the functionalities (or formulas) of the program
The context diagram is a complete representation of
the program. The context diagram for the case study is:
Program

5
Problem Analysis for Case Study
Purpose: Write a program to read the radius of
a circle, and then compute and output the
circles area and circumference.
Required inputs:
(1) the radius of a circle
Required output:
(1) the area of the circle
(2) the circumference of the circle
Formulas:
(1) Area of a circle = * radius * radius
(2) Circumference of a circle = 2 * * radius

6
2.4 Program Design
Aim: design the logic of the program or
algorithm.
Analyse the problem
Design the program logic --- verified
against system specification
Many techniques may be used - e.g.
flowchart or pseudocode

7
Program Design Approach

8
Top-down Stepwise Refinement
Technique
It starts from the top level: what the
program is supposed to do.
Through a number of refinement steps
we obtain the solution, i.e. the
sequence of C statements which is the
program.
In each step, a task is broken down to a
number of smaller and simpler
subtasks.

9
Algorithm
Using informal English to describe the
logic of the program.
Designing algorithm
must be unambiguous
every step must be clear and precise
specify the order of steps precisely
consider all possible points of decision
must execute in steps
terminate in finite time
Can be represented using
Pseudocode
Flowcharts
10
Flowcharts
This is a way to represent the logic or solution
method of a program by diagram. The flow of the
control can be easily visualized.

11
Pseudocode
This is another way to represent the logic
or solution method of a program.
no strict rules
informal language - mix of English and
keywords
common keywords: begin, end, if, else,
else_if, while, do, end_while, end_do
other keywords: read, print, set,
initialize, compute, add, subtract, etc.

12
Control Structures
Three basic structures: sequence, branching
and looping (will be discussed in Chap 6 & 7)
Sequence Structure

13
Selection (Branching) Structure

14
Repetition (Looping) Structure

15
Program Design for Case Study
Initial Algorithm:
1. Read the circle radius.
2. Perform the computation.
2.1 Calculate the area of the circle.
2.2 Calculate the circumference of the circle.
3. Print the area and the circumference of the circle.
Refinements:
Step 2.1
2.1.1 Assign PI*radius*radius to area.
Step 2.2
2.2.1 Assign 2*PI*radius to circumference.

16
Program Design for Case Study
Pseudocode
Algorithm: computeCircle
begin
Set constant PI = 3.14159
Print message "Enter the radius:"
Read radius
area radius * radius * PI
circumference 2 * radius * PI
Print area and circumference
end

17
2.5 Implementation
#include <stdio.h>
#define PI 3.14159
int main(void)
{
float radius, area, circumference;
printf("Enter the radius: \n"); /* get input */
scanf("%f", &radius);
area = PI * radius * radius; /* computation */
circumference = 2 * PI * radius;
printf("area = %f\n", area); /* print output */
printf("circumference = %f\n", circumference);
return 0;
}

18
Programming Errors
Syntax Errors
grammatical errors
detected by compiler
found automatically
need to be fixed before running the code
error message may be misleading
Run-time Errors
execution error (e.g. divide by zero)
detected during the execution of program
error messages may be useful
sometimes not easy to fix

19
Programming Errors
Logic Errors
due to error in designing the algorithm or
implementation
no compilation errors, no run-time error
message
difficult to detect
program debugging
Program tracing
using printf()
Isolation of code
Hand simulation

20
2.6 Program Testing
A program is correct if it returns the correct
result for every possible combination of input
values.
Exhaustive testing: use all possible
combinations of input values and check the
output is correct. This will take a whole year or
forever to show the program is correct.
-> Impractical.
What we can do: use test data that causes
every program path to be executed at least
once. For example,

21
During testing, pay particular attention to the
actions the program takes in special cases.

22
Testing for the Case Study
$ ./circle
Enter the radius:
5.0
Area = 78.539749
Circumference = 31.415899
$ ./circle
Enter the radius:
10.0
Area = 314.158997
Circumference = 62.831799

23
2.7 Documentation
problem definition and specification;
program inputs, outputs, constraints
and mathematical equations;
flowchart or pseudocode for the
algorithm;
source program listing;
sample test run of the program; and
user manual for end users.
24
Programming Style
The aim is to make programs simple,
readable and easy to understand. Some
suggestions are
use indentation, blank spaces, blank
lines.
use meaningful names for variables,
constants.

25
never put two or more statements on the
same line.
add comments into the program.
at the beginning of the program, put
comments to say what the program will do;
your name and date. Subsequently when the
program is modified/extended, put comments
to record the modifications, who made it, and
the date of the modifications.
If the purpose of a portion of the program is
not very clear at a glance, e.g. a nested loop
or nested if statement, put comments to say
what the program is doing.

26

S-ar putea să vă placă și