Program Development: Five Steps: Objectives

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Objectives

Program  This topic introduces programming as a


problem solving discipline.
Development:  It presents a five step procedure for
Five Steps 
program development.
At the end of this topic you should be
able to develop more complex programs
1E3Topic 5
using a systematic approach.

5 Program Development 1 5 Program Development 2

Programming and Problem


Programming Process
Solving
 Algorithm  Up till now we’ve gone straight to
 A sequence of precise instructions which programming.
leads to a solution  For big complex problems we need to be more
systematic.
 Program  We need “solve the problem” and “map out a
solution” before trying to code it in C++.
 An algorithm expressed in a language the
computer  This is where algorithms in pseudocode come in.
can understand

5 Program Development 3 5 Program Development 4

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

5 Program Development 5 5 Program Development 6

Implementation Phase Example: Better-valued Pizza


Step 4: Translate the algorithm into a  Consider Practical 2.
programming language  Step 1
Step 5: Test the program  Input: diameter and price of two pizzas
 Output: the diameter of the better valued pizza,
Testing may result in based on price per sq. inch
Compilation (parse) errors  Info required:
 Price per square inch is price/area
Logic errors  Area of a circle is (pi * radius * radius)
Errors will require you to return to some step.  Radius is diameter/2

5 Program Development 7 5 Program Development 8

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

 This step is more useful for other problems


 But it might have helped avoid people doing
 Area/price !!! Or ppsi1 < ppsi2 => No.2 is better value!!!!

5 Program Development 9 5 Program Development 10

Definition: Algorithm Algorithm Representation


 An algorithm is an  An algorithm can be represented in many
 ordered set of ways.
 unambiguous  F = (9/5)C+32
 executable steps
 Multiply the celsius reading by 9, then divide
 that defines a terminating process. by 5, then add 32.
 Recipes, route directions, knitting  A program is one formal representation
instructions, assembly instructions are all
of an algorithm designed for execution on
examples.
a computer.
 Though some examples may have ambiguity!!
5 Program Development 11 5 Program Development 12

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

Languages Pseudocode (1)


 A language consists of  ASSIGN a <- expression
 Primitives  A is given the value of the expression
 Syntax for how to combine the primitives
 Semantics - meaning of the primitives and the
combinations  SELECT IF condition
 A programming language like C++ is an THEN activity
example. ELSE activity
 But we will now look at a less formal language
for algorithm design.  IF balance < 0 THEN print “Overdrawn”
 Known as pseudocode ELSE print “In credit”

5 Program Development 15 5 Program Development 16

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 Program Development 17 5 Program Development 18

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 19 5 Program Development 20

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)

5 Program Development 21 5 Program Development 22

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.

5 Program Development 23 5 Program Development 24

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.

5 Program Development 25 5 Program Development 26

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 users 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.

5 Program Development 27 5 Program Development 28

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.

5 Program Development 29 5 Program Development 30

You might also like