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

(dou-csc111/csc 113)

Lecture Two – Concepts of problem solving processes


Faculty of computing

Department of computer science

Dennis osadebay university, anwai

Asaba, delta state

1
Computer programming i

 Specific Objectives
 Understand problem solving using algorithm 2

 Understand the roles algorithm in problem solving


Specific  Understand problem solving using flowchart
objectives  Understand problem solving using pseudocode
 Discuss decision tree and decision table
 Understand implementation strategies

CSC 113
Computer programming i
 What is an Algorithm?
 An algorithm is a well-defined computational procedures consisting of a set of instructions that takes
3
some values or set of values as input and produces some values or set of values as output.
 An algorithm is a procedure consisting of a finite set of steps which specifies a finite sequence of
operations that provides the solution to a problem.
 In other word, an algorithm is a procedure that accepts data, manipulate them following the
prescribed steps so as to eventually fill the required unknown with the desired value(s).

Activities or Specific
Input steps output

CSC 113
Computer programming i
 Properties of an Algorithm
An algorithm possess the following properties:
4
 Finiteness: An algorithm must terminate in a finite number of steps
 Definiteness: Each step of the algorithm must be precisely and unambiguously stated
 Effectiveness: Each step must be effective, in the sense that it should be primitive easily convert able
into program statement) can be performed exactly in a finite amount of time.
 Generality: The algorithm must be complete in itself so that it can be used to solve problems of a
specific type for any input data.
 Input/output: Each algorithm must take zero, one or more quantities as input data produce one or
more output values. An algorithm can be written in English like sentences or in any standard
representation sometimes, algorithm written in English like languages are called Pseudo Code

CSC 113
Computer programming i

 Example of an algorithm
5
Suppose we want to find the average of three numbers, the algorithm is as follows:
 Step 1: Read the numbers A, B and C
 Step 2: Compute the sum of A, B and C
 Step 3: Divide the sum by 3
 Step 4: Store the result in variable Result
 Step 5: Print the value of Result
 Step 6: End the program

CSC 113
Computer programming i

 Example of an algorithm
Write an algorithm to calculate the simple interest using the formula. 6

Simple interest = P*N* R/100.


Where P is principle Amount, N is the number of years and R is the rate of interest.
 Step 1: Read the three input quantities’ P, N and R.
 Step 2 : Calculate simple interest as
 Simple interest = P* N* R/100
 Step 3: Print simple interest.
 Step 4: Stop.

CSC 113
Computer programming i

 Example of an algorithm
7
Write an algorithm to calculate the perimeter and area of rectangle. Given its length and width.
Step 1: Read length of the rectangle.
Step 2: Read width of the rectangle.
Step 3: Calculate perimeter of the rectangle using the formula:
perimeter =2* (length + width)
Step 4: Calculate area of the rectangle using the formula area = length*width.
Step 5: Print perimeter.
Step 6: Print area.
Step 7: Stop

CSC 113
Computer programming i

 What is a flowchart?
 A flowchart is a step by step diagrammatic representation of the logic paths to solve a given 8
problem.
 Or A flowchart is visual or graphical representation of an algorithm.
 The flowcharts are pictorial representation of the methods to be used to solve a given problem and
help a great deal to analyze the problem and plan its solution in a systematic and orderly manner.
 A flowchart when translated in to a proper computer language, results in a complete program.

CSC 113
Computer programming i

 Uses of flowchart
9
 The major use of flowcharts is in documentation and in programming. As a documentation
device, the flowchart provides a way of communicating, from one person to another, the nature of
the operation to be performed and of the data upon which it is to be performed, regardless of the
programming language or computer used.
 Programming aid, flowcharts are often prepared by systems analysts and designers to describe
systems and to specify the work to be accomplished by programs. Programmers use flowcharts as
a basis for writing programs and as a means of communicating among each other, particularly
when the programming is done as a team.

CSC 113
Concepts of Problem Solving

 Advantages of flowcharts
Advantages of Flowcharts 10

 The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates easier
checking of an algorithm.
 The Flowchart is good means of communication to other users. It is also a compact means of recording
an algorithm solution to a problem.
 The flowchart allows the problem solver to break the problem into parts. These parts can be connected to
