Lecture 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Study Session 3: Introduction to Problem Solving Methods II

Introduction
In the previous session, you were introduced to the various methods of solving computable
problems. As discussed in that session, you have been told that problem solving method requires
the development of algorithms as explained in the third step which is Solution Refinement. To
understand this concept better, you need to know more about algorithms.

In this study session, you will be introduced to the various approaches to problem solving,
Algorithm, Pseudocode and Flowchart, the various ways of representing an algorithm and also
its properties, differences between Pseudocode and Flowchart, the symbols used in designing
Flowcharts and the advantages and disadvantages of using a Flowchart

Learning Outcomes for Study Session 3


When you have studied this session, you should be able to:

3.1 Explain what you understand by an Algorithm and the various ways of representing an
algorithm and its properties and its desirable attributes

3.2 Explain what a Pseudocode is, its features, advantages and disadvantages.

3.3 Recognise the symbols used in designing Flowcharts, its rules and its advantages and
disadvantages.
3.1 What is an Algorithm?
An algorithm is a detailed step by step instruction towards solving a computable problem. It
involves a sequence of actions that helps you in solving a computable problem. These actions
can be expressed in human languages like English, Yoruba, Igbo e.t.c. The words used in these
instructions should not be ambiguous, should be precise and specific for all possible cases.
Algorithms should be applicable to a number of similar or related problems rather than a specific
instance.

In your own words, describe what you understand by an algorithm.

3.1.1 Ways of Representing an Algorithm


Algorithms can be represented in different ways. Hence, a particular problem can have hundreds
of algorithm but the most important feature is that it must provide a solution to the problem.

1. Verbal representation: In verbal representation, algorithms are expressed in words which can
be natural languages like English, Yoruba, Hausa, Igbo.

2. Algebraic representation: This involves the use of mathematical symbols and formulas.

3. Tabular representations involve the use of tables in listing the sequence of instructions needed
to solve a computable problem.

4. Hierarchical representation involves the use of a break-out diagram to represent the steps you
need in solving a computable problem.

5. Data-flow diagram: Here, the algorithm is shown as a set of boxes that show the actions to be
performed. These boxes are linked by lines showing how data flows from one action to another.
This is referred to as the flow of data.

6. Flowchart: This involves representing an algorithm in the form of a diagram with action boxes
linked by lines showing the order in which they are executed, or the sequence of actions. This is
referred to as the flow of control.
7. Flowblocks: Just like flowcharts, the algorithm is represented by action boxes. Instead of
connecting the boxes with lines, the flow of control is illustrated by stacking boxes on top of
each other, or by nesting boxes within other boxes.

8. Pseudocode: In Pseudocodes, the set of instructions are written as a mixture of natural


language and mathematical notation.

Box 3.1: Algorithm

An algorithm is a detailed step by step instruction towards solving a computable problem. It


involves a sequence of actions that helps you in solving a computable problem.

 As discussed in this study, what are the various ways of representing an algorithm?
 Read through the six various ways of representing an algorithm, identify the similarity (ies)
between them.

3.1.2 Properties of Algorithms


The major properties of any algorithms are indicated below:

Completeness: An effective and efficient algorithm must be complete; it must fully contain all
the steps that lead to solving a computable problem.

Unambiguous: A sequence of instructions is ambiguous if it has more than one interpretation;


hence, an effective algorithm must be specific and simple, having only one meaning.

Deterministic: An algorithm is deterministic if after following the detailed steps the solution to
the problem or task is achieved. Considering the example in Case Study 3.1, if after following
the steps listed in sequential order, you were able to reach your destination then the algorithm is
deterministic.

Finite: A good algorithm that has a beginning must have an end; hence, the finite property of an
algorithm stipulates that a good algorithm must terminate after a sequential number of steps. The
finite property does not only stipulate that an algorithm terminates after a finite number of steps
but also stipulate that the algorithm must use finite number of variables to achieve the desired
result.

Example 3.1

If you are to tell your friend the location of your Department in your university, the algorithm for
this task can be like this:

Step 1:START

Step 2: Board a bus going to Ogbomoso.

Step 3: Alight at Takie.

Step 4: If a taxi is available board a taxi else board a Motorcycle.

Step 5: Alight at LAUTECH main gate.

Step 6: locate the High rise building

Step 7: Go to the Ground floor

Step 8: You are already in Computer Science Department

Step 9: Stop

Considering Example 3.1, to show if the algorithm is complete, it must be able to direct anybody
to your department but if it stops at step 4, which is the LAUTECH main gate, then the algorithm
is incomplete.

