0% found this document useful (0 votes)
1 views

New Microsoft PowerPoint Presentation (2)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

New Microsoft PowerPoint Presentation (2)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

ALGORITHM

DESIGNING AND
PROBLEM
SOLVING
7.1 THE DEVELOPMENT
LIFE CYCLE

• The program
development life
cycle is divided into
five stages: analysis,
design, coding,
testing.
Abstraction keeps the key elements required for the solution to the problem and discards any unnecessary
details and information that is not required. For example, a map only shows what is required for travelling
from one place to another. Different methods of transport will require different types of map.

Decomposition breaks down a complex problem into smaller parts, which can then be subdivided into even
smaller parts, that can be solved easily. Any daily task can be divided into its constituent parts.
7.1.2 Design
The program specification
from the analysis stage is
used to show to how the
program should be
developed. When the
design stage is complete,
the programmer should
know what is to be done, all
the tasks that need to be
completed, how each task is
to be performed and how
the tasks work together. This
can be formally documented
using structure charts,
flowcharts and
pseudocode – see Section
Pseudocode Flowchart 7.2.
7.1.3 Coding and Iterative
Testing
The program or set of programs is developed. Each module of
the program is written using a suitable programming language
and then tested to see if it works. Iterative testing means
that modular tests are conducted, code amended, and tests
repeated until the module performs as required.
7.2.2 Decomposing a problem
We know any problem can be solved by using computer
system. There are four main computer components that are
needed to find a solution.
INPUT - The data used by the system PROCESS - The task that
should be performed
STORAGE - Data that needs to be stored in the OUTPUT - Information that
should be displayed
7.2.3 Methods used to design and
construct a solution to a problem
There are some methods to design and construct a solution to a problem.
Formal methods help us to understand clearly and effectively. These are
the formal methods that we used:

Structure diagrams
Flowcharts
Pseudocode

Structure charts
Structure diagrams can be used to show top-down design in a diagrammatic
form.
Structure diagrams are hierarchical, showing how a computer system
solution can be
divided into sub-systems with each level giving a more detailed breakdown.
If necessary, each sub-system can be further divided.
Flowcharts
A flowchart shows
diagrammatically the
steps required to complete
a task 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 how the
algorithm that makes up a
system or sub-system
Begin/end
Terminator flowchart symbols are
used at the beginning and end of
each flowchart.
Process flowchart Symbols are used to show actions, for
example, when values are assigned to variables. If a process
has been defined elsewhere then the name of that process is
shown

INPUT and OUTPUT DECISIONS We know about “if” operator in HLL. It helps
The same flowchart symbol is us to make decisions. But it can also be “for”, “while” and
used to show the input of data “repeat”. Decision flowchart symbols are used to
and output of information decide which action is to be taken next; these can be used
for selection and repetition/iteration. There are always
two outputs from a decision flowchart symbol.
Pseudocode
Pseudocode is a simple method of showing an algorithm. It describes what the
algorithm does by using English key words that are very similar to those used in a
high level programming language. Data items to be processed by the algorithm are
given meaningful names in the same way that variables and constants are in a high-
level programming language. However, pseudocode is not bound by the strict
syntax rules of a programming language. It does what its name says, it pretends to
be programming code!
To ensure that pseudocode is easily understandable by others it is useful to be
consistent in the way that it is written.
The pseudocode for an assignment
statement
A value is assigned to an item/variable using the ←
operator. The variable on the left of the ← is assigned the value
of the expression on1.the
Basic Calculation:
right.
o Price = Cost + 2

 Adds 2 to the cost and assigns the result to Price.


2. Combined Operations:
o SellingPrice = Price + Tax

 Adds Tax to Price and assigns the result to Selling Price.


3. Conditional Assignments:
o Chosen = HasValue

 Assigns the value of Has Value to Chosen.


The pseudocode for conditional statements
There are two main conditional statements in pseudocode:
➢ IF … THEN … ELSE … ENDIF
The pseudocode for input and
output statements
INPUTS and OUTPUTS used when we want to
enter the data or display the answer on the
screen.
7.4 Standard methods of solution
Repeating existing methods is crucial in algorithm
design, as they may be executed thousands of times in
a program. Key standard methods include:
•Totalling
•Counting
•Finding maximum, minimum, and average values
•Linear searching
•Bubble sorting
7.4.2 Counting
7.4.1 Totalling Counting tracks how many times an action occurs, such as
Totalling involves maintaining a sum to which tallying the number of students who received a pass mark.
values are added, such as keeping a running
total of marks for each student in a class.

Counting is also used to count down until a certain value is


reached, for example, checking the number of items in stock in
a supermarket
7.4.3 Maximum, minimum and average
Finding the largest and smallest values in a list is a
common method in algorithms, such as identifying the
highest and lowest marks in a class.

Calculating the average (mean) of values in a list extends


the totalling method, such as finding the average mark for
a class.

