Lecture 4
Lecture 4
TECHNIQUES
Lecture 2a
Lecture Outline
■ Algorithms
■ Pseudocode
■ Flowcharts
■ Types of Errors
Algorithms
■ An algorithm is a sequence of steps for solving a specific
problem given its input data and the expected output
data.
– It can also be described as a step-by-step problem-
solving process in which a solution is arrived at in a
finite amount of time.
■ When we write a computer program, we are generally
implementing a method that has been devised previously
to solve some problem.
Example
■ Music Purchase Program Algorithm
1. Enter the number of songs
2. Calculate the price
– Use the formula: Price = 0.99 * songs
3. Display the results: Price
Operations used to construct algorithms belong
to one of three categories:
■ Sequential (sequence) operations:
– This carries out a single well-defined task. When that task
is finished, the algorithm moves on to the next operation.
■ Conditional (decision/selection) operations:
– These ask a question, and the next operation is selected
on the basis of the answer to that question.
■ Iterative (loop/repetition) operations
– They tell us not to go on to the next instruction but,
instead, to go back and repeat the execution of a previous
block of instructions.
The Sequential Operations
■ A sequential operations consists of a series of
consecutive statements, executed in the order in which
they appear.
■ None of the statements in this operation causes a
branch. The general form of a sequential structure is as
follows:
Statement 1
Statement 2
…Statement N
Conditional Operations
■ Selection/decision/conditional operations contain branch
points or statements that cause a branch to take place.
■ In a conditional operation there is a branch forward at
some point, which causes a portion of the program to be
skipped.
■ Depending upon a given condition at the branch point, a
certain block of statements will be executed while
another is skipped.
Conditional Structure
Iterative (Loop/Repetition) Operation
■ A iterative operation contains a branch which results in a
block of statements that can be executed many times.
■ It will be repeated as long as a given condition within the
loop structure causes the branch to be taken.
– The ability to repeat the same actions over and over is
the most basic requirement in programming
Repetition Operation
Pseudocode
■ Pseudocode uses short, English-like phrases to describe
the outline of a program.
– It’s not actual code from any specific programming
language, but sometimes it strongly resembles the
actual code.
Example 1
■ Pseudocode for cooking rice might look as follows:
1) Wash the rice
2) Put into boiling water in saucepan
3) Leave to boil for some minutes until thoroughly cooked
■ Pseudocode for circle area program might look as follows:
1) Input the circle radius
2) Compute circle area using the circle area formula
3) Output the results of circle area
Example 2
■ Pseudocode for adding two numbers might look as:
1) Enter first number
2) Enter second number
3) Calculate the sum of the two numbers, i.e. num1 + num2
4) Display the results
Try
1. Write an algorithm in pseudocode to find the profit on
each item bought in a shop.
2. Write an algorithm in pseudocode to find the area and
perimeter of a rectangle.
Flow charts
■ This is a diagram that uses special symbols to display
pictorially the flow of execution within a program or a
program module.
■ It is a formalized graphic representation of a program’s
logic sequence, a work or manufacturing process, or any
similar structure.
■ It provides an easy, clear way to see which pieces of code
follow the various programming structures that all
programs are constructed from.
Creating flow charts
■ A programmer would create a flow-chart to aid in
understanding the flow of a program.
■ Then, the programmer would write code, in a specific
programming language, following the logic that he had
created in the flowchart.
■ Interactive flowcharting applications are available for
creating flow charts
– Raptor, MS word templates, etc
Flow Chart Symbols
■ A typical flowchart will include some or all of the following
symbols;
– Start and End symbols are represented as ovals or
rounded rectangles.
– Arrows show the flow of control. An arrow coming from
one symbol and ending at another symbol represents
that control passes to the symbol to which the arrow
points.
– Processing steps are represented as rectangles.
Flow Chart Symbols
– Input / Output steps are represented as parallelograms.
– Conditional (or decision or selection) segments are
represented as diamond shapes. These typically
contain a Yes/No question or a True/False test. This
symbol has two arrows coming out of it.
– Connectors are represented as circles. These are used
to connect one program segment to another.
Flow
Chart
Symbols
Example 1 Start
3. Calculate Average
4. Output Results
Calculate Average
Output Results
End
Example 2
Circle area program
1) Input the circle radius
2) Compute circle area using the circle area formula
3) Output the results of circle area