Program Design and
Algorithm
Development
Katja Fennel
Lab 5
Objective
Program Design and Algorithm Structure plan
Design goals
Development Design process
Examples
Temperature conversion
Marine Modelling February 12, 2010 Guessing Game
Katja Fennel
Oceanography
Dalhousie University
5.1
Program Design and
Learning objective: Algorithm
Development
Katja Fennel
Objective
Structure plan
Design goals
Design process
Examples
Temperature conversion
Introduce the steps of program design and the concept of Guessing Game
structure plan (pseudocode) as helpful means of designing a
program
5.2
Program Design and
Structure Plan: Algorithm
Development
Katja Fennel
Objective
Structure plan
Design goals
• helpful strategy for how to think about the development of Design process
a code that will solve your problem (applicable to any Examples
Temperature conversion
software and even problem-solving in general) Guessing Game
• in order to write a successful code you need to understand
the problem thoroughly first, and then break it down into its
most fundamental logical units
• sequence of fundamental logical units: systematic
procedure or algorithm
5.3
Program Design and
Design goals: Algorithm
Development
Katja Fennel
Objective
Structure plan
Design goals
• software that works Design process
• can easily be read and understood Examples
Temperature conversion
• can be systematically modified when necessary Guessing Game
• must satisfy the requirements associated with the class of
problems to be solved
Note: The specifications (purpose/function, inputs, method of
processing, outputs) must be known before design of code can
begin.
5.4
Program Design and
Design Process: Algorithm
Development
Katja Fennel
Objective
• step 1: problem analysis (establish context, understand
Structure plan
nature of problem, develop detailed statement of
Design goals
mathematical problem to be solved) Design process
Examples
Temperature conversion
Guessing Game
5.5
Program Design and
Design Process: Algorithm
Development
Katja Fennel
Objective
• step 1: problem analysis (establish context, understand
Structure plan
nature of problem, develop detailed statement of
Design goals
mathematical problem to be solved) Design process
• step 2: processing scheme (define inputs and outputs) Examples
Temperature conversion
Guessing Game
5.5
Program Design and
Design Process: Algorithm
Development
Katja Fennel
Objective
• step 1: problem analysis (establish context, understand
Structure plan
nature of problem, develop detailed statement of
Design goals
mathematical problem to be solved) Design process
• step 2: processing scheme (define inputs and outputs) Examples
Temperature conversion
• step 3: develop algorithm (step-by-step procedure that Guessing Game
decomposes overall problem into subproblems; strategy:
divide and conquer; itemized list of the subtasks: structure
plan, written in pseudocode [a combination of English,
math and anticipated MATLAB commands])
5.5
Program Design and
Design Process: Algorithm
Development
Katja Fennel
Objective
• step 1: problem analysis (establish context, understand
Structure plan
nature of problem, develop detailed statement of
Design goals
mathematical problem to be solved) Design process
• step 2: processing scheme (define inputs and outputs) Examples
Temperature conversion
• step 3: develop algorithm (step-by-step procedure that Guessing Game
decomposes overall problem into subproblems; strategy:
divide and conquer; itemized list of the subtasks: structure
plan, written in pseudocode [a combination of English,
math and anticipated MATLAB commands])
• step 4: write the algorithm (translate pseudocode into
syntactically correct MATLAB code)
5.5
Program Design and
Design Process: Algorithm
Development
Katja Fennel
Objective
• step 1: problem analysis (establish context, understand
Structure plan
nature of problem, develop detailed statement of
Design goals
mathematical problem to be solved) Design process
• step 2: processing scheme (define inputs and outputs) Examples
Temperature conversion
• step 3: develop algorithm (step-by-step procedure that Guessing Game
decomposes overall problem into subproblems; strategy:
divide and conquer; itemized list of the subtasks: structure
plan, written in pseudocode [a combination of English,
math and anticipated MATLAB commands])
• step 4: write the algorithm (translate pseudocode into
syntactically correct MATLAB code)
• step 5: evaluation (test all input option, validate by
comparing against known known outputs)
5.5
Program Design and
Examples of structure plans: Algorithm
Development
Katja Fennel
A script that converts temperatures given on the Fahrenheit Objective
scale to Celsius scale: Structure plan
Design goals
1 Initialize temperature in Fahrenheit Design process
2 Calculate and display Celsius temperature Examples
Temperature conversion
3 Stop Guessing Game
5.6
Program Design and
Examples of structure plans: Algorithm
Development
Katja Fennel
A script that converts temperatures given on the Fahrenheit Objective
scale to Celsius scale: Structure plan
Design goals
1 Initialize temperature in Fahrenheit Design process
2 Calculate and display Celsius temperature Examples
Temperature conversion
3 Stop Guessing Game
Step 1 is straightforward, but step 2 requires elaboration:
1 Initialize temperature in Fahrenheit (F )
2 Calculate and display Celsius temperature (C)
1 Subtract 32 from F and multiply by 5/9
3 Display C
4 Stop
5.6
Program Design and
Here is my version: Algorithm
Development
Katja Fennel
convert_F2C.m :
% This script converts temp.s from the Fahrenheit Objective
Structure plan
% to the Celsius scale.
Design goals
Design process
% 1. Initialize temperature in Fahrenheit (F) Examples
% use "input" command to receive input from the Temperature conversion
Guessing Game
% command line
F = input(’Enter temperature in degrees F: ’);
% 2. Calculate and display Celsius temperature (C)
% 2.1. Subtract 32 from F and multiply by 5/9
C = (F-32)*5/9;
% 3. Display C
% use "disp" command to display the result
disp([’This temperature corresponds to ’ num2str(C) ...
’ degrees C.’])
% 4. Stop
Test for F = 32(C = 0) and F = 212(C = 100).
5.7
Program Design and
Another example: Guessing game Algorithm
Development
Katja Fennel
Objective
MATLAB thinks of an integer between 1 and 10 (it generates Structure plan
this number at random). We need to guess it. If our guess is Design goals
too high or too low, MATLAB will tell us. When we are right Design process
MATLAB will display a congratulation message. Examples
Temperature conversion
Guessing Game
5.8
Program Design and
Another example: Guessing game Algorithm
Development
Katja Fennel
Objective
MATLAB thinks of an integer between 1 and 10 (it generates Structure plan
this number at random). We need to guess it. If our guess is Design goals
too high or too low, MATLAB will tell us. When we are right Design process
MATLAB will display a congratulation message. Examples
Temperature conversion
Guessing Game
The while command (hybrid between if and for):
while expression
statements
end
Statements are repeated for as long as expression is true.
Typically one component of the expression is modified by the
statements.
5.8
Program Design and
Algorithm
Development
Katja Fennel
Objective
Structure Plan: Structure plan
Design goals
Design process
Examples
Temperature conversion
Guessing Game
5.9
Program Design and
Algorithm
Development
Katja Fennel
Objective
Structure Plan: Structure plan
1 Generate random number Design goals
Design process
2 Ask for our guess
Examples
3 While the guess is wrong: Temperature conversion
Guessing Game
1 If guess is too low, say so
2 If guess is too high (otherwise), say so
3 Ask for new guess
4 If we make it past 3. we must be right: congratulate
5 Stop
5.9
Program Design and
Algorithm
Development
Katja Fennel
Objective
Structure Plan: Structure plan
1 Generate random number Design goals
Design process
2 Ask for our guess
Examples
3 While the guess is wrong: Temperature conversion
Guessing Game
1 If guess is too low, say so
2 If guess is too high (otherwise), say so
3 Ask for new guess
4 If we make it past 3. we must be right: congratulate
5 Stop
Now write it!
5.9
Program Design and
Exercise: Algorithm
Development
Katja Fennel
Objective
Structure plan
Come up with the structure plan for a script that projects the Design goals
population of China forward in time for 8 decades (starting from Design process
1980). This will use 1980 population structure data and death Examples
Temperature conversion
rates form the handout. Guessing Game
1 Initialize age structure and age-specific death rates
2 Build transformation matrix
3 Calculate
4 Step forward in time
5 Output/plot results
5.10