CS11AlgorithmDesign&ProblemSloving
CS11AlgorithmDesign&ProblemSloving
Learning objectives
0. show understanding that an algorithm is a solution to a problem expressed as a sequence of
defined steps
1. use suitable identifiers for the representation of data used by a problem and summarise
identifiers using an identifier table
2. show understanding that many algorithms are expressed using the four basic constructs of
assignment, sequence, selection and repetition
3. show understanding that simple algorithms consist of input, process, output at various stages
4. document a simple algorithm using: structured English, pseudocode, program flowchart
5. derive pseudocode or a program flowchart from a structured English description of a problem
6. derive pseudocode from a given program flowchart
7. show an appreciation of why logic statements are used to define parts of an algorithm solution
8. use logic statements to define parts of an algorithm solution use the technical terms associated
with arrays including upper and lower bound
9. select a suitable data structure (1D or 2D array) to use for a given task
10. write algorithms to process array data including sorting using a bubble sort and searching using
a linear search.
2.1.1 Algorithms
0. show understanding that an algorithm is a solution to a problem expressed as a sequence of
defined steps
1. use suitable identifier names for the representation of data used by a problem
- summarise identifier names using an identifier table
2. show understanding that many algorithms are expressed using the four basic constructs of
assignment, sequence, selection and repetition
3. show understanding that simple algorithms consist of input, process, output at various
stages
4. document a simple algorithm using:
- structured English
Part 2
- pseudocode (on the examination paper, any given pseudocode will be presented using the
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
14
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
15
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
Structured English
Structured English provides a more formal way of documenting the stages of the algorithm:
PROCESS RepairPuncture
Start: JobDone ← "NO"
IF 'a1l tools are not available'
THEN
Delay the task
GOTO End
ELSE
Remove the wheel
Remove the inner-tube
ENDIF
IF 'there is major damage'
THEN
Buy a new inner tube
GOTO Delay
ELSE
Inflate the tube to locate the leak
Apply glue and a patch
Inflate the tube to re-test
ENDIF
IF 'still leaking'
THEN
Purchase new inner tube
GOTO Delay
ELSE
Part 2
Re-assemble
JobDone ← "YES"
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
ENDIF
Delay: Purchase new inner-tube
Fundamentals Problem-Solving and Programing Skills
IF JobDone ← "NO"
THEN
GOTO Start
ENDIF
End:
ENDPROCESS
In the structured English:
• Three labels (Start, Delay and End) have been used to mark particular steps in the
algorithm.
• When a decision has to be made the keywords IF - THEN - ELSE - ENDIF have been used.
• Indentation and the use of blank lines (‘whitespace’) has been used to indicate which steps
belong together.
• The complete process has been given an identifier name, RepairPuncture.
16
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
Identifier names
We have used several identifier names:
• for the whole process - RepairPuncture
• to indicate whether or not the process has been completed - JobDone
• to label steps - Start, Delay and End.
Sequence
Traditional high-level programming languages are designed for problems which can be designed as
a sequence of steps. It is a feature of a text editor or IDE for program development that the lines of
code can be numbered to show the sequence.
Assignment
In our algorithm, JobDone is initially assigned the value ‘NO’ and later assigned a value of
‘YES’.
Note that the symbol ← has been used to mean ‘the value on the right of the statement is assigned to
the identifier on the left’.
Selection
In our algorithm, a question is asked: ‘are all the tools available?’
This is called a condition. The result of the condition is always either TRUE or FALSE. The
Part 2
algorithm carries out a different set of steps for the TRUE and FALSE results.
Iteration
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
Iteration will occur in an algorithm design when a block of steps is repeated. This is done in a
Fundamentals Problem-Solving and Programing Skills
Stepwise refinement
In the design, one of the steps is stated as Re-assemble.
This may not convey to a reader of the design precisely what this involves, so the step could be
expanded; that is, we could use stepwise refinement.
Re-assemble consists of:
Replace the inner tube
Replace the tyre
Replace the wheel
Inf1ate the inner tube
17
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
Start
• assignment or action
Action
• decision box
Conditi Yes
on?
No
Input
18
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
An identifier table
The task requires the input, processing and output of data items. We shall use the identifier names
summarised in this Identifier Table for the Garage Application:
Identifier name Description
PricePaid Original price paid
RegDate Registration date
Mileage Car mileage when purchased
NormalMileage The expected car mileage based on the age of the car
ProvSellPrice The calculated selling price
Part 2
Later, when we attempt to write pseudocode or program code for the problem, the programmer will
Fundamentals Problem-Solving and Programing Skills
need to specify the type of data which will be stored by e.ach identifier.
19
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
These processes will have data inputs, called parameters We shall make up an identifier name for
each process and use the identifiers in the table (page 19) for the parameters.
This gives a high-level description of the problem:
InputCarData(PricePaid, RegDate, Mileage)
CalculateSellingPrice(RegDate, PricePaid, Mileage)
OutputSellPrice(SellPrice, Profit)
These stages are designed as 'modules'. Later they will be implemented with program code as
'procedures'.
The breaking down of the problem into modules and the passing of data values into each module
can be shown using a structure chart hereunder:
CarPriceCalculator
RegDate
PricePaid
Mileage
The name implies that something is wrong! All program code needs to be extensively tested. This
Fundamentals Problem-Solving and Programing Skills
will involve:
• choosing appropriate test data
• using a trace table to check carefully how the data values change as each step of the program
is executed.
Before we can complete a trace table, we need to formalise the algorithm required. We shall write
this using pseudocode with the identifier names from the table (page 19). (The // symbol denotes a
‘comment’)
00 // InputCarData module
01 INPUT Pricepaid
02 INPUT ReqDate
04 INPUT Mileage
05 // CalculateSelling Price module
06 Months ← TodaysDate - RegDate
20
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
07/2017
Case 2: A car registered in November 2017 is purchased for $5000. It has done 8000 miles. These
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
figures mean the vehicles has the ‘low mileage’ price adjustment. As the provisional selling price
has changed, this new value can be shown on the next row:
Fundamentals Problem-Solving and Programing Skills
In any trace table, as more values are calculated the numbers will occupy more rows.
Progress check: A program calculates the interest earned on $100. The value of the investment
increases by 20% each year. Stop the process when the amount reaches $150. Draw a trace table
to complete the execution of this algorithm, adding blank rows as required, and draw a program
flowchart for this calculator algorithm:
YearNumber Amount OUTPUT
21
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
00 YearNumber ← 1
01 Amount ← 100
02 Amount ← Amount * 1.20
03 IF Amount >= 150
04 THEN
05 OUTPUT YearNumber
06 STOP
07 ELSE
08 YearNumber ← YearNumber + 1
09 GOTO Step 03
10 ENDIF
2.1.3 Adaptive maintenance
7. make amendments to an algorithm and data structure in response to specification changes
8. analyse an existing program and make amendments to enhance functionality
An existing program may need to be 'adapted' or changed. This could be for a variety of reasons.
The most likely is that the user now has slightly different requirements and so the specification of
the program needs to be changed. If the program design needs to be changed, so will the program
code.
For our car purchase program, the garage might decide the following:
• Car users are now traveling on average a greater distance in a month. It's now on average 200
miles per month (not 1000).
• Cars seem to be holding their price better: We need to calculate that the value goes down by
only 3% per month (not 5%).
These changes to the specification, and possible other specification changes, will require that the
program code is amended and re-tested.
Progress check: A program is to developed to calculate the tax paid by employees each month.
Part 2
The program has been written and successfully used for a year. Why might adaptive maintenance
be required at some future date?
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
Fundamentals Problem-Solving and Programing Skills
Chapter Summary
22
02.11 Algorithm Design and Problem-Solving Flair · Discipline · Academic Rigour
⃞ A structure chart is used to document the parameters passed in and out of modules which
make up the program design.
⃞ Corrective maintenance is carried out using white box testing. This involves drawing up
suitable test cases with appropriate data and then -using a trace table-tracing various routes
through the code.
⃞ Adaptive maintenance takes place when the user makes a change to the program
specification.
Exam-style questions
0. Ali uses a sequential file of records to store the user IDs and encrypted passwords. When a user
types in their User ID, the program calls a function, FindPassword, with parameter
ThisUseriD. The function searches each record in the file for ThisUseriD and retums the
encrypted password. If ThisUseriD is not stored in the file, the function returns an error
code. Complete the pseudocode:.
FUNCTION FindPassword (ThisUseriD: STRING) RETURNS . . . . . . . . .
Found ← FALSE
WHILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
IF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
THEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ENDIF
Part 2
ENDWHILE
·
CHAPTER 11 ALGORITHM DESIGN AND PROBLEM-SOLVING
IF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
THEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fundamentals Problem-Solving and Programing Skills
ELSe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ENDIF
CLOSEFILE “PASSWORDS”
ENDFUNCTION [8]
Cambridge International AS and A Level Computing 9691 Paper 23, Q2b, Nov 2014
23