CSC101
INTRODUCTION TO COMPUTER SCIENCE
Computational-
Problem Solving
Process
PROBLEM SOLVING USING
ALGORITHMS AND FLOWCHARTS
A computer is only a problem-solving tool!
(one of the many different tools engineers and
computer scientists use in solving problems).
The 5 steps in using a computer as a
problem-solving tool
Develop an Algorithm and a Flowchart.
Write the program in a computer language.
Enter the program into the computer.
Test and debug the program.
Run the program, input data, and get the results
from the computer.
ALGORITHMS
An Algorithm is just a detailed sequence of simple
steps that are needed to solve a problem.
It is a process or set of rules to be followed in
problem-solving operations, especially by a computer.
It is an effective method expressed as a finite list of
well-defined instructions for solving a problem.
An algorithm is a sequence of unambiguous
instructions for solving a problem, i.e., for obtaining
a required output for any legitimate input in a finite
amount of time.
PROPERTIES OF AN ALGORITHM
“Besides merely being a finite set of rules that gives
a sequence of operations for solving a specific type of
problem, an algorithm has the five important
features/properties”
finiteness: The algorithm must always terminate after a
finite number of steps.
definiteness: Each step must be precisely defined; the
actions to be carried out must be rigorously and
unambiguously specified for each case.
input: An algorithm has zero or more inputs, taken from
a specified set of objects.
output: An algorithm has one or more outputs, which
have a specified relation to the inputs.
effectiveness: All operations to be performed must be
sufficiently basic that they can be done exactly and in
finite length.
PROBLEMS VS. ALGORITHMS VS. PROGRAMS
For each problem or class of problems, there may
be many different algorithms.
For each algorithm, there may be many different
implementations (programs).
A computer algorithm is a detailed step-by-step
method for solving a problem using a computer.
A program is an implementation of one or more
algorithms.
METHOD FOR DEVELOPING AN ALGORITHM
1. Define the problem: State the problem you are
trying to solve in clear and concise terms.
2. List the inputs (information needed to solve the
problem) and the outputs (what the algorithm will
produce as a result).
3. Describe the steps needed to convert or manipulate
the inputs to produce the outputs.
Start at a high level first, and keep refining the steps until
they are effectively computable operations.
4. Test the algorithm: choose data sets and verify that
your algorithm works!
EXPRESSING ALGORITHMS
An algorithm may be expressed in a number of
ways, including:
natural language: usually verbose and ambiguous
flow charts: avoid most (if not all) issues of
ambiguity; difficult to modify w/o specialized tools;
largely standardized
pseudo-code: also avoids most issues of ambiguity;
vaguely resembles common elements of programming
languages; no particular agreement on syntax
programming language: tend to require expressing
low-level details that are not necessary for a high-
level understanding
COMMON ELEMENTS OF ALGORITHMS
acquire data (input)
some means of reading values from an external source;
most algorithms require data values to define the specific
problem (e.g., coefficients of a polynomial)
computation
some means of performing arithmetic computations,
comparisons, testing logical conditions, and so forth...
selection
some means of choosing among two or more possible
courses of action, based upon initial data, user input and/or
computed results
iteration
some means of repeatedly executing a collection of
instructions, for a fixed number of times or until some
logical condition holds
report results (output)
some means of reporting computed results to the user, or
requesting additional data from the user
EXAMPLE 1
Assuming that the gross pay of an employee is to be
calculated and then 10 percent of the gross is to be
deducted as tax while the remaining gives the net
pay. Write down the algorithm for this problem.
Sol.:
Begin
input name, hours-worked, and wage/hour
Calculate gross-pay = hours-worked * wage/hour
Calculate tax = (10/100) * gross-pay
Calculate net-pay = gross-pay – tax
Print name, net-pay
End
EXAMPLE 2
Write an algorithm to read the name and the
mark of one student and then add 5 to his/her
mark.
Sol.:
Begin
input name, mark
New-mark = mark + 5
print name, new-mark
End
EXAMPLE 3
Write an algorithm to read the name and marks
of 10 students and calculate the new mark of
each by adding 5 to their marks.
Sol.:
Begin 1
student-count = 1 2
if student-count > 10 then Stop 3
else 4
read (input) name, mark 5
calculate new-mark = mark + 5 6
print name, new-mark 7
student-count = student-count + 1 8
goto step 3 9
End 10
FLOWCHARTS
A graphical tool that diagrammatically depicts the
steps and structure of an algorithm or program
The basic and most commonly used symbols are given
below;
GENERAL RULES FOR FLOWCHARTS
All symbols of the flowchart are connected by
flow lines (note arrows, not lines)
Flow lines enter the top of the symbol and exit
out the bottom, except for the Decision symbol,
which can have flow lines exiting from the bottom
or the sides
Flowcharts are drawn such that flow generally
goes from top to bottom
The beginning and the end of the flowchart is
indicated using the Terminal symbol
EXAMPLE 1
Draw the flowchart for this algorithm:
Begin
input name, mark
new-mark = mark + 5
print name, new-mark
End
EXAMPLE 2
Draw the flowchart for the algorithm below:
Begin
student-count = 1
if student-count > 10 then Stop
else
read (input) name, mark
calculate new-mark = mark + 5
print name, new-mark
student-count = student-count + 1
goto step 3
End
EXAMPLE 3
EXAMPLE 4
Read name for student, when name = "xxx“ stop
the program, if name does not equal "xxx“ then
compute the grade of each student. Every student
has five marks. The average of these marks has
to be computed for each student, and based on
the student average, the grade has to be as
follows:
Avg >= 90------------------------> grade = 'A'
80 <= Avg < 90------------------------> grade = 'B'
70 <= Avg < 80------------------------> grade = 'C'
60 <= Avg < 70------------------------> grade = 'D'
Avg < 60------------------------> grade = 'F'
PSEUDOCODE (OR PROGRAM DESIGN LANGUAGE)
Consists of natural language-like statements that
precisely describe the steps of an algorithm or
program
Statements describe actions
Focuses on the logic of the algorithm or program
Avoids language-specific elements
Written at a level so that the desired programming
code can be generated almost automatically from
each statement
Steps are numbered. Subordinate numbers and/or
indentation are used for dependent statements in
selection and repetition structures
PSEUDOCODE LANGUAGE CONSTRUCTS
EXERCISES
1. Write an appropriate Algorithm and draw a
corresponding Flowchart for finding the real
roots of a quadratic equation.
I. O. Akinyemi (2017)
2. Consider Example 4, assume 100 Level result
is required for your Department:
write an appropriate Algorithm and
draw corresponding Flowchart
for computation of CGPA for all 100L students’
results for all registered courses
THANK
YOU