make master chart.
 The flowchart is a permanent record of the solution which can be consulted at a later time.

CSC 113
Computer programming i
 Differences between flowchart and algorithm
Algorithm Flowchart 11
A method of representing the step-by-step logical Flowchart is diagrammatic representation of an
procedure for solving a problem algorithm. It is constructed using different types of
boxes and symbols.
It contains step-by-step English descriptions, each The flowchart employs a series of blocks and
step representing a particular operation leading to arrows, each of which represents a particular step
solution of problem in an algorithm

These are particularly useful for small problems These are useful for detailed representations of
complicated programs

For complex programs, algorithms prove to be For complex programs, Flowcharts prove to be
Inadequate adequate

CSC 113
Computer programming i
 Symbols used in a flowchart
The symbols that we make use while drawing flowcharts as given below are as per conventions followed by 12
International Standard Organization (ISO)
Oval: Rectangle with rounded sides is used to indicate either START/STOP of the program

Input and output indicators: Parallelograms are used to represent input and output operations. Statements
like INPUT, READ and PRINT are represented in these Parallelograms.

CSC 113
Computer programming i
 Symbols used in a flowchart
13
Process Indicators: - Rectangle is used to indicate any set of processing operation such as for storing
arithmetic operations.

Decision Makers: The diamond is used for indicating the step of decision making and therefore known
as decision box. Decision boxes are used to test the conditions or ask questions and depending upon the
answers, the appropriate actions are taken by the computer. The decision box symbol is

CSC 113
Computer programming i
 Symbols used in a flowchart
14
Flow Lines: Flow lines indicate the direction being followed in the flowchart. In a Flowchart, every line
must have an arrow on it to indicate the direction. The arrows may be in any direction

On- Page connectors: Circles are used to join the different parts of a flowchart and these circles are called
on-page connectors. The uses of these connectors give a neat shape to the flowcharts. Ina complicated
problems, a flowchart may run in to several pages. The parts of the flowchart on different

CSC 112
Computer programming i
 Symbols used in a flowchart
Off-page connector: This connector represents a break in the path of flowchart which is too large to fit on15
a single page.
It is similar to on-page connector.
The connector symbol marks where the algorithm ends on the first page and where it continues on the
second.

CSC 113
Computer programming i
 Example of flowchart
Draw a flowchart for adding the integers from 1 to 100 and to print the sum. 16

Start

SUM = 0
N=1

Yes
N > 100 PRINT SUM, N

No

SUM =Sum + N STOP


N = N+1

CSC 113
Computer programming i
 What is Pseudocode?
 Pseudocode is a method of describing computer algorithms using a combination of natural 17

language and programming language.


 The Pseudo code is neither an algorithm nor a program. It is an abstract form of a program.
 It consists of English like statements which perform the specific operations. It is defined for an
algorithm.
 It does not use any graphical representation.
 In pseudo code, the program is represented in terms of words and phrases, but the syntax of
program is not strictly followed.

CSC 113
Computer programming i
 Advantages of Pseudocode
 The Advantages of pseudo code are: 18

 Easy to read,
 Easy to understand,
 Easy to modify.

CSC 113
Computer programming i
 Pseudocode Rules
In general, here are some rules that are frequently followed when writing pseudocode: 19
 The usual programming symbols are used for arithmetic operations (e.g. +, -, *, / , **).
 Symbolic names are used to indicate the quantities being processed.
 Certain language keywords can be used, such as PRINT, WRITE, READ, etc.
 Indentation should be used to indicate branches and loops of instruction.

CSC 113
Computer programming i
 Pseudocode Rules - Example
2
Write a pseudocode to perform the basic arithmetic operations.
0
 Read n1, n2
 Sum = n1 + n2
 Diff = n1 – n2
 Mult = n1 * n2
 Quot = n1/n2
 Print sum, diff, mult, quot
 End.

CSC 113
Computer programming i
 Decision Tree and Table
21
 Decision tree is a graphical representation of every possible outcome of a decision.
 It is a tree like model that acts as a decision support tool, visually displaying decision, and their
potential outcomes, consequences and costs.
 A decision tree is a graph that always uses a branching method to demonstrate all the possible