3.1.3 Desirable Attributes of an Algorithm


It is possible for your algorithm to exhibit all the properties listed in Box 3.2 and still it is not
suitable to be used by a computer system. Hence, it is expected that an algorithm exhibit the
following attributes so as to be appropriate to be used by a computer system.

The desirable attributes of an algorithm are:


Usability: A good algorithm must be easy to use by any computer system; it must not be
complex of difficult to interpret. When writing your algorithm, try and avoid the use of
grammars, let it be explicit enough to be understood by anybody that come across it.

Generality: An algorithm that is designed to solve a particular problem must be able to solve
any other similar problem. For example, an algorithm to add variable a and b must be able to add
variable c and d.

Efficiency: A desirable attribute of any algorithm is efficiency; this attribute stipulates that an
algorithm must be able to perform the function for which it was designed.

Good Structure: An algorithm must be written in an acceptable format that suites the intended
programming language.

Robustness: A good algorithm must be robust. To a reasonable degree, it must be able to resist
or withstand failure when unexpected data are given to it.

Cost-Effectiveness: a good algorithm must be affordable, it must give the easiest and the
cheapest steps or route towards solving a problem or reaching a goal.

Pilot Question 3.1


1. In your own word, what do you understand by an algorithm?
2. Enumerate the various ways of representing an algorithm.
3. Highlight the properties of an algorithm.
4. Itemize the desirable attributes of an algorithm.

3.2 Pseudocodes
The two major ways of representing an algorithm involves the use of Pseudocodes and
flowcharts.
3.2.1 What is a Pseudocode?
Pseudocodes are nonstandard English like instructions needed to solve a problem. They are
English like because the logic statements are expressed using English sentences and phrases
compared to what is typically found in standard programming language syntax. Pseudocode
literarily contains two words which are Pseudo (which means false) and code (which means
instructions).

Box 3.2: PSEUDOCODE

Pseudocode is a simple and concise sequence of English-like instructions to solve a problem.

Pseudocodes are nonstandard because unlike programming languages which have strict rules
regarding keywords and syntax, there are no standards when it comes to pseudocodes, as long as
the statement is readable and defines what that logic step needs to accomplish, it is acceptable as
pseudo code.

3.2.2 Features of a Good Pseudocode


A good Pseudocode must have the features enumerated below:

i. The Pseudocode must always begin with the Start instruction and complete with the End
ii. instruction.
iii. Each Pseudocode statement should contain at least one instruction.
iv. Each Pseudocode statement should contain a verb that represents the action performed along
with any identifier and operators that hold program values or perform calculations

3.2.3 Advantages of using a Pseudocode


Some advantages of Pseudocodes are enumerated below:
i. A Pseudocode is a very simple and unstructured programming language.
ii. It is very easy to translate a Pseudocode into any formal programming language.
iii. It is very easy to read and understand a Pseudocode statement.

3.2.4 Disadvantages of using a Pseudocode


The disadvantages of pseudo code may start with its lack of standards. One person‟s logic
instructions may not be equal to another‟s as logical instruction. Hence, due to the unstructured
nature of pseudo code, it has few or no rules and is hard to standardize.

Example 3.2: Pseudocodes


Consider an electrician trying to replace burnt out box with new ones. To replace the burnt bulb,
he must first check if the bulb is burnt or not. If it‟s burnt, he replaces the bulb, else leaves the
bulb. To solve this problem, the electrician wrote down the Pseudocode and the flowchart he needs
in order to solve the problem. The Pseudocode for the problem is enumerated below:

1. Start
2. IF lamp works, go to step 7.
3. Check if lamp is plugged in.
4. IF not plugged in, plug in lamp.
5. Check if bulb is burnt out.
6. IF blub is burnt, replace bulb.
7. IF lamp doesn‟t work buy new lamp.
8. Quit ... problem is solved
9. Stop

Pilot Question 3.2


1. Explain what you understand by the word “Pseudocode”
2. Enumerate the features of a good Pseudocode
3. Highlight the advantages and disadvantages of a Pseudocode
3.3 Flowcharts
Another way to represent an algorithm is to use a flowchart. Flowchart gives a graphic,
diagrammatic or pictorial representation of the sequence of steps needed to solve a computable
problem.

Box 3.3: FLOWCHART

A FLOWCHART is a graphic representation of the steps required in solving a problem.

3.3.1 Flowchart Symbols


Flowchart uses some standardized set of symbols that represent different types of logic
operations. The symbol shapes are standardized and the way they are read (top to bottom) is also
standardized.

