You are on page 1of 20

2.

1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Show understanding that every computer system is made up of sub-systems, which in turn are
made up of further sub-systems

A system is a set of interacting or interdependent component parts forming a complex/intricate


whole.

A subsystem is a set of elements, which is a system itself, and a component of a larger system.

R
Components and subsystems fall into three categories: hardware, software, and network.

KU
Elements of a system: an example
Following are considered as the elements of a system in terms of Information systems:
 Input
 Output
 Processor
 Control
 Feedback

A
 Boundary and interface
 Environment

Types of systems
Systems are classified in different ways:
 Physical or abstract systems.
TH
 Open or closed systems.
 'Man-made' information systems.
 Formal information systems.
 Informal information systems.
 Computer-based information systems.
 Real-time system.
ED

 Use top-down design, structure diagrams, flowcharts, pseudocode, library routines and
subroutines

In order to understand how a computer system is built up and how it works, it is often divided up into
sub-systems. This division can be shown using top-down design to produce structure diagrams that
HM

demonstrate the modular construction of the system. Each sub-system can be developed by a
programmer as sub-routine or an existing library routine may be already available for use. How each
sub-routine works can be shown by using flowcharts or pseudocode.

Tools and Techniques


 Top-down design
 Structure diagrams
 Pseudocode
 Flowcharts
 Library routines
A

 Subroutines

Top-down design
A technique in which program/algorithm broken down into simpler modules/smaller tasks where
each module is further sub-divided until basic elements are produced. It allows several programmers
to work at same time on the software. Programmers can test each module independently.

OR

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 1
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Top down design also called stepwise refinement is breaking down the problem/task/program into
sub problems/smaller tasks/modules.

Advantages
 Breaking the problem into parts helps us to clarify what needs to be done.
 At each step of refinement, the new parts become less complicated and, therefore, easier to
figure out.
 Parts of the solution may turn out to be reusable.

R
 Breaking the problem into parts allows more than one person to work on the solution

Disadvantages

KU
 It is difficult to guarantee that the initial design adequately models the desired system
 It is tougher to do testing, as a large portion of the design must be done first
 On a large projects, redundant code may become an issue

Structure Diagram
Data structure diagram (DSD) is a diagram of the conceptual data model which documents the

A
entities and their relationships, as well as the constraints that connect to them.

Structure Chart
A Structure Chart (SC) in software engineering and organizational theory, is a chart which shows the
breakdown of a system to its lowest manageable levels. They are used in structured programming
TH
to arrange program modules into a tree. Each module is represented by a box, which contains the
module's name. The tree structure visualizes the relationships between modules.

Example of Structure Diagram: Department Store


 A department store operates in several cities.
 In a city there is one headquarter coordinating the local operations.
 A city may have several stores.
 Stores hold any amount of items.
ED

 Customers place their orders for any number of items to a given store.

Goal: Optimize shopping in each city.

Structure Diagram:
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 2
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Structure Diagram for Department Store


Example: Alarm for Smart Phone
Consider the alarm app computer system for a smart phone. This could be divided into three sub-
systems, setting the alarm, checking for the alarm time, sounding the alarm. These sub-systems could
then be further sub-divided; the structure diagram makes the process clearer.

R
KU
A
TH
Structure diagram for Alarm App

Example: A School Website’s Structure Diagram


ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 3
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Example: Structure Diagram for a large company


Line and staff organizations have both line and staff executives. Line executives are assisted by staff
specialists in R & D, planning, distribution, quality, legal, audit, public relations, etc. The job of staff is
mainly advisory and guidance. Line executives maintain the supervisory power and control over the
execution of work.

R
KU
A
TH
ED
HM

Example: Structure Diagram of a Company


A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 4
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Algorithm
 What is an algorithm?
An algorithm is a precise, step-by-step set of instructions for solving a task. An algorithm does not
solve a task; it gives you a series of steps that, if executed correctly, will result in a solution to a
task.

R
 What is algorithmic thinking?
Algorithmic thinking is the ability to understand, execute, evaluate, and create algorithms. Let's
discuss each of these ideas separately.

KU
Algorithm is written in two ways:
 Pseudocode
 Flowchart

Pseudocode for Alarm

A
REPEAT
Wait 10 seconds
Get Time
UNTIL Time = Alarm Time
Sound Alarm
TH
Flowchart

A FLOWCHART shows diagrammatically the steps required for a task (sub-system) and the order that
they are to be performed. These steps together with the order are called an ALGORITHM. Flowcharts
are an effective way to communicate the algorithm that shows how a system or sub-system works.
ED