outcome of any decision.
 It helps in processing logic involved in decision making, and corresponding actions are taken.
 It is a diagram that shows conditions and alternative actions within a horizontal tree framework.
It helps the analyst consider the sequence of decisions and identifies the accurate decision that
must be made.

CSC 113
Computer programming i
 Symbols of Decision Tree
Decision node 22
This indicates a situation when a decision has to be made and is represented by a closed square. This is how most
of the decision tree diagrams start.

Chance node
Whenever we are uncertain about the outcome of a situation, we represent it with a chance node. It can lead to
multiple outcomes, but mostly it is recommended to just lead to two results at once. It is represented by a circle.

End node
These are the leaf nodes that represent the end of the decision diagram. No further branches are expected from an
end node. A small triangle is used for a terminator or end node.

CSC 113
Computer programming i
 Symbols of Decision Tree
Branches 23
Every time a decision is made, it leads to different nodes. A branch would connect these nodes and represent a
situation or a result. Mostly, the result is written over the branch in normal text.

Rejected option
This is an optional symbol in a decision tree which is drawn after the entire diagram has been created. We simply
mark it over the branch of the option that we wish to no longer continue.

CSC 113
Computer programming i
 Decision Tree and Table
24

CSC 113
Computer programming i
 Decision Table
25
 Decision table is a tabular representation of conditions and actions in a decision tree.
 Decision tables are derived from decision tree.
 A decision table consists of rows and columns that are used to indicate conditions and actions in a
simplified and orderly manner.
 A decision table shows the decision-making logic and corresponding actions taken in a tabular or a
matrix form.
 It is a diagram of all the logic and possible outcomes associated with a particular process.
 It contains condition, condition alternatives and actions. The structure of a decision table is:

CSC 113
Computer programming i
 Decision Table
26

 The upper rows of the table specify the variables or conditions to be evaluated and the lower rows
specify the actions to be taken when an evaluation test is satisfied.
 A column in the table is called a rule.
 A rule implies that if a certain combination of conditions is true, then the corresponding actions is
executed.

CSC 113
Computer programming i
 Decision Table
 Step 1: Determine the number of conditions that may affect the decision. Combine rows that overlap, for example, conditions 27
that are mutually exclusive. The number of condition becomes the number of rows in the top half of the decision table.
Mutually exclusive conditions
Gender • Male and Female
Education • High school, Bachelor, Master and Doctor etc
Mutually exclusive conditions should be combined to be one condition with multiple alternatives.
 Step 2: Determine the number of possible actions that can be taken. This becomes the number of rows in the lower half of the
decision table.
 Step 3: Determine the number of condition alternatives for each condition. In the simplest form of decision table, there would
be two alternatives (Y or N).In an extended-entry table, there may be many alternatives conditions.
 Step 4: Calculate the maximum number of columns in the decision table by multiplying the number of alternatives for each
condition.

CSC 113
Computer programming i
 Decision Table
 For instance, if there are four conditions and two alternatives for each of them, then the possibilities will: 28

 Step 5: Fill the condition alternatives, start with the first condition and divide the number of columns by the number of
alternatives (2) for that condition.
 Step 6: Complete the table by inserting an X where rules suggest certain actions.
 Step 7: Combine rules where it is apparent that an alternative does not make a difference in the outcome
 Step 8: Check the table for any impossible situations, contradictions, redundancies.
 Step 9: Rearrange the conditions and actions (or even rules) to make the decision table more understandable

CSC 113
Computer programming i
 Decision Table
An example 29

A store wishes to program a decision on non-cash receipts for goods into their intelligent tills.
Conditions
1. Transaction under £50
2. Pays by cheque with cheque card (guarantee £50)
3. Pays by credit card
Possible actions
1. Ring up sale
2. Check credit card from local database
3. Call a supervisor
4. Automatic check of credit card company database

CSC 113
Computer programming i
 Decision Table
30

Some of the condition rules are invalid.Customer


cannot pay by cheque AND pay by credit card or
not pay by either method.

CSC 113
Computer programming i
 Decision Table
31

CSC 113
Computer programming i
 Decision Table
32

CSC 113

You might also like