You are on page 1of 57

Algorithms

Problem Solving
Topic & Structure of the lesson
Design

In this chapter you will learn about:


• Problem Solving
• Algorithm
• Pseudocodes
• Flowcharts

PSPD Using C Slide 2 of 40


Algorithmic Problem Solving
Design

Algorithmic problem:
Any problem whose solution can be expressed as a
set of executable instructions.

Algorithm:
A well defined computational procedure consisting of
a set of instructions, that takes some value or set of
values, as input, and produces some value or set of
values, as output.

PSPD Using C Slide 3 of 40


Characteristics of an Algorithm
Design

 
 
 Each step of an algorithm must be exact,
  preciously and ambiguously described.
 It must terminate, i.e. it contains a finite
number of steps.
 It must be effective, i.e.., produce the correct
output.
 It must be general, i.e.. to solve every
instance of the problem.

PSPD Using C Slide 4 of 40


Problem Solving
Design

Processing
5 10
5 + 10 = 15

15

Input
Output
Let us assume we are interested in calculating the sum of 5
and 10.
PSPD Using C Slide 5 of 40
Problem Solving
Design

As shown previously, the example values (5 and


10) have been specified explicitly.
As the brain is flexible enough in calculating a
wide range of numbers, the two input values
have to be generalised.

PSPD Using C Slide 6 of 40


What Are Variables?
Design

Variables are memory locations within the computer which


allows pieces of data to be stored.

The word variable comes from the word vary, which means
that whatever you place within a variable can be changed.

A variable can be viewed as a container used to store


things.

Data (for example, name,


age, salary) can be stored in
these containers.
PSPD Using C Slide 7 of 40
What Are Variables?
Design

PSPD Using C Slide 8 of 40


Problem Solving
Design

Now that we have an exact idea about how the


problem is solved, let us represent this in a clearer
manner, using the defining diagram.

Input Processing Output

Value1 Sum
Value2

PSPD Using C Slide 9 of 40


Problem Solving
Design

The next step is to identify the actual processing


steps required to convert the input to become the
output.

Input Processing Output

Value1 1) Read Value1, Value2 Sum


Value2 2) Calculate Sum
3) Display Sum

PSPD Using C Slide 10 of 40


Algorithm Development
Design
Once the defining diagram has been
developed, the next logical step is to develop
the algorithm (which is much more detailed).

Input Processing Output

Value1 1) Read Value1, Value2 Sum


Value2 2) Calculate Sum
3) Display Sum

The developed processing steps have to be more


detailed in the algorithm.
PSPD Using C Slide 11 of 40
Algorithm Development
Design
The basic mathematical operators used in algorithms
are as follows:-

+ addition
- subtraction
* multiplication
/ division
= assignment
() brackets for grouping calculations

PSPD Using C Slide 12 of 40


Algorithm Development
Design

Example of an algorithm (using pseudocodes) which


can be used to carry out the tasks outlined in the
defining diagram is as follows:-

1) Read Value1, Value2


2) Calculate
Sum = Value1 + Value2
3) Display Sum
PSPD Using C Slide 13 of 40
Pseudocoding
Design

 
A Pseudocode language is semiformal, English-
  like language with a limited vocabulary that can be
  used to design and describe algorithms.

The pseudocode language can be used for:


 Designing algorithms
 Communicating algorithms as programs
 Implementing algorithms as programs
 Debugging logic errors in program

PSPD Using C Slide 14 of 40


Pseudocode for the Control Structures
Design

 
  The Sequence Control Structure:
  The sequence control structure is a series of steps
or statements that are executed in the order in which
they are written in an algorithm.
For Example:
read taxable income
read filing status
compute income tax

PSPD Using C Slide 15 of 40


Cont’d
Design

 The Selection Control Structure:


 The selection control structure defines two courses of
action, depending on the outcome of a condition. A
 condition is an expression that, when evaluated, computes
to either true or false.
Syntax is: if condition
then-part
else
else-part
end-if

PSPD Using C Slide 16 of 40


Decision Making
Design

The expression is a comparison between


two values which evaluates to either true of
false.

IF (expression) THEN
:
:
ENDIF
Statements are
placed here.
PSPD Using C Slide 17 of 40
Decision Making
Design

Example:-
We are looking for a job which pays more than
RM4000.

Example of an
Expression

IF (Salary>4000) THEN
Say "I Will Take The Job!!"
ENDIF

PSPD Using C Slide 18 of 40


Decision Making
Design

Commonly used relational operators in expressions:-

> Greater Than


< Less Than
= Equals To
<> Not Equals To
>= Greater Than or Equals To
<= Less Than or Equals To
() Brackets used for prioritising certain calculations

PSPD Using C Slide 19 of 40


Decision Making
Design

Since all expressions works out to be either true or false,