Have a look at a flowchart for the checking-for-the-alarm-time sub-system.


HM
A

Library routines

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 5
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

A LIBRARY ROUTINE is a set of programming instructions for a given task that is already available for
use. It is pre-tested and usually performs a task that is frequently required. For example, the task ‘get
time’ in the checking-for-the-alarmtime algorithm would probably be readily available as a library
routine.

Sub-routines
A SUB-ROUTINE is a set of programming instructions for a given task that forms a subsystem, not the
whole system. Sub-routines written in high-level programming languages are called ‘procedures’ or

R
‘functions’ depending on how they are used. See Chapter 11 for further details.

KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 6
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Work out the purpose of a given algorithm

Example: Pseudocode
SET count, total, number, average, x to 0
Count =1

REPEAT

R
INPUT number
IF number > 0 THEN
Total = total + number

KU
X = x +1
ENDIF
Count = count + 1
UNTIL count = 10
AVERAGE = total/x

A
OUTPUT average
END

Example: Flowchart
TH
ED
HM
A

Input Values: 15, -2, 0, 8, 0, 21, -8, -12, 1, 25

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 7
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Working of the Algorithm (Trace Table)

R
KU
A
TH
ED
HM

Solution of Trace Table


A

Purpose of this algorithm: To find the average of all positive numbers entered

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 8
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Explain standard methods of solution

Structured Programming: Programming that produces programs with clean flow, clear design, and
a degree of modularity or hierarchical structure.

Modular Programming: Modular programming is an approach to writing programs which divides up


a task into separate sections or modules (sub programs) to perform a specific job. In theory

R
amendments are easier to make because individual sections can be corrected without having to
work through the whole program.

KU
Methods of Solution for Pseudocodes:
 Variables
 Value/Variable Assignment
 Operators
 Arithmetic Operators
 Relational Operators
 Logical Operators

A
Statement: A program statement is a single instruction composed of its reserved words.

Expression: An expression is a set of variables, constants & operators (such as +, -, etc) that is to be
evaluated by the computer.
TH
Reserved Words: A reserved word is a name that has some special significance to the compiler or
interpreter. The programmer can only use it for its special purpose & can’t use it as an identifier.

Identifier: An identifier is a name invented by the programmer for some data. An identifier can be a
name for a variable, a constant, a file, an array, etc.

Variable: A variable is a quantity named in a program & whose value can change.
ED

Constant: A constant is a value that doesn’t change. In a program it may or may not be given a
name.

Assignment Operator/Symbol: 
HM

Operators: In mathematics and sometimes in computer programming, an operator is a character


that represents an action, as for example x is an arithmetic operator that represents multiplication.

Arithmetic Operators
A = 10
B = 20
OPERATOR PHRASE DESCRIPTION EXAMPLE

+
A

Add Adds two operands A + B will give 30

- Subtract Subtracts second operand from the first A - B will give -10

* Multiply Multiplies both operands A * B will give 200

/ Divide Divides numerator by de-numerator B / A will give 2

% MOD This gives remainder of an integer division B % A will give 0

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 9
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Relational Operators
A = 10
B = 20
OPERATOR PHRASE DESCRIPTION EXAMPLE

= Equal to Checks if the values of two operands are


equal or not, if yes then condition
(A = B) is not
true.

R
becomes true.

> Greater than Checks if the value of left operand is (A > B) is not

KU
greater than the value of right operand, if true.
yes then condition becomes true.

< Less than Checks if the value of left operand is less


than the value of right operand, if yes then
(A < B) is true.

condition becomes true.

A
>= Greater than or
equal to
Checks if the value of left operand is
greater than or equal to the value of right
(A >= B) is not
true.
operand, if yes then condition becomes
true.
TH
<= Less than or equal
to
Checks if the value of left operand is less
than or equal to the value of right
(A <= B) is
true.
operand, if yes then condition becomes
true.

<> Not equal to Checks if the values of two operands are


equal or not, if values are not equal then
(A <> B) is
true.
ED

condition becomes true.

Logical Operators
A = 10
HM

B = 20
OPERATOR DESCRIPTION EXAMPLE

(A AND
Called Logical AND operator. If both the operands
AND B) is
are non-zero, then condition becomes true.
false.

Called Logical OR Operator. If any of the two (A OR B)


OR
operands is non-zero, then condition becomes true. is true.
A

Called Logical NOT Operator. Use to reverses the


