0% found this document useful (0 votes)
15 views9 pages

LESSON 12 Algorithms

This document provides an introduction to algorithms, explaining their definition as step-by-step procedures for problem-solving and outlining techniques for representing them, such as pseudocode and flowcharts. It details the building blocks of algorithms, including sequences, conditionals, and iterations, with examples of each type. The lesson concludes by summarizing the key concepts learned about algorithms and their representation techniques.

Uploaded by

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

LESSON 12 Algorithms

This document provides an introduction to algorithms, explaining their definition as step-by-step procedures for problem-solving and outlining techniques for representing them, such as pseudocode and flowcharts. It details the building blocks of algorithms, including sequences, conditionals, and iterations, with examples of each type. The lesson concludes by summarizing the key concepts learned about algorithms and their representation techniques.

Uploaded by

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

INTRODUCTION

For effective development of a program, it is important that we plan exactly the various requirements,
operations and functions of the program. In this lesson, we will learn about algorithm and what it entails.

OBJECTIVES
By the end of the lesson
a) explain the concept of Algorithms.
b) explain the techniques used for representing them.
c) state the Algorithm building blocks.
d) Write the VB syntax for
i. If else
ii. Repeat until
iii. While loop
iv. For loop
Algorithms
Algorithm is a step-by-step procedure to solve a given problem. They are a set of instructions(method)
which if faithfully followed will produce a solution to a given problem.
Example 1: Design an algorithm for adding the test scores as given: 26, 49, 98, 87, 62, 75
Algorithm
1. Start
2. Sum = 0
3. Get the first testscore
4. Add first testscore to sum
5. Get the second testscore
6. Add to sum
7. Get the third testscore
8. Add to sum
9. Get the Forth testscore
10. Add to sum
11. Get the fifth testscore
12. Add to sum
13. Get the sixth testscore
14. Add to sum
15. Output the sum
16. Stop
Techniques for Representing Algorithms
 Pseudo code
 Flowcharts
 Actual code

Pseudo code
Pseudocode is one of the tools that can be used to write a preliminary plan that can be developed into a
computer program. In the algorithm design, the steps of the algorithm are written in free English text and,
although brevity is desired, they may be as long as needed to describe the particular operation. The steps of
an algorithm are said to be written in pseudocode. It is a readable description of what a computer
program or algorithm must do, expressed in a formally-styled natural language rather than in a
programming language.

Algorithm Building Blocks


All problems can be solved by employing any one of the following building blocks or their combinations.

1. Sequences
A sequence of instructions that are executed in the precise order they are written in:

statement block 1
statement block 2
statement block 3

Example 2: suppose you are required to design an algorithm for finding the average of six numbers, and
the sum of the numbers is given. The pseudocode will be as follows

Start
Get the sum Average = sum / 6
Output the average
Stop

Example 3: This is the pseudo-code required to input three numbers from the keyboard and output the
result.

Start
Use variables: sum, number1, number2, number3 of type integer
Accept number1, number2, number3
Sum = number1 + number2 + number3
Print sum
Stop
Example 4: The following pseudo-code describes an algorithm which will accept two numbers from the
keyboard and calculate the sum and product displaying the answer on the monitor screen.
Start
Use variables sum, product, number1, number2 of type real
display “Input two numbers”
accept number1, number2
sum = number1 + number2
print “The sum is “, sum
product = number1 * number2
print “The Product is “, product
Stop

2. Conditionals
Select between alternate courses of action depending upon the evaluation of a condition
If (condition = true)
statement block 1
Else
statement block 2
End if
Example 5: The following shows how the selection control structure is used in a program where a user
chooses the options for multiplying the numbers or adding them or subtracting.
Start
Use variables: choice, of the type character; ans, number1, number2, of type integer
display “choose one of the following”
display “m for multiply” display “a for add”
display “s for subtract”
accept choice
display “input two numbers you want to use”
accept number1, number2
if choice = m then
ans = number1 * number2
if choice = a then
ans = number1 + number2
if choice = s then
ans = number1 - number2
display ans
Stop
Example 6: The program is to input an examination mark and test it for the award of a grade. The mark is
a whole number between 1 and 100. Grades are awarded according to the following criteria:
>= 80 – Distinction
>= 60 – Merit
>= 40 – Pass
< 40 - fail

The pseudo-code is
Start
Use variables: mark of type integer
If mark >= 80
display “distinction”
If mark >= 60 and mark < 80
display “merit”
If mark >= 40 and mark < 60
display “pass”
If mark < 40
display “fail”
End

An if statement on its own is often not the best way of solving problems. A more elegant set of conditions
can be created by adding an else statement to the if statement. The else statement is used to deal with
situations as shown in the following examples.

Example 7: A person is paid at top for category 1 work otherwise pay is at normal rate.
If the work is category 1
pay-rate is top
Else
pay-rate is normal
3. Repetition or Iteration Structure
A third structure causes the certain steps to be repeated.
The Repetition structure can be implemented using
 Repeat Until Loop
 The While Loop
 The For Loop