Rounded Rectangle: As shown in Figure 3.1 below, rounded rectangle is used to show the start
and finish of the logic flow. This symbol is called the terminator symbol.

START STOP

Figure 3.1: Start and Stop Symbols

Circle: The circle is used to connect drawings that span more than one piece of paper and help
to connect the logic paths from page to page. It is also called the connector symbol. The circle
shows the end of the drawing on that page and has a number inside it. The next page of the
diagram starts with a circle with that same number. This helps to tie the two pages together and
when the logic ends on one page, it should be picked up in the circle on the drawing for the next
page.
1

Figure 3.2: Connector Symbol

Rectangle: The rectangle is used to show a single step in the model logic. It is called a process
symbol. It is used to indicate that a computation (such as expression or calculation) is to be
carried out.

A= B + C

Figure 3.3: Process Symbol

Diamond: The diamond is used to represent a decision. The outcome may be true or true/false
and is usually implemented in program code as an IF statement.

Is A > B

Figure 3.4: Decision Symbol

Parallelogram: The parallelogram is used to represent a logic input or output (i.e. reading from
a keyboard or file or writing to a display or printer).

Figure 3.5: Input / Output Symbol

Lines: Lines show the movement of logic and the direction of information from symbol to
symbol.
Figure 3.6: Program Flow Symbol

3.3.2 Flowchart Rules


There are rules you need to take into consideration when developing a flowchart, the rules are
necessary for the flowchart to work and in other cases the standard is there so that other
programmers can look at the logic model and see the same logic solution. Some rules guiding the
use of flowcharts are enumerated below:

1. Each flowchart symbol must have one line coming into the symbol and one line going out of
the symbol. Decision symbol is the only exception because it can have two lines where one of
the lines represents the „YES‟ or „TRUE‟ condition while the remaining line represent the „NO‟
or „FALSE‟ condition.

2. Lines connecting flowchart symbols must never cross one another.

3. Every flowchart Symbol must be appropriately labelled with a short explanation giving more
information about that step.

3.3.3 Advantages of Flowcharts


Some advantages of flowcharts are enumerated below:

1. Communication: - Flowcharts offer a better way to communicating the logic of a system to all
that are concerned.

2. Effective analysis: - With the help of a flowchart, problem can be analysed in a more effective
way.

3. Proper documentation: - Program flowcharts serve as a good program documentation, which is


needed for various purposes.

4. Efficient Coding: - The flowchart serves as a guide or blueprint during the systems analysis
and program development phase.
5. Proper Debugging: - Flowchart helps in program debugging.

3.3.4 Disadvantages of Flowcharts

Some disadvantages of flowcharts are enumerated below:

1. Complex logic: - Sometimes, the program logic is quite complicated. In that case, flowchart
becomes complex and clumsy.

2. Alterations and Modifications: - If alterations are required the flowchart may require re-
drawing completely.

3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart


becomes a problem. The essentials of what is done can easily be lost in the technical details of
how it is done.

Considering the definitions of Pseudocode and flowchart given above, identify the major
similarities and differences between Pseudocode and flowchart.

Example 3.3

Consider an electrician trying to replace burnt out box with new ones. To replace the burnt bulb,
he must first check if the bulb is burnt or not. If it‟s burnt, he replaces the bulb, else leaves the
bulb. To solve this problem, the electrician wrote down the Pseudocode and the flowchart he needs
in order to solve the problem. The Flowchart for the problem is represented in the below:
Figure 3.7: Flowchart

Pilot Question 3.3

1. What do you understand by the word “Flowchart”


2. Enumerate the Flowchart symbols discussed in this study session.
3. Highlight the advantages and disadvantages of using a flowchart
Summary of Study Session 3
In this Study Session, you have learnt the following:

1. An algorithm is a detailed step by step instruction towards solving a computable problem.

2. An algorithm can be represented in different ways but on matter the number of ways, they are
aimed towards achieving a particular goal which is to solve a computable problem.

3. A good Algorithm must exhibit the following properties which are: completeness,
unambiguous, deterministic and finiteness.

5. The major attributes of a good Algorithm are usability, generality, efficiency, good structure,
robustness and cost effectiveness.

6. Pseudocodes are nonstandard English like instructions needed to solve a problem.

7. The major ways of representing an algorithm are Pseudocode and flowchart.

8. Flowchart gives a pictorial or graphical representation of an algorithm.

9. Flowchart has some standardized symbols used to represent various actions and these are
guided by several rules.

10. Both Pseudocode and flowchart represent steps involved in solving problems.

You might also like