(A NOT
NOT logical state of its operand. If a condition is true then
B) is true.
Logical NOT operator will make false.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 10
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Flowchart
A graphic map of the path of control or data through the operations in a program or an information-
handling system. Symbols such as squares, diamonds, and ovals represent various operations. These
symbols are connected by lines and arrows to indicate the flow of data or control from one point to
another.

OR

R
The block diagrammatic representation of the flow of program is known as Flowchart.

Methods of Solution for Flowcharts: Flowchart Symbols

KU
SHAPE NAME SYMBOL PURPOSE

Oval Terminal Interrupt Start/Stop of Flowchart

A
Parallelogram Input/output Box TH For Input and Output

For Variable initialization


Rectangle Process Box
Calculation

For Selection
Rhombus Decision Box
IF condition
ED
HM

Lines with arrow Flowlines Direction of flow

On-page To continue the flowchart


Small Circle
Connector on same page
A

Off-page To continue the flowchart


connector on another page

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 11
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Suggest and apply suitable test data

Testing
Testing is a method of checking whether the system/program/field is functioning appropriately
according the designed data requirements.

R
Test Data
Test data is data which has been specifically identified for use in tests, typically of a computer
program

KU
Testing carried out using the plan to make sure that all the parts of the system work correctly with
normal, extreme and erroneous data
 Normal test data is used to check that a system can handle the sort of data that would be
expected during day-to-day use
 Extreme test data is used to check that a system can cope with data that lies on the boundaries
of what is acceptable

A
 Erroneous/Abnormal (or exceptional) test data is used to check that a system can identify data
that is wrong and reject it

Example: Testing students marks between 0 - 50

Test Type Test data


TH Purpose Expected Result

Test input
Notmal Enter incorrect mark ‘45’ Mark accepted
markfunction
Test input
Abnormal Enter incorrect mark ‘–1 Mark rejected
markfunction
ED

Enter lowest extreme marks Test lowest


Marks accepted
i.e. 0 extreme marks
Extreme
Test highest
Enter highest extreme marks
extreme marks Marks accepted
i.e. 0
HM

i.e. 50

 Understand the need for validation and verification checks to be made on input data (validation
could include range checks, length checks, type checks and check digits)

Validation
Validation is a process of checking if data satisfies certain criteria when input i.e. falls within
A

accepted boundaries. Examples of validation techniques include:

It is the checking of the type of data, i.e. the checking if the data is in
Type Check
the same type/format as it is required or not.
this checks whether data is within given/acceptable values e.g.
Range check
checks if a person’s age is > 0 but is also < 140.
this checks if the input data contains the required number of
Length check characters e.g. if a field needs 6 digits, then inputting 5 digits or 7
digits, for example, should be rejected.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 12
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

this checks that the input data doesn’t contain invalid (type check)
Character check
characters; e.g. a person’s name shouldn’t contain numbers.
this checks that data is in a specified format (template) e.g. (picture
Format check
check) date should be in the form dd/mm/yyyy.
this is similar to a range check except only ONE of the limits
Limit check
(boundaries) is checked e.g. input data must be > 10.
check that data is actually present and not missed out e.g. in an

R
Presence check electronic form, somebody’s telephone number may be a required
field.
Consistency this checks if fields correspond (tie up) with each other e.g. if Mr. has

KU
check/ Cross field been typed into a field called “Title” then the “Gender” field must
check contain M or Male.
this is an extra digit added to a number which is calculated from the
Check digit digits; the computer re-calculates and validates the check digit
following input of the number (see next page).
(NOTE: check digits can identify 3 types of error:
(1) if 2 digits have been inverted e.g. 23459 instead of 23549

A
(2) an incorrect digit entered e.g. 23559 instead of 23549
(3) a digit missed out altogether e.g. 2359 instead of 23549)
TH
This section shows how check digits are calculated. The ISBN-10 (used on books) has been chosen
as the example; this uses a modulo 11 system which includes the letter X to represent the number 10.

Example 1
To calculate the check digit for the ISBN 0 - 2 0 1 - 5 3 0 8 2 - ?

(i) the position of each digit is considered:


(ii) Each digit is then multiplied by its digit position and the totals added together
ED

(iii) the total is then divided by 11 (modulo – 11) and the remainder, if any is subtracted from 11 to
give the check digit.
98 ÷ 11 = 8 remainder 10
11 – 10 = 1

This gives a check digit of 1


HM

final ISBN becomes 0 -2 0 1 - 5 3 0 8 2 - 1