Any program instruction that repeats some statement or sequence of statements a number of times is called an
iteration or a loop. The commands used to create iterations or loops are all based on logical tests. There three
constructs for iterations or loops in our pseudo- code.
The Repeat Until loop or Do While loop.
The syntax is
REPEAT
A statement or block of statements
UNTIL a true condition

Example 8: A program segment repeatedly asks for entry of a number in the range 1 to 100 until a valid
number is entered.
REPEAT
DISPLAY “Enter a number between 1 and 100”
ACCEPT number
UNTIL number < 1 OR number > 100
Example 9. A survey has been carried out to discover the most popular sport. The results will be typed into
the computer for analysis. Write a program to accomplish this.
REPEAT
DISPLAY “Type in the letter chosen or Q to finish”
DISPLAY “A: Athletics”
DISPLAY “S: Swimming”
DISPLAY “F: Football”
DISPLAY “B: Badminton”
DISPLAY “Enter data”
ACCEPT letter
If letter = ‘A’ then
Athletics = athletics + 1
If letter = ‘S’ then
Swimming = Swimming + 1
If letter = ‘F’ then
Football = Football + 1
If letter = ‘B’ then
Badminton = Badminton + 1
UNTIL letter = ‘Q’
DISLAY “Athletics scored”, athletics, “votes”
DISLAY “Swimming scored”, swimming, “votes”
DISLAY “Football scored”, football, “votes”
DISLAY “Badminton scored”, Badminton, “votes”

The WHILE Loop


The second type of iteration we will look at is the while iteration. This type of conditional loop tests for
terminating condition at the beginning of the loop. In this case no action is performed at all if the first test
causes the terminating condition to evaluate as false.
The syntax is
WHILE (a condition is true)
A statement or block of statements
ENDWHILE
Example 10: A program segment to print out each character typed at a keyboard until the character ‘q’ is
entered.
WHILE letter <> ‘q’ ACCEPT letter
DISPLAY “The character you typed is”, letter
ENDWHILE

Example 11: Write a program that will output the square root of any number input until the number input is
zero.
In some cases, a variable has to be initialized before execution of the loop as shown in the following
example.
Use variable: number of type real
DISPLAY “Type in a number or zero to stop” ACCEPT number
WHILE number <> 0
Square = number * number
DISPLAY “The square of the number is”, square
DISPLAY “Type in a number or zero to stop”
ACCEPT number
ENDWHILE

The FOR Loop


The third type of iteration, which we shall use when the number of iterations is known in advance, is a for
loop. This, in its simplest form, uses an initialization of the variable as a starting point, a stop condition
depending on the value of the variable. The variable is incremented on each iteration until it reaches the
required value.
The pseudo-code syntax will be:
FOR (starting state, stopping condition, increment)
A statement or block of statements
ENDFOR
Example 12.
FOR (n = 1, n <= 4, n + 1)
DISPLAY “loop”, n
ENDFOR
The fragment of code will produce the output
Loop 1
Loop 2
Loop 3
Loop 4
In the example, n is usually referred as the loop variable, or counting variable, or index of the loop. The loop
variable can be used in any statement of the loop. The variable should not be assigned a new value within the
loop, which may change the behaviour of the loop.

Example 13: Write a program to calculate the sum and average of a series of numbers. The pseudo-code
solution is:
Use variables: n, count of the type integer
Sum, number, average of the type real
DISPLAY “How many numbers do you want to input”
ACCEPT count
SUM = 0
FOR (n = 1, n <= count, n + 1)
DISPLAY “Input the number from your list”
ACCEPT number
SUM = sum + number
ENDFOR
Average = sum / count
DISPLAY “The sum of the numbers is “, sum
DISPLAY “Average of the numbers is “, average

Example 14: Design an algorithm and the corresponding flowchart for finding the sum of n numbers.
Pseudocode Program
START
Sum = 0
Display “Input number n”
Input n
FOR (i = 1, i<=n, i=i+1)
Display “Input a value”
Accept value
Sum = sum + value
ENDFOR
Output sum
STOP
In this example, we have used i to allow us to count the numbers, which we get for the addition. We compare
I with n to check whether we have exhausted the numbers or not in order to stop the computation of the sum
(or to stop the iteration structure). In such a case, i is referred to as a counter.
CONCLUSION
In this lesson, we have learnt that,
a. Algorithm is a step-by-step procedure to solve a given problem
b. Techniques for Representing Algorithms
i. Pseudo code
ii. Flowcharts
iii. Actual code
c. Pseudo code is a readable description of what a computer program or algorithm must do, expressed in
a formally-styled natural language rather than in a programming language.
d. Algorithm building block are
i. Sequences
ii. Conditionals
iii. Iteration / repetitions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy