Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Chapter 7 :Algorithm design and Problem Solving

Using 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.it is not written on the computer to run

• Variables and constants are given meaningful names .


• a non-proportional font is used throughout.
• all keywords (words used to describe a specific action e.g. INPUT) are written in
capital letters.
• all names given to data items and subroutines start with a capital letter
• where conditional and loop statements are used, repeated or selected statements
are indented by two spaces.
Chapter 7 :Algorithm design and Problem Solving
Using Pseudocode
A variable in a computer program is a named data store than contains a value that may
change during the execution of a program. In order to make programs understandable
to others, variables should be given meaningful names.

A constant in a computer program is a named data store than contains a value that
does not change during the execution of a program. As with variables,
The pseudocode statements

1. Assignments statements

2. Input/ Output Statements

3. Conditional Statements

4. Loop Statements
The pseudocode statements- Assignment statement
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 on the right.
Is equivalent to the processing shape in the flowchart .

Mathematical operators :

Example : Cost  10 Cost has the value 10


The pseudocode statements- Input and output statements

• Input and output statements are used to enter(Input)data and output(print )

results.

• For a program to be useful, the user needs to use prompt messages to know

what they are expected to input and explaining the result.

• If there are several parts to an output statement, then each part is

separated by a separator character.


The pseudocode statements(selection) - Conditional statements
Conditional statements : When different actions are performed by an algorithm according to the values
of the variables, conditional statements can be used to decide which action should be taken.
Is equivalent to Diamond shape in the flowchart .

There are two types of conditional statement:


1. a condition that can be true or false :
IF … THEN … ELSE … ENDIF IF
2. a choice between several different values:
CASE OF … OTHERWISE … ENDCASE
The Conditional statement(Selection ) IF … THEN … ELSE … ENDIF

• For an IF condition the THEN path is followed if the condition is true and
• ELSE path is followed if the condition is false.
• There may or may not be an ELSE path.
• The end of the statement is shown by ENDIF.

There are different ways that an IF condition can be set up:


1. Use of a Boolean variable that can have the value TRUE or FALSE
For example:
Found true
IF Found THEN OUTPUT "Your search was successful"
ELSE
OUTPUT "Your search was unsuccessful"
ENDIF
The pseudocode statements : Conditional: IF … THEN … ELSE … ENDIF
There are different ways that an IF condition can be set up:
2. Comparisons made by using comparison operators, where comparisons are made from
left to right, for example:
A > B means ‘A is greater than B’
Comparisons can be simple or more complicated, for example:
IF ((Height > 1) OR (Weight > 20)) AND (Age < 70) AND (Age > 5)
THEN
OUTPUT "You can ride“ Comparison operators
ELSE
OUTPUT "Too small, too young or too old"
ENDIF
Conditional statements(selection ) CASE OF … OTHERWISE …
ENDCASE
• For a CASE statement the value of the variable decides the path to be taken.
• Several values are usually specified.
• OTHERWISE is the path taken for all other values.
• The end of the statement is shown by ENDCASE.

Example : Have a look at the algorithm below that specifies what happens if the value of
Choice is 1, 2, 3 or 4.

CASE OF Choice
1 : Answer ← Num1 + Num2
2 : Answer ← Num1 - Num2
3 : Answer ← Num1 * Num2 4 : Answer ← Num1 / Num2
OTHERWISE
OUTPUT "Please enter a valid choice"
ENDCASE
Loop structures (iteration )
When some actions performed as part of an algorithm need repeating, this is called
‘iteration’.
Why using loops : Loop structures are used to perform the iteration.
There are three different types of loop structure It is important to know though that not all
loops work in the same way, and we are expected to know which loop to use and
when.
General Kinds of Repetition
Fixed repetition: (For Loop)
It can be determined in advance how many times a segment of code will be
repeated
The number of times the segment of code is repeated is independent of what
happens inside the loop

Variable repetition: (Repeat until , While Do)


It cannot be determined in advance how many times a segment of code will be
repeated
The value (true false) of the condition determining whether a segment of code will
be repeated or not changes as a result of what happens inside the loop
Loop structures(Iteration)-For Loop
1) FOR … TO … NEXT ( fixed loop)
• This is used in loops where the number of iterations is known before the loop starts (Definite iteration).
• A variable is set up with a start value and an end value and then incremented (by itself) in steps
of one until the end value is reached and the iteration finishes
• The variable can be used within the loop so long as its value is not changed.
• This type of loop is very useful for reading values into lists.

Example 1 : input and add 10 numbers ---- number of iterations is 10 times

Steps of using FOR :


- Define a variable call it Total to store the total and assign 0 to it Total  0
- Define a variable call it counter
- Counter starts from 1 till 10 FOR Counter  1 to 10
- The sequence in the loop :
input the number INPUT Num
add the number to the total Total  total + Num
- End the loop using the word NEXT NEXT Counter
Loop structures (Post-condition loops ):
REPEAT
2) REPEAT … UNTIL
• This loop structure is used when the number of repetitions/iterations is not known
• The actions are repeated UNTIL a given condition becomes true.
• The actions in this loop are always completed /iterate at least once.

Syntax:
REPEAT
<loop body>
UNTIL <termination condition>

• If termination condition evaluates to


true, loop exit occurs, and next program statement is executed
false, loop-body is repeated
Loop structures (Pre-condition loops ) :
WHILE
3) WHILE … DO … ENDWHILE

• This loop structure is used when the number of repetitions/iterations is not known
• The actions are only repeated WHILE a given condition is true.
• If the WHILE condition is False when the loop is first entered then the actions in
the loop are never performed.
• If condition evaluates to
false, loop exit occurs, and next program statement is executed
True , loop-body is repeated
Common Algorithms
– linear search
– bubble sort
– totaling
– counting
– finding maximum, minimum and average values
Common Algorithms- totaling and finding the average
Example : The flowchart shows an algorithm that should:
• allow 100 numbers to be entered into the variable Number
• total the numbers as they are entered
• output the total and average of the numbers after they have all been entered.
Variables :
• Counter to count 100 numbers must be initialized to 0
• Number : saves the input values
• Total : saves the total : initialize it to 0 and use the formula : Total  total + Number
• Average  Total / 100 OR Total /counter

Using REPEAT Loop:


Using FOR Loop: Using REPEAT Loop:
Total  0
Total  0 Total  0
Count0
FOR I1 TO 100 Count0
WHILE Count <=99 / Count <100 DO
INPUT Number REPEAT
INPUT Number
Total  Total + Number INPUT Number
Total  Total + Number
NEXT I Total  Total + Number
Count  Count +1
Avg  Total / 100 Count  Count +1
ENDWHILE
PRINT Total , Avg UNTIL Count=100/ Count>=100 / Count >99
Avg  Total / 100
Avg  Total / 100
PRINT Total , Avg
PRINT Total , Avg
Common Algorithms- totaling and finding the average

Example : The flowchart shows an algorithm that should:


• allow 100 numbers to be entered into the variable Number
• total the numbers as they are entered
• output the total and average of the numbers after they have all been entered.
Variables :
• Counter to count 100 numbers must be initialized to 0
• Number : saves the input values
• Total : saves the total : initialize it to 0 and use the formula : Total  total + Number
• Average  Total / 100 OR Total /counter

Using REPEAT Loop:


Using FOR Loop: Using REPEAT Loop:
Total  0
Total  0 Total  0
Count0
FOR I1 TO 100 Count0
WHILE Count <=99 / Count <100 DO
INPUT Number REPEAT
INPUT Number
Total  Total + Number INPUT Number
Total  Total + Number
NEXT I Total  Total + Number
Count  Count +1
Avg  Total / 100 Count  Count +1
ENDWHILE
PRINT Total , Avg UNTIL Count=100/ Count>=100 / Count >99
Avg  Total / 100
Avg  Total / 100
PRINT Total , Avg
PRINT Total , Avg
Common Algorithms : Finding Maximum and Minimum

Input 10 numbers ,and find the maximum and minimum values :


Using REPEAT Loop : Using WHILE Loop :
Using FOR Loop : COUNTER  0 COUNTER  0
INPUT Num INPUT Num INPUT Num
Highest  Num Highest  Num Highest  Num
Lowest  Num Lowest  Num Lowest  Num
FOR I  1 TO 9 REPEAT WHILE Counter <=8/Counter <9
INPUT Num INPUT Num INPUT Num
IF Num >= Highest THEN IF Num >= Highest THEN IF Num >= Highest THEN
Highest Num Highest Num Highest Num
ELSE ELSE ELSE
IF Num < Lowest THEN IF Num < Lowest THEN IF Num < Lowest THEN
Lowest  Num Lowest  Num Lowest  Num
ENDIF ENDIF ENDIF
ENDIF ENDIF ENDIF
NEXT I Counter  Counter +1 Counter  Counter +1
OUTPUT Highest,Lowest UNTIL Counter >=9/ ENDWHILE
Counter >8 / OUTPUT Highest, Lowest
Counter =9/
OUTPUT Highest, Lowest
Example : finding total, average , maximum and minimum,
Write an algorithm in pseudocode to:
• input 25 positive whole numbers less than 100
• find and output the largest number
• find and output the average of all the numbers
Example : finding maximum and minimum,
(a) Write an algorithm in pseudocode to input 500 positive whole numbers. Each number input must be less than 1000.
Find and output the largest number input, the smallest number input and the range (difference between the largest
number and smallest number).
(b) Describe how the algorithm could be changed to make testing less time-consuming.

0478/22/M/J/21
Example : Counting
Keeping a count of the number of times an action is performed is another standard method.
For example, counting the number of students that were awarded a pass mark:

PassCount ← 0
FOR Counter ← 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50 THEN
PassCount ← PassCount + 1
NEXT Counter

Write An algorithm in pseudocode to input 50 numbers. Count how many positive numbers and how many negative
numbers.

You might also like