Professional Documents
Culture Documents
Program Development: Five Steps: Objectives
Program Development: Five Steps: Objectives
Program Development: Five Steps: Objectives
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 Procedural abstraction
DO activity Give a name to an algorithm
WHILE there are more names in the hat Use the name to execute that algorithm
DO draw a name and print it within another one.
ITERATE Example
FOR EACH of a set of items procedure sort (list)
DO activity {algorithm for sorting the list}
FOR EACH integer between 1 and 100 sort (list-of-names)
DO print the number squared Means APPLY the sort procedure to list-of-names.
5
Same algorithm but using
Step 4: Implement in C++
ABSTRACTION
1. Read in the diam and price of the first pizza.
Implement the algorithm in C++
2. ppsi1 <- ppsi (diameter1, price1) This step is relatively straightforward for
3. Read in the values for the second pizza. the pizza algorithm. See solution.
4. ppsi2 <- ppsi (diameter2, price 2)
5. IF ppsi1 < ppsi2 THEN first pizza is better value ELSE In other cases it involves some ingenuity.
second pizza is better value. Sometimes at this stage you discover that
FUNCTION ppsi (diameter, price)
returns the price per square inch of a pizza diameter your algorithm is inadequate and have to
inches wide costing price euros. go back to Step 3, or earlier.
RETURN price / (pi * diam/2 * diam/2)
6
Perfect Numbers Step 2 Perfect numbers Step 3
Try to do it yourself Get number from user
Is 4 perfect?
Factors are 1 and 2; 1 + 2 = 3; not 4; not perfect. FOR n from 1 to entered number
Is 6 perfect? Sum <- 0
Factors are 1, 2, 3; 1+2+3=6; 6 is perfect.
FOR i from 1 to n
Try 1, 2, 10, 12, …
{ IF i is a factor of n
So for each number n to be checked
THEN add i to sum }
we need to test each integer from 1 to n-1 to see if it’s
a factor; if it is add it to a running total. IF sum = n THEN print n “is perfect”
If the running total ends up equal to n, n is perfect -
print it out.
7
Practical 5 Practical 5 continued
Apply the 5 step process to Practical 5. Step 4: Implement the algorithm in C++.
Step 1: Make sure you understand what Step 5: Test
the input is and how you are to compute Check at least
the output. No overtime and some overtime
All at 20% and some at 40% tax
Step 2: Work the example given and a few
Where tax credit covers full tax liability and where
more to get a sense of the algorithm it doesn’t.
Step 3: Sketch the algorithm before trying Boundary conditions : 0 hours, 40 hours, 0 euro
to code it. per hour, 200 euro gross, and tax = tax credit.