Example 2
To check the correctness of a check digit the computer re-calculates it as follows:

The ISBN to check is: 0 - 1 3 1 5 - 2 4 4 7 - X

(i) the position of each digit is considered:


(ii) each digit is then multiplied by its digit position and the totals added together
(iii) the total is then divided by 11; if there is no remainder then the check digit is correct:
A

132 ÷ 11 = 12 remainder 0

hence the check digit is correct

Verification
Verification is a way of preventing errors when data is copied from one medium to another (e.g.
from paper to disk/CD, from memory to DVD, etc.). There are a number of ways in which verification
can be done:

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 13
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Double entry: in this method, data is entered twice (using two different people); the data is only
accepted if both versions match up. Often used to verify passwords by asking them to be typed
in again by the same person.

 Visual check: this is checking for errors by comparing entered data with the original document
(NOTE: this is not the same as proof reading!!).

Parity check: this is used to check data following potential transmission errors; an extra bit is added

R
to each binary number before transmission – e.g. EVEN parity makes sure each number has an even
number of 1 – bits;

KU
Example: if 11000110 is sent (four 1’s)
and 11100110 is received (five 1’s)

then the system will know an error has occurred.

 Use trace tables to find the value of variables at each step in an algorithm

A
Trace Table
A trace table is a technique used to test algorithms, in order to make sure that no logical errors occur
whilst the algorithm is being processed.
TH
A trace table is a true interpretation of understanding layers and levels of algorithms following the
steps of algorithm in a tabular form.

Example 1: Algorithm to output sum of first 10 natural numbers.

Set a,sum  0
WHILE a < 10 DO
a = a + 1
ED

sum = sum + a
ENDWHILE
OUTPUT sum
END

TRACE TABLE
HM

a a < 10 a = a + 1 Sum = sum + a Comments


latest value of a = 1
0  a = 0 + 1 = 1 sum = 0 + 1 = 1
latest value of sum = 1
1  a = 1 + 1 = 2 sum = 1 + 2 = 3 value of a increases (1
2  a = 2 + 1 = 3 sum = 3 + 3 = 6 is added to a in each
3  a = 3 + 1 = 4 sum = 6 + 4 = 10 repetition of loop)
4  a = 4 + 1 = 5 sum = 10 + 5 = 15
5  a = 5 + 1 = 6 sum = 15 + 6 = 21
value of sum increases
6  a = 6 + 1 = 7 sum = 21 + 7 = 28
7  a = 7 + 1 = 8 sum = 28 + 8 = 36 (value of a is added to
A

8  a = 8 + 1 = 9 sum = 36 + 9 = 45 sum in each repetition


9  a = 9 + 1 = 10 sum = 41 + 10 = 55 of loop)
 - - -
Output sum: 55

Conclusion: Using the trace table we had been able to see what is happening in each step of
algorithm until the algorithm ends, and to be able to see the result.

TIPS: 1. Must follow the sequence of Algorithm in the columns of Trace Table.
2. Always use the instructions with variables in the trace table.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 14
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

3. Always use the calculations in trace tables.


4. Must avoid duplication of columns that may result in mistake/error.
5. Must use conditions in columns for TRUE() and FALSE ()

Example 2: Paper 21, June 2015 – Q3

The flowchart below inputs the weight of a number of parcels in kilograms. Parcels weighing more
than 25 kilograms are rejected. A value of –1 stops the input.

R
The following information is output: the total weight of the parcels accepted and number of parcels
rejected.

KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 15
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Task: Complete the trace table for the input data:


1.8, 26.0, 7.0, 11.3, 10.0, 2.5, 25.2, 5.0, 19.8, 29.3, –1

Method 1:
1 2 3 4 5 6
Input Weight = -1 Weight > 25
Total Reject Weight
Weight YES NO YES NO

R
Total
Reject
Output Goto =
=
Total, End Column total
Reject
Reject 6 +

KU
+ 1
weight

Method 2: (Additional Columns Skipped as per question paper)

A
TH
ED

Solution:
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 16
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Identify errors in given algorithms and suggest ways of removing these errors

Errors in algorithms can be easily Identified/traced if :


1. The purpose/task of algorithm is known/mentioned.
2. A Trace table is used.

Example 1: June 2015, Paper 22 – Q2

R
Read this section of program code that should input 30 positive numbers and then output the largest
number input.

KU
1 Large = 9999
2 Counter = 0
3 WHILE Counter > 30
4 DO
5 INPUT Num
6 IF Num < Large THEN Large = Num

