Académique Documents
Professionnel Documents
Culture Documents
1
Program Design Problem Solving Phase
Programming is a creative process Step 1: Understand the Problem
No complete set of rules for creating a program What is the input?
It takes lots of practice. What is the output?
What is the relationship between the input and
output? What formulas or techniques do we need?
Program Design Process
Problem Solving Phase
Step 2: Do a small example by hand
Result is an algorithm that solves the problem
Implementation Phase Step 3: Write an algorithm to solve the problem
Result is the algorithm translated into a programming Use pseudocode
language
2
Testing for Primality: Step 2 Better-value Pizza : Step 3
Step 2: Work examples by hand Step 3: Write an algoritm
Given a 10 inch costing 12 euro and a 12 inch costing Before looking at Step 3 let’s look more
15 euro
closely at the meaning of
Area of 10 inch pizza is … 78.5
Algorithm
Price per sq inch is 12/78.5 or 15 cents
12inch 113 sq inches; 15/113 = 13.26 cents per sq inch… pseudocode
Since ppsi of 12 inch is smaller it is better value
3
Knitting pattern Extract Algorithmic Primitives
We need representations that are detailed
enough to allow the algorithm to be
implemented.
Define primitives
Building blocks for constructing algorithmic
representations.
Example: Knitting instructions ..
(k2, p2) twice
5 Program Development 13 5 Program Development 14
4
Pseudocode (2) Repetition Pseudocode (3) Abstraction
LOOP WHILE condition The preceding primitives are enough to
DO activity specify all algorithms.
WHILE there are more names in the hat
DO draw a name and print it
However we will also use procedural
abstraction to create pseudocode
ITERATE
FOR EACH of a set of items
algorithms.
DO activity Abstraction means identifying and
FOR EACH integer between 1 and 100 naming an algorithm and then using that
DO print the number squared name to carry out that algorithm.
5
Writing pseudocode Better-value Pizza: Step 3
Pseudocode is informal and can be 1. Read in the diam and price of the first pizza.
adapted to fit your needs 2. ppsi1 <- the price per sq. inch of the first pizza
3. Read in the values for the second pizza.
BUT you must avoid writing sweeping
4. ppsi2 <- the price per sq. inch of the second
commands that hide all the interesting pizza.
detail! 5. IF ppsi1 < ppsi2 THEN first pizza is better value
Use indentation to help readability. ELSE second pizza is better value.
Use brackets to help
More detail may be provided in this step, such as
readability/ambiguity. how to compute ppsi.
5 Program Development 21 5 Program Development 22
6
Step 5: Testing Another Sample Algorithm
Good testing is crucial. Write a program to print the perfect numbers
Don’t assume that any answer is correct! between 1 and an entered number.
A positive whole number is ‘perfect’ if the sum
Try a variety of test cases that test of its divisors (apart from itself) equals the
different paths of your program. number.
Developing test data is a whole science in Step 1:
Input : an integer
itself!
Output : the perfect numbers between 1 and the
entered integer
Info needed: definition of “perfect number” above.
7
Perfect numbers Step 4 Perfect numbers Step 5
Implementing the algorithm in C++ is Compile , run and check.
straightforward. Is the output what you expect given step 2?
See allperfectnumbers.cpp If there are logic errors you can’t find
Remember to use == for equality tests! Add debug output statements.
Declare variables. What happens if the user’s input is invalid?
We have ignored this problem so far, for simplicity,
Check that they all get initialised.
but in general all inputs should be checked for
Be very careful with { } braces validity before use.
to code it. per hour, 200 euro gross, and tax = tax credit.