Chapter 2 - Computer Program Algorithm
Chapter 2 - Computer Program Algorithm
An algorithm can be viewed as a tool for solving a well-specified computational problem. The statement of
the problem specifies in general terms the desired input/output relationship. The algorithm describes a
specific computational procedure for achieving that input/output relationship. Algorithms are very essential
as they instructs the computer what specific steps it needs to perform to carry out a particular task or solve a
problem.
2) Algorithm Analysis: The analysis of an algorithm determines the amount of resources, such as time and
space required by it for its execution. Generally, the algorithms are formulated to work with the inputs or
arbitrary length. Algorithm analysis provides theoretical estimates required by an algorithm to solve a
problem.
2) Dynamic programming: is a method for solving a complex problem by breaking it down into a
collection of simpler sub-problems, solving each of those sub-problems just once, and storing their
solutions. Dynamic programming algorithms are often used for optimization. A dynamic programming
algorithm will examine the previously solved sub-problems and will combine their solutions to give the
best solution for the given problem.
3) Greedy algorithm: is an algorithmic paradigm that follows the problem solving heuristic of making the
locally optimal choice at each stage with the hope of finding a global optimum.
In general, greedy algorithms have five components:
a) A candidate set, from which a solution is created
b) A selection function, which chooses the best candidate to be added to the solution
c) A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
d) An objective function, which assigns a value to a solution, or a partial solution, and
e) A solution function, which will indicate when we have discovered a complete solution
Greedy algorithms produce good solutions on some mathematical problems, but not on others.
4) Back Tracking. is a general algorithm for finding all (or some) solutions to some computation al
problems, notably constraints satisfaction problems, that incrementally builds candidates to the
solutions, and abandons each partial candidate ("backtracks") as soon as it determines that the candidate
cannot possibly be completed to a valid solution.
Backtracking is an important tool for solving constraint satisfaction problems, such as crossword, verbal
arithmetic, Sudoku and many other puzzles.
5) Brute Force: A general problem-solving technique that consists of systematically enumerating all
possible candidates for the solution and checking whether each candidate satisfies the problem's
statement.
A flowchart is drawn using boxes of different shapes with lines connecting them to show the flow of control.
The purpose of drawing a flowchart is to make the logic of the program clearer in a visual form.
A flowchart may be simple or complex. While drawing a flowchart, some rules need to be followed
1) A flowchart should have a start and end,
2) The direction of flow in a flowchart must be from top to bottom and left to right, and
3) The relevant symbols must be used while drawing a flowchart.
Calculate wages
Is the
transaction a
credit?
Start
Stop
Connector symbol:
Small Circle
Exit to or entry from another part of the chart
Or
Sequence: In a sequence, the steps are executed in linear order one after the other.
Selection / Branching: the step to be executed next is based on a decision taken. If the
condition is true (yes) a different path is followed than if the condition evaluates to false (no).
Interation / looping. In case of iterative operation, a condition is checked. Based upon the
result of this conditional check, true or false, different paths are followed. Either the next step in
the sequence is executed or the control goes back to one of the already executed steps to make a
loop.
1) The first flowchart computes the product of any two numbers and gives the result. The flowchart is a
simple sequence of steps to be performed in a sequential order.
2) The second flowchart compares three numbers and finds the maximum of the three numbers. This
flowchart uses selection. In this flowchart, decision is taken based upon a condition, which decides the
next path to be followed, i.e. If A is greater than B then the true (Yes) path is followed else the false
(No) path is followed. Another decision is again made while comparing MAX with C.
3) The third flowchart finds the sum of first 100 integers. Here, iteration (loop) is performed so that some
steps are executed repetitively until they fulfill some condition to exit from the repetition. In the decision
box, the value of I is compared with 100. If it is false (No), a loop is created which breaks when the
condition becomes true (Yes).
Pseudo code is a short-hand way of describing a computer program. It is an informal high-level description
of a computer programming algorithm, which omits details that are not essential for human understanding of
the algorithm. Using pseudo code, it is easier for a programmer or a non-programmer to understand the
general working of the program, since it is not based on any programming language. It is used to give a
sketch of the structure of the program, before the actual coding. It uses the structured constructs of the
programming language but is not machine-readable.
Examples
1. Finding the Product of two numbers
For writing the pseudo code, the programmer is not required to know the programming language in which
the pseudo code will be implemented later.
2.8. Representing Program Control Structures using Flow Chart & Pseudo code
Control Structure Pseudo code Flow Chart
Sequence: 1st Instruction
In the absence of selection, or
repetition, program statements 2nd Instruction
are executed in the sequence in
which the appear in the program 3rd Instruction
Selection IF
Part of decision making and condition Yes
No
allows alternative actions to be THEN
?
taken according to the conditions actions
that exist at particular stages in ELSE
program execution actions
ENDIF
Or
REPEAT
actions
UNTIL
condition
No
?
Yes
Exercises
Develop the Algorithm and represent using both Flow Chart and Pseudo code that would solve the following
problem
1. A Construction company which hires casual works intends to computerize their wages calculation
process. The company wages payment policy is as follows;
a) The company has a 5 days working week.
b) Men are paid Ksh 250 per day while women are paid Ksh 200 per day
c) Extra day is paid at Ksh 50 more than the Normal working day.
3. A Bank intends to automate the calculation of bonuses for account holders at the end of every year. The
Bank’s policy is to give a 2% bonus on the ending balance of all accounts at the end of the year. A 5%
bonus is also given to all Female account holders if the balance is more than 50,000.