A
7 Counter = Counter - 1
8 ENDWHILE
9 PRINT Large

There are four errors in this code.


TH
Locate these errors and suggest a corrected piece of code for each error

Error 1:
Large Counter Counter > 30 Input Num IF Num < Large Counter =
Then Counter -1
Large = Num
9999 0 FALSE () - - -
ED

Corrected
Large Counter Counter < 30 Input Num IF Num < Large Large
Then
Large = Num
Line 3
HM

Error 2:
Large Counter Counter < 30 Input Num IF Num < Large Counter =
Then Counter -1
Large = Num
9999 0 TRUE () - - -

Corrected: To check if the number is positive, Large must be 0


Large Counter Counter < 30 Input Num IF Num < Large Counter =
A

Then Counter -1
Large = Num
0 0 TRUE () - -
Line 1

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 17
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

Error 3:
Large Counter Counter < 30 Input Num IF Num < Large Counter =
Then Counter -1
Large = Num
0 0 TRUE () -26 Large = -26 -

Corrected: To filter larger number, the number must be checked for greater (>) than larger.

R
Large Counter Counter < 30 Input Num IF Num > Large Counter =
Then Counter -1
Large = Num
0 0 TRUE () 26 Num > Large

KU
26 > 0
Large = 26
Line 6

Error 4:

A
Large Counter Counter < 30 Input Num IF Num > Large Counter =
Then Counter -1
Large = Num
0 0 TRUE () 26 Large = 26 Counter = -1
TH
Corrected: To run loop 30 times, the counter must go from 0 to 30. If the counter is decremented 1
value from 0 i.e. 0, -1, -2…. Then the loop never ends.
Large Counter Counter < 30 Input Num IF Num > Large Counter =
Then Counter + 1
Large = Num
0 0 TRUE () 26 Large = 26 Counter = 1
26 1 TRUE () 65 Large = 65 Counter = 2
65 2 TRUE () 45 - Counter = 3
65 3 TRUE () 77 Large = 77 Counter = 4
ED

- Line 7

Solution:
Line 1 or Large =9999: this should read Large = 0
Line 3 or WHILE: this should read WHILE Counter < 30
HM

Line 6 or IF: this should read IF Num > Large THEN Large = Num
Line 7 or Counter =…: this should read Counter = Counter + 1
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 18
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Produce an algorithm for a given problem (either in the form of pseudocode or flowchart)

Example 1: June 2014, Paper 11 – Q15


A survey is being carried out which involves reading and recording sound levels near a busy road
junction. Once all the data are collected, they are input manually into a computer. A sound level of
0 decibels (0 dB) is input to indicate the end of the data.

R
Write an algorithm, using pseudocode or a flowchart, which:
 inputs all the sound levels
 after a sound level of 0 is input, outputs the following:

KU
 average sound level
 highest recorded sound level

Solution: (Pseudocode)
total = 0: highest = 0: count = 0

A
INPUT sound
WHILE sound > 0 DO
total = total + sound)
IF sound > highest THEN highest = sound
count = count + 1
INPUT sound
TH
ENDWHILE
average = total/count
Print average, highest

Example 2: June 2013, Paper 12 – Q17


A country has four mobile phone network operators. Each mobile phone number has eight digits.
The first three digits identify the network operator:
ED

444 Yodafone
555 N2 network
666 Kofee mobile
777 Satsuma mobile
HM

Write an algorithm, using pseudocode or flowchart only, which reads 50 000 eight-digit mobile phone
calls made during the day and outputs the number of calls made on each of the four networks.

Solution:

Y = 0: N = 0: K = 0: S = 0

FOR count = 1 to 50 000


INPUT number
X = number/100 000 000
A

IF X > 0.7 THEN S = S + 1


ELSEIF X > 0.6 THEN K = K + 1
ELSEIF X > 0.5 THEN N = N + 1
ELSEIF X > 0.4 THEN Y = Y + 1
ELSE PRINT “error in number”
NEXT count
PRINT Y, N, K, S

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 19
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR

2.1.1 PROBLEM-SOLVING AND DESIGN

 Comment on the effectiveness of a given solution

There are many different solutions to the same problem. In order to consider the effectiveness of a
given solution ask the following questions.

 Does the solution work for all sets of data?


 Does the solution have any unnecessary processes that are never used?

R
 Are any actions repeated more often than necessary?
 Can the solution be simplified and still work as well?

KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 20
2210

You might also like