Title: Wavelet transform And Its Applications to Image Processing
1Writing a Good Program 4. Basic Software
Engineering 3 October 2007
24.1 Design a Structured Program
3- Seldom do we have a program worked the first time
it is written - Need debugging
- ? To find out and correct the errors in a
program - May need many times of iteration for a large
program - To reduce the number of iteration, need a
structural method for program development.
4- Debugging a big program is exponentially more
difficult than a small program. - If a big program is to be developed, always try
to break it down to many small programs and debug
them independently. - ? Divide and Conquer
Difficulty in debugging
Program size
5- It is a very bad habit to write a program in
main() only - since it makes the program very big.
- Always break it down by calling functions from
main().
?
?
int main() int function1()
int function2()
int main()
6- Usually main() is only used to indicate the
program flow. The actual task is done by the
functions called by main(). - By looking at the sequence of functions called by
main(), people basically understand what is going
on in a program. - It gives the skeleton of a program.
7- In the example program below, people expect this
program will repeatedly show a menu. - If interested in the details of the menu, he can
further read the function menu().
include ltiostreamgt using namespace std bool
menu() //function prototype int main() bool
exitflag false //flag is bool while
(exitflag false) exitflag menu()
return 0
8Improve the Readability of Programs
- In practice, the time a programmer spends in
documentation is more than writing the program. - A real program is never developed by a single
person, co-operations are needed between
programmers or even teams of programmers. - Besides, a program developed by a team of
programmers often needs to be maintained by
another team. - It is essential that a program is properly
documented to allow team work.
9Documenting a Program - readme
- After developing a program, a readme file is
often prepared in practice to indicate the
following - Background information of the program (e.g.
objectives, version no., development date,
developer name) - How to use the program (e.g. command line
options) - Additional resource required (e.g. hardware,
driver, etc.) - Bug fixed as compared to previous version(s)
- Possible conflict with other programs.
10Within a program - comments
- Commenting a program is the responsibility of
every programmer. - Need meaningful comments.
- ? Not explaining something people know already,
but something people will have difficulty to
understand.
11Totally meaningless comment!
// The main function int main() cout ltlt
"Hello World!\n" // cout Hello World return
0 // return a number 0
12Tell something hidden in the program!
// It is to demonstrate the basic structure of a
C // program // Usage HelloWorld int
main() cout ltlt "Hello World!\n" // print
the string Hello World! on screen return 0
// Indicate to the system the execution is //
successful
See the lines for comments are more than the
codes
13How to comment?
- At the beginning of each program, the following
comments are often required - Background information of the program
- (Objectives, version no., development date,
developer name, etc.) - Usage (e.g. command line options)
- Additional resource required (e.g. if any other
projects/files are required for compiling this
program, e.g. header files)
14How to comment?
- At the beginning of each function, the following
information is needed - Objective of this function
- Other functions that it will call
- Passed parameters
- Return parameters
- Global variables that have been made use of.
15How to comment?
- Inside a function, the following information is
needed - The use of every local variable
- Explanation of any tricky part that other people
may have difficulty in understanding.
16Case study Developing a program step-by-step
- Write a program that repeatedly asks the user to
select one of the following three options - 1. On choosing a, ask the user to input a
series of positive numbers and show the mean of
them - 2. On choosing b, ask the user to input a
series of positive numbers and show the variance
of them - 3. On choosing q, quit the program.
17Step 0 Prepare a flowchart
Start
D
Ask user to choose a, b or q
Yes
- To better visualize the problem, we may develop a
flowchart for it
User chooses a?
A
No
Yes
User chooses b?
B
No
No
User chooses q?
C
Skeleton
Menu
Yes
End
18D
A
Input integers and calculate the mean
B
Input integers and calculate the variance
C
Generate error message
19// This program is to compute the mean or
variance // of a series of positive numbers
input by user // Usage Mean_Var // Version 1 //
Date Feb 14, 2006 // Author Frank include
ltiostreamgt using namespace std bool menu()//
Show a menu to user and ask for input int
main() bool exitflag false // A flag to
indicate if user wants to quit while (exitflag
false) exitflag menu() // If user
chooses 'q', menu() returns true return 0
Step 1 construct the skeleton
Flow Chart
20- Step 2 Add stubs (i.e. just for testing)
- The above program cannot be compiled and executed
since the required function menu() has not be
implemented - We need to ensure the skeleton is correct before
we proceed to implement the functions add stubs
Just enough for the program skeleton to compile
and execute
// A stub // Input parameter Nil // Return
parameter Just a constant bool menu() return
true //Loop 1 time
21// This program is to compute the mean and
variance // of a series of positive numbers
input by user // include ltiostreamgt using
namespace std bool menu()// Show a menu to user
and ask for input int main() return
0 // A stub // Input parameter Nil // Return
parameter Just a constant bool menu() return
true
So the whole program at this stage is just like
that
22- Step 3 Implement Functions
- If the skeleton has been proved to be correct,
start to implement functions. - The original comments may need to be updated when
any change is made to the original codes as a
result of the implementation of functions. - Similar to developing main(), it is desirable to
further break a big function into smaller
functions. - Add stubs again when needed.
23// Show a menu to user and ask for input //
Input Nil // Return true if user chooses 'q',
otherwise false void average(char) // Ask for a
series of numbers and // compute their
mean or variance bool menu() char choice
// To store the command of user coutltlt"a
Mean\n"ltlt"b Variance\n"ltlt"q Quit\n" cout
ltlt "Please enter your choice " cin gtgt
choice switch (choice) case 'a'
average('a') break case 'b' average('b')
break case 'q' return true // Shall quit
menu() here default cout ltlt "Wrong input!\n"
break return false
Flow Chart
24- menu() cannot be compiled or executed since
average() has not been implemented - To ensure menu() is correct, we need to compile
and execute it. - To solve the problem, a stub is added for
average()
// A stub // Input parameter char command //
if 'a', do mean if 'b', do
variance // Return parameter Nil void
average(char command)
25- Step 4 Implement the sub-functions
- If the major functions have been proved to be
correct, start to implement sub-functions - The original comments may need to be updated when
any change is made to the original codes as a
result of the implementation of functions. - Similar to developing menu(), it is desirable to
further break a big function into smaller
functions. - Add stubs again when needed.
- It is desirable to have each function contained
at most 20 lines of codes.
average().
Divide and conquer
26// Get positive nos., find mean or variance //
Input parameter char command - // 'a'-gt mean
'b'-gt variance // Return parameter Nil void
average(char command) double input 0//
Store data input by user double sum 0 //
Store temporary sum int count 0 // Count
no. of data entered while (input gt 0) // If
negative no., quit cout ltlt "Please enter
your data " cin gtgt input if (input lt 0)
continue // Leave average() on -ve
input count1 if (command 'a') //
calculate and show the current mean sum sum
input coutltlt"\n\nThe current mean is
"ltltsum/countltltendl else // calculate
and show the current variance sum sum
inputinput coutltlt"\n\nThe current variance
is "ltltsum/countltltendl // it is in fact not
the real variance
27Exercise 4.1
By following the steps as described above, you
are requested to develop the skeleton of a
personnel database program. The program should be
a console application that repeatedly asks users
to do one of the following in the main menu a.
Enter/modify record b. Show all records q. Quit
28- For every user command input, call a function.
You dont need to implement the details of the
functions, just do the following - If user chooses a, show a message Please enter
record call a function and get back to the main
menu. - If user chooses b, show a message All records
are shown below call a function and get back to
the main menu. - If user chooses q, show a message Thank you.
Goodbye!!! and then the program will quit. - Make sure your program follows the style as shown
in the case study (Do NOT use main() to do
everything) - Make sure your program is commented appropriately
using the method as described above.
29Acknowledgment
- The slides are based on the set developed by Dr.
Frank Leung (EIE).