what the IF..THEN statement represents is a two-state
condition.
For example,
A potential employer is waiting for you to give a reply (on the
spot) about the job offer with a salary of RM2000. Your
decision would be to only take a job worth more than
RM4000. What would you say?
IF (Salary>4000) THEN
Say “YES!”
ELSE
Say “NO!”
ENDIF
PSPD Using C Slide 20 of 40
Decision Making
Design

Certain conditions may give rise to more than


one expression being evaluated. These are
known as compound expressions.
Example:-
You are interested in taking up a job which pays
more than RM4000 and that the company must
also provide a credit card.
IF (Salary>4000) And (CreditCard=YES) THEN
Take Job!!
ENDIF

PSPD Using C Slide 21 of 40


Decision Making
Design

Compound expressions can be represented


using the following operators:-

AND Every expression must evaluate to be


true in order for the whole expression to
be true.
OR As long as any one of the expression
can be true, the entire IF statement will
be true.
NOT The inverse (opposite) of the entire
expression.
PSPD Using C Slide 22 of 40
Decision Making
Design

IF statements can be nested, that is, placed


within another IF statement.
This is used in situations when the expression
is more complex than the simple decisions (as
seen earlier).

PSPD Using C Slide 23 of 40


Decision Making
For example, this statement......... Design

IF (Salary>4000) And (CreditCard=YES) THEN


Say “Yes I Will Take The Job!!”
ENDIF
can be represented like this.........
IF (Salary>4000) THEN
IF (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”
ELSE
Say “No Credit Card?”
Say “Sorry!!”
ENDIF
ELSE
Say “Not Enough Pay!!”
ENDIF
........ whereby more possibilities can be represented.
PSPD Using C Slide 24 of 40
Decision Making
Design
For good practice...........
IF (Salary>4000) THEN
IF (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”
ELSE
Say “No Credit Card?”
Say “Sorry!!”
ENDIF
ELSE
Say “Not Enough Pay!!”
ENDIF

........ ensure that statements are properly


indented to indicate block of statements
which belong together.
PSPD Using C Slide 25 of 40
Cont’d
Design

 
  For Example:
 
if a is greater than b then
print “A is greater”
else
print “B is greater”
end if

PSPD Using C Slide 26 of 40


Cont’d
Design

 Repetition Control Structure:


 The repetition control structure specifies a block of
 one or more statements that are repeatedly executed
until a condition is satisfied.
Syntax is:
while condition
loop-body
end-while

PSPD Using C Slide 27 of 40


Looping Constructs
Design

Looping constructs (also known as repetition or


iteration constructs) are a kind of construct found
in pseudocodes which allows statements (or a
group of statements) to be repeated.
The main reason why looping constructs are
provided is because most of the problems which
we encounter everyday requires some degree of
repetition.

PSPD Using C Slide 28 of 40


Looping Constructs
Design

An example of a process which is iterative:-

Payroll processing is very much an iterative


process as the person processing the
payroll applies the same calculations for
each employee to produce the pay slip.

PSPD Using C Slide 29 of 40


Looping Constructs
Design

The looping constructs available in pseudocodes


are as follows:-

DOWHILE...ENDDO
FOR…NEXT
REPEAT...UNTIL

PSPD Using C Slide 30 of 40


Looping Constructs
Design

The format of the


DOWHILE...ENDDO construct is
shown below:-
DOWHILE (expression)
:
:
:
ENDDO
Group of An expression which determines
statements whether the loop will continue.
PSPD Using C Slide 31 of 40
Looping Constructs
Design

The format of the FOR...NEXT


construct is shown below:-

FOR (initialze TO expression) STEP increment


:
:
:
ENDDO
Group of An expression which determines
statements whether the loop will continue.
PSPD Using C Slide 32 of 40
Looping Constructs
Design

The format of the


REPEAT...UNTIL construct is
shown below:-

REPEAT
:
:
:
UNTIL (expression)
Group of An expression which determines
statements whether the loop will continue.
PSPD Using C Slide 33 of 40
Looping Constructs
Design

Take a look at the following example:-

You are required to develop a complete system


which will allow the total payroll to be
calculated.
The system is required to read in the amount to
be paid for each employee.
The moment the system receives an input
value of -99, the system is required to stop and
display the total payroll.
PSPD Using C Slide 34 of 40
Looping Constructs
Design

The Defining Diagram


Input Processing Output

Salary 1) Read Salary Total


2) Calculate Total
3) Display Total

PSPD Using C Slide 35 of 40


Looping Constructs
Design
Algorithm (Using Pseudocodes)
1) Display "Enter Salary"
2) Read Salary
3) Total = 0
4) DOWHILE (Salary<>-99)
Total = Total + Salary
Display "Enter Salary"
Read Salary
ENDDO
5) Display "Total Payroll = ", Total
PSPD Using C Slide 36 of 40
Cont’d
Design

 
  Example:
 
Dowhile (income is less than 50000)
print “Enter taxable income;should be
greater than or equal to 50000”
read income
Enddo

PSPD Using C Slide 37 of 40


Flowcharting
Design

 
  Another technique used in designing and representing
algorithms.
 
 Alternative to pseudocoing
 A pseudocode description is verbal, a flowchart is
graphical in nature.

Definition:
 A flowchart is a graph consisting of geometrical shapes
that are connected by flow lines.

PSPD Using C Slide 38 of 40


Sequence Structure
Design

 
Pseudocode: Flowchart:
 
statement_1
  Statement -1
statement_2
------------ Statement -2
statement_n

Statement -n

PSPD Using C Slide 39 of 40


Selection Structure
Design

 
Pseudocode: Flowchart:
 
if condition
  then-part
else false true
condition
else-part
end_if
else-part then-part

PSPD Using C Slide 40 of 40


Selection Structure
Design

 
Pseudocode:
  Flowchart:

 
if condition
Y
then-part true
end_if condition

N
then-part
false

PSPD Using C Slide 41 of 40


Repetition Structure
Design

Pseudocode: Flowchart:

while condition
loop-body
end-while T
Y
condition loop-body
F
N

PSPD Using C Slide 42 of 40


Summary of Main Teaching Points
Design

• Problem Solving

• Pseudocodes
• Flowcharts

• Basic control structures

• The sequence structure


• The selection structure
• The repetition structure

PSPD Using C Slide 43 of 40


Example 1
Design

• A town contains 5000 houses. Each


house owner must pay tax based on the
value of the house. Houses over $200
000 pay 2% of their value in tax, houses
over $100 000 pay 1.5% of their value in
tax and houses over $50 000 pay 1% of
their value in tax. All others pay no tax.
Write an algorithm to solve this problem in
the form of a flowchart.

PSPD Using C Slide 44 of 40


Design

PSPD Using C Slide 45 of 40


Writing algorithms using pseudocode
Design
• for count = 1 to 5000
• input house
• if house > 50 000 then tax = house * 0.01
• else if house > 100 000 then tax = house *
0.015
• else if house > 200 000 then tax = house * 0.02
• else tax = 0
• print tax
• next

PSPD Using C Slide 46 of 40


Example 2
Design

• The following formula is used to calculate


n: n = (x * x)/(1 – x). The value x = 0 is
used to stop the algorithm. The
calculation is repeated using values of x
until the value x = 0 is input. There is also
a need to check for error conditions. The
values of n and x should be output. Write
an algorithm to show this repeated
calculation in the form of a flowchart.

PSPD Using C Slide 47 of 40


Design

PSPD Using C Slide 48 of 40


Writing algorithms using pseudocode
Design

• input x
• while x <> 0 do
• if x = 1 then print “error”
• else n = (x * x)/(1 – x)
• print n, x
• endif
• input x
• endwhile

PSPD Using C Slide 49 of 40


Example 3
Design

• Write an algorithm in the form of a


flowchart which takes temperatures input
over a 100 day period (once per day) and
outputs the number of days when the
temperature was below 20C and the
number of days when the temperature was
20C and above.

PSPD Using C Slide 50 of 40


Design

PSPD Using C Slide 51 of 40


Writing algorithms using pseudocode
Design

• total1 = 0: total2 = 0
• for days = 1 to 100
• input temperature
• if temperature < 20 then total1 = total1 + 1
• else total2 = total2 + 1
• endif
• next
• print total1, total2
PSPD Using C Slide 52 of 40
Writing algorithms using pseudocode
Design

• case temperature of
• 1: total1 = total1 + 1
• 2: total2 = total2 + 1
• endcase

PSPD Using C Slide 53 of 40


Example 4
Design
• A shop sells books, maps and magazines. Each item is
identified by a unique 4 – digit code. All books have a
code starting with 1, all maps have a code starting with 2
and all magazines have a code starting with 3. The code
9999 is used to end the algorithm.

• Write an algorithm in the form of a flowchart which


inputs the codes for all items in stock and outputs the
number of books, number of maps and the number of
magazines in stock. Include any validation checks
needed.

PSPD Using C Slide 54 of 40


Design

PSPD Using C Slide 55 of 40


Writing algorithms using pseudocode
• books = 0: maps = 0: mags = 0 Design

• repeat
• input code
• if code > 999 and code < 2000 then books = books + 1
• else if code > 1999 and code < 3000 then maps = maps + 1
• else if code > 2999 and code < 4000 then mags = mags + 1
• else print “error in input”
• endif:endif:endif
• until code = 9999
• print books, maps, mags

PSPD Using C Slide 56 of 40


Writing algorithms using pseudocode
• books = 0: maps = 0: mags = 0 Design

• repeat
• input code
• x = INT(code/1000) * divides code by 1000 to give a
• case x of * number between 0 and 9
• 1: books = books + 1
• 2: maps = maps + 1
• 3: mags = mags + 1
• otherwise print “error”
• endcase
• until code = 9999
• print books, maps, mags

PSPD Using C Slide 57 of 40

You might also like