CS 211 Lesson 10
Program Design
Quote:
"Hearing is one of the body's five senses. But listening is an art." Frank Tyger
Lesson Objectives:
Understand the steps in the program design process
Know what an algorithm is
Know what pseudocode is and how to use it
Understand top-down design and step-wise refinement
Lesson:
I. MATLAB Concepts
(none for this lesson)
II. Good Programming Practices
A. The Program Design Process
- There are 4 general steps to the "classic" program design process:
Understand the problem
Design a solution (Design inputs, design outputs, and design data processing algorithms)
Translate the solution into a program (programming, or sometimes called coding)
Test and debug the program
Understanding the problem is often the most challenging step.
Typically the customer who needs the software does not fully understand their own requirements.
For smaller problems, understanding the problem may not be very difficult, if you understand the problem domain. However, for large problems, understanding the problem can take much effort and much time.
Attempting to solve a problem you do not fully understand can lead to solving the wrong problem.
- it.
Translating a solution into a program (i.e., programming) typically is the easiest step for an experienced programmer.
However, novice programmers sometimes spend a lot of time encoding a solution to a problem and debugging
One software expert (Fred Brooks) estimates that on large projects, the amount of time required for these tasks are:
1/3 of the time on planning (understanding the problem and designing a solution)
1/6 of the time on programming
1/2 of the time on testing and debugging
As a general rule, the more time you spend on steps 1 and 2 (understanding the problem and design), the less time you should need to spend on programming and debugging.
Following these four steps, in order, is an ideal which is not always achievable.
Designing a software solution for small, well-defined problems is much easier than designing a software solution for the typical Department of Defense (DoD) problem. Developing skills in designing solutions for small software problems (software engineering "in the small") should help you deal with larger engineering design challenges later.
B. Designing a solution
An algorithm is a step-by-step plan for solving a problem.
An algorithm may be expressed in programming language code, or in a more informal English-like notation.
Pseudocode is an informal combination of English and code which expresses an algorithm.
The following is an example of an algorithm written in pseudocode:
set Secret_number to a random integer from 1 to 100
set User_guess to 0
repeat until User_guess = Secret_number
get a valid value for User_guess from the user
if User_guess > Secret_number
tell the user their guess is too high
otherwise, if User_guess < Secret_number
tell the user their guess is too low
otherwise
tell the user their guess is correct
thank the user for playing
Note that indenting pseudocode helps show the blocks of code which are executed, skipped, or repeated as units.
A pseudocode algorithm may leave out many of the details required by an actual MATLAB program.
Using pseudocode can help you focus on the solution design rather than syntactic and MATLAB-specific details.
You can test a well-written pseudocode algorithm with pencil and paper. This is sometimes called desk checking or performing a walk-thru.
It is common to include the major steps of your algorithm as comments in your program code.
One well-established strategy for designing software solutions is top-down design with step-wise refinement.
Top-down design involves developing a high-level plan for solving the whole problem, and then breaking that plan down into smaller subtasks which may be solved independently.
Step-wise refinement involves adding details to your solution, one subtask at a time.
Top-down design and step-wise refinement are particularly useful once you begin to develop larger programs which can be divided into independent parts, each implemented with one or more separate functions.
III. Algorithms
A. Sort a list of numbers in ascending order
The fastest, general-purpose sorting algorithm known is called the quick sort.
The quick sort, written in pseudocode, is performed with the following steps:
Select the first element in the list as a "pivot" value
Create two sub arrays from the remaining elements in the list:
one array containing all values less than or equal to the "pivot" value
one array containing all other values greater than the "pivot" value
quick sort both sub arrays
combine the two sorted arrays and the pivot element to create the sorted listThis pseudocode can be written in MATLAB in a variety of ways. One possible translation is shown below:
function sorted_list = quick_sort(list)
if isempty(list)
sorted_list = [];
else
less = [];
more = [];
pivot = list(1);
for j = 2:length(list)
if list(j) <= pivot
less(end+1) = list(j);
else
more(end+1) = list(j);
end
end
sorted_list = [ quick_sort(less), pivot, quick_sort(more) ];
end
end % quick_sortNotice that the algorithm, written in pseudocode, is totally concentrated on the problem solving task. In contrast, the MATLAB program must deal with the details of the programming language, such as testing for empty lists and properly initializing variables.
Performing problem solving and programming as distinct tasks is extremely beneficial to the overall software development process.
Lab Work: Lab 10
References: Chapman Textbook: sections 3.1-3.2