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

Algorithm Design and problem solving2

Uploaded by

lstluqman11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Algorithm Design and problem solving2

Uploaded by

lstluqman11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

ALGORITHM DESIGN AND

PROBLEM SOLVING
Learning Objectives
 Select appropriate identifier names.
 Write programs in pseudocode using
input, process and output.
 Write pseudocode using assignment,
sequence, selection and repetition.
 Write pseudocode from a structured
English description and from
flowchart.
VARIABLES

This is a memory location in a program that stores a value


(values) that can change. Variables are needed in
programming to store and manipulate data.

NAMING CONVENTION FOR A VARIABLE

(1) Variable name must not contain any space

(2) Naming of a variable should reflect the variable’s use

(3) CamelCap naming convention should be used, e.g.


ThisScore
ASSIGNMENT
After naming a variable, then there should be assignment.
Assignment refers to getting a value for a variable in a
programming language.

Example:
Structured English Pseudocode Flowchart
SET
Set X to 12 X 12 X TO 12

Flowchart
INCREMENT
INCREMENT Y Y Y+1 Y
SEQUENCE: Execution of instructions in a predetermined order, one
after another. Under this we can have the following:

Updating a value:

Y 5
The value of Y can be updated (either to increase or decrease it as the
program instructs)

Incrementing the value of Y by 1, we have

Y 5+ 1

This means the value of Y now becomes 6


Copying a value

Example
Y 5
Copying the value of Y to Z, we have

Y Z

Z Y
Swapping two values
To swap the content of two variables, writing the pseudocode
like

Y Z
Z Y; is not correct
Because there could be issue of overwriting.

To avoid this, another variable called ‘TEMP’ can be created.


That is,
TEMP Y
Y Z
Z TEMP
Worked Example

Write a program that asks the user for a Fahrenheit


value. Convert that value to degree Celsius and
output it.

Formula Fahrenheit = 32 x (5/9) Celsius


Variable Needed Fahrenheit, Celsius
Algorithm of the question
Step 1: Enter the value of Fahrenheit
Step 2: Calculate the Celsius equivalent using
Celsius = F – 32 x (5/9)
Step 3: Print the result Celsius
Flowchart
Start

INPUT “Enter
Fahrenheit”
Fahrenheit

Celsius = Fahreheit – 32 *
(5/9)

OUTPUT Celsius

End
Pseudocode

INPUT “Enter the Fahrenheit value” Fahrenheit


Celsius F – 32 * (5/9)
OUTPUT “The Celsius value is:” Celsius
Class Exercise
Enter a distance in miles and output it in kilometer using the
formula

Km = Miles * 1.61

Express the solution to the above question in


i) Algorithm
ii) Pseudocode and
iii) Flowchart
LOGIC STATEMENTS
A logic statement in computing is a declarative statement that
evaluates to either true or false. This is a type of construct refers to as
Selection.

Selection means making decisions in a program based on conditions.


STRUCTURED PSEUDOCODE FLOWCHART
ENGLISH
A 5

IF A > B B 3
IF A IS GREATER THAN B THEN…
THEN...... ELSE…
ELSE…… ENDIF Y
A>B Do this
N

Do this
CONDITION (SELECTION
CONSTRUCT)
A Condition consists of at least one logic
proposition, that is, a statement that can be
either true or false.

Example:
IF A > B
THEN….
ELSE….
ENDIF
COMPARISON OPERATOR
Operator Comparison Meaning
= equal to
< less than
> greater than
<= less than or equal
to
>= greater than or
equal to
<> not equal to
Worked Example 1:
Write a program that takes an input age from the user and outputs
“You can vote” if the age is 18 or above, otherwise outputs “You
cannot vote yet”. Selection Construct

INPUT “Enter your age” Age


IF Age >= 18
THEN
OUTPUT “You can vote”
ELSE
OUTPUT “You cannot vote yet”
ENDIF
START

INPUT
Age

Age No
OUTPUT
>= You cannot
18 vote
Yes

OUTPUT
You can vote

END
TASK (CLASS EXERCISE)
A town has a bus service where passengers
under the age of 12 and over the age of 60
do not need to pay a fare.
i) Write the logic statement for free fares

(Pseudocode)
ii) Draw the flowchart for the above.
WORKED EXAMPLE 2
Question: Take three number as input and output the smallest
number.
e.g. N1 N2 N3
10 15 8

Pseudocode
INPUT Number 1 (N1)
INPUT Number 2 (N2)
INPUT Number 3 (N3)
IF N1 < N2
THEN
IF N1 < N3
THEN
OUTPUT N1
ELSE
OUTPUT N3
ENDIF
ELSE
IF N2 < N3
THEN
OUTPUT N2
ELSE
OUTPUT N3
ENDIF
ENDIF
START

INPUT
FLOWCHART N1

INPUT
N2

INPUT
N3

Y N1 Y N1 OUTPUT
< <
N2 N3
N1

N
N2
OUTPUT
<
N3 N3
N
OUTPUT
N2 Y
END
ASSIGNMENT
Take three numbers as input and
output the largest number.
Express this in Pseudocode and
flowchart
LOOP
Looping means continuous execution of a
command until a condition is met.

A looping programming allows the execution of a


set of instructions repeatedly until a specified
condition is met.

Two important loop statements are:


1) REPEAT
(statement to be repeated)
UNTIL condition
2) WHILE condition DO
(statement to be
executed)
ENDWHILE

You might also like