If the largest and smallest values are unknown, set


them to the first item in the list. This method can be
used to find the highest and lowest marks in a class.
7.4.4 Linear search
A search checks if a value is in a list by
systematically examining each item. For
IGCSE Computer Science, you need to
know the linear search method, which
inspects each item to find a match. For
example, it can be used to search for a
name in a class list of unique student
names.
7.4.5 Bubble sort
Sorted lists are more useful, such as names in alphabetical order or temperatures in
ascending/descending order.
The bubble sort is a standard method to sort items. It compares each element with the
next, swapping them if they are in the wrong order. This continues until no swaps are
needed or only one element remains unchecked. For example, the bubble sort can sort a
list of ten temperatures in ascending order, represented in pseudocode as:
7.5 Validation
and verification
Range check
A range check checks that the value of a
number is between an upper value and a lower
value. For example, checking that percentage
marks are between 0 and 100 inclusive:

OUTPUT "Please enter the student's mark " REPEAT


INPUT StudentMark
IF StudentMark < 0 OR StudentMark > 100 THEN
OUTPUT "The student's mark should be in the range 0 to
100, please re-enter the mark "
ENDIF
UNTIL StudentMark >= 0 AND StudentMark <= 100
Length check
A length check checks either:
» that data contains an exact number of characters, for
example that a password must be exactly eight
characters in length so that passwords with seven or
fewer characters or nine or more characters would be
rejected, for instance:
OUTPUT "Please enter your password of eight characters”
REPEAT
INPUT Password
IF LENGTH(Password) <> 8
THEN
OUTPUT "Your password must be exactly eight characters, please
re-enter "
ENDIF UNTIL LENGTH(Password) = 8
or that the data entered is a reasonable number of characters,
for example,
a family name could be between two and thirty characters
inclusive so that names with one character or thirty-one or more
characters would be rejected.

OUTPUT "Please enter your family name " REPEAT


INPUT FamilyName
IF LENGTH(FamilyName) > 30 OR LENGTH(FamilyName) < 2
THEN
OUTPUT "Too short or too long,
please re-enter " ENDIF
UNTIL LENGTH(FamilyName) <= 30 AND
LENGTH(FamilyName)>= 2
Type check
A type check checks that the data entered is of a
given data type, for example, that the number of
brothers or sisters would be an integer (whole
number).

OUTPUT "How many brothers do you have? " REPEAT


INPUT NumberOfBrothers
IF NumberOfBrothers <> DIV(NumberOfBrothers, 1)
THEN
OUTPUT “ ERROR"
ENDIF
Presence check
A presence check checks to ensure that some data has
been entered and the value has not been left blank, for
example, an email address for an online transaction must
be completed.

OUTPUT "Please enter your email address "


REPEAT
INPUT EmailAddress
IF EmailAddress = "" THEN
OUTPUT "*=Required " ENDIF
UNTIL EmailAddress <> ""
7.4.1 Verification
TYPES USAGE

DOUBLE FOR DOUBLE ENTRY THE DATA IS ENTERED TWICE. THE


COMPUTER
ENTRY SYSTEM COMPARES BOTH ENTRIES AND IF THEY ARE
DIFFERENT OUTPUTS AN ERROR MESSAGE
REQUESTING THAT THE DATA IS ENTERED AGAIN.

SCREEN/VISUAL A SCREEN/VISUAL CHECK IS A MANUAL CHECK COMPLETED


BY THE USER WHO IS ENTERING THE DATA.
CHECK
7.5 Test Data
KEYWORDS MEANING

NORMAL DATA DATA THAT IS ACCEPTED BY A PROGRAM

ABNORMAL DATA DATA THAT IS REJECTED BY A PROGRAM

EXTREME THE LARGEST/SMALLEST DATA VALUE THAT IS


ACCEPTED BY A PROGRAM

BOUNDARY THE LARGEST/SMALLEST DATA VALUE THAT IS


ACCEPTED
BY A PROGRAM AND THE CORRESPONDING
SMALLEST/ LARGEST REJECTED DATA VALUE
for student, they usually take 0 to
100. Using this data we can find
Normal Data,
Abnormal data, Extreme and
Boundary
• Normal data → 65, 42 and 32 …
• Abnormal data → -13, -46 and 164 …
• Extreme → 0 and 100
7.7 Trace tables to document dry runs of algorithms
A structured approach is essential to determine an
algorithm's purpose. This involves recording and analyzing
results from each step using test data.

A Trace table records the results of each step in an algorithm, showing the value of variables as
they change. The manual process of going through the algorithm step by step is called a dry run.

Create a column for each variable.
•Add a column for output.
During a dry run:
•Enter a new variable value in the
trace table whenever it changes.
•Record output values in the output
column.

Test Data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5. The output shows that the algorithm selects
the largest and smallest numbers from a list of ten positive numbers. The same
trace table can be used if the algorithm is presented in pseudocode.

You might also like