Lesson 10 - Introduction To Computer Programming (Algorithms and Flowcharts)
Lesson 10 - Introduction To Computer Programming (Algorithms and Flowcharts)
Computer
Programming
Algorithm, Pseudocode and Flowcharts
Introduction
• A typical programming task can be divided into the following:
• Problem solving: Produce an ordered sequence of steps that describe
solution to the problem called an algorithm
• Implementation: implement the algorithm in some programming
language
Algorithm
• Algorithm can be defined as a sequence of activities or steps to be carried
out to get a desired output from a given input.
• There may be more than one way to solve a problem, so there may be
more than one algorithm for a problem.
• Before writing an algorithm for a problem, one should find out what is/are
the inputs to the algorithm and what is/are expected output after running
the algorithm.
Writing Algorithm
• While writing algorithms we use following symbol for different operations:
• ‘+’ for Addition
• ‘-’ for Subtraction
• ‘*’ for Multiplication
• ‘/’ for Division and
• ‘←’ for assignment. For example A ← X*3 means A will have a value of X*3.
Examples of Algorithms
• An algorithm to read two numbers and find their sum.
• Step 1: Input num1, num2
• Step 2: Sum ← num1+num2
• Step 3: Print Sum
Examples of Algorithms
• An algorithm to find the greater number between two numbers
• Step 1: Input A, B
• Step 2: If A > B then C ← A
• Step 3: if B > A then C ← B
• Step 4: Print C
Examples of Algorithms
• An algorithm to find the largest value of any three numbers.
• Step 1: Input A, B, C
• Step 2: If (A >= B) and (A >=C ) then Max ← A
• Step 3: If (B >= A) and (B >= C) then Max ← B
• Step 4:If (C >= A) and (C >= B) then Max ← C
• Step 5: Print Max
Properties of algorithm
• Donald Ervin Knuth has given a list of five properties for algorithm, these
properties are:
• 1) Finiteness: An algorithm must always terminate after a finite number of steps. It
means after every step one reach closer to solution of the problem and after a finite
number of steps algorithm reaches to an end point.
• 2) Definiteness: Each step of an algorithm must be precisely defined. It is done by
well thought actions to be performed at each step of the algorithm. Also the actions
are defined unambiguously for each activity in the algorithm.
Properties of algorithm
• 3) Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation. So the value/quantities are given
to the algorithm before it begins.
• 4) Output: One always expects output/result (expected value/quantities) in terms of
output from an algorithm. The result may be obtained at different stages of the
algorithm. If some result is from the intermediate stage of the operation then it is
known as intermediate result and result obtained at the end of algorithm is known as
end result. The output is expected value/quantities always have a specified relation
to the inputs
Properties of algorithm
• 5) Effectiveness: Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can in principle be done
exactly and in a finite amount of time by a person, by using paper and pencil only.
Pseudocode
• Pseudocode is a simplified representation of an algorithm that uses the
English language to describe coding logic.
• Pseudocode primarily uses plain text to describe various coding actions
and their correct sequence in the algorithm.
• You can also include mathematical symbols to reference data sets in
pseudocode. Programmers can interpret each line of pseudocode and
create algorithms using their preferred coding language.
Pseudocode and Algorithm
• Write a pseudocode and an algorithm to determine a student’s final grade
and indicate whether it is passing or failing. The final grade is calculated
as the average of four marks.
Pseudocode and Algorithm
• Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE ← (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Pseudocode and Algorithm
• Pseudocode
• Input a set of 4 marks
• Calculate their average by summing them and dividing
by 4
• if average is below 50
• Print “FAIL”
• else
• Print “PASS”
Flowcharts
• A flowchart is a diagram which visually presents the flow of data through
processing systems. This means by seeing a flow chart one can know the
operations performed and the sequence of these operations in a system.
• Algorithms are nothing but sequence of steps for solving problems. So a
flow chart can be used for representing an algorithm.
• A flowchart, will describe the operations (and in what sequence) are
required to solve a given problem. You can see a flow chart as a blueprint
of a design you have made for solving a problem.
Flowchart Symbols
• There are 6 basic symbols commonly used in flowcharting: Terminal,
Process, input/output, Decision, Connector and Predefined Process.
• This is not a complete list of all the possible flowcharting symbols, it is
the ones used most often.
Flowchart Symbols
Symbol Name Function
Process Indicates any type of internal operation inside the Processor or
Memory
Input/Output Used for any Input / Output (I/O) operation. Indicates that the
computer is to obtain data or output results
Decision Used to ask a question that can be answered in a binary format
(Yes/No, True/False)
Connector Allows the flowchart to be drawn without intersecting lines or without
a reverse flow.
Predefined Used to invoke a subroutine or an Interrupt program.
process
Flowchart Symbols
Symbol Name Function
Terminal Indicates the starting or ending of the program, process, or interrupt
program
Flow lines Shows direction of flow.
General Rules for flowcharting
• 1. All boxes of the flowchart are connected with Arrows. (Not lines)
• 2. Flowchart symbols have an entry point on the top of the symbol with no
other entry points. The exit point for all flowchart symbols is on the
bottom except for the Decision symbol.
• 3. The Decision symbol has two exit points; these can be on the sides or
the bottom and one side.
• 4. Generally a flowchart will flow from top to bottom. However, an
upward flow can be shown as long as it does not exceed 3 symbols.
General Rules for flowcharting
• 5. Connectors are used to connect breaks in the flowchart. Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same page.
• An upward flow of more then 3 symbols
• 6. Subroutines and Interrupt programs have their own and independent
flowcharts.
General Rules for flowcharting
• 7. All flow charts start with a Terminal or Predefined Process (for interrupt
programs or subroutines) symbol.
• 8. All flowcharts end with a terminal or a contentious loop.
Examples of Flowcharts
• A flowchart to read radius of a circle and calculate an area of the circle.
Examples of Flowcharts
• An algorithm to find the greater number between two numbers
Advantages of Using Flowcharts
• 1) Communication: A Flowchart can be used as a better way of
communication of the logic of a system and steps involve in the solution, to all
concerned particularly to the client of system.
• 2) Effective analysis: A flowchart of a problem can be used for effective
analysis of the problem.
• 3) Documentation of Program/System: Program flowcharts are a vital part of
a good program documentation. Program document is used for various
purposes like knowing the components in the program, complexity of the
program etc.
Advantages of Using Flowcharts
• 4) Efficient Program Maintenance: Once a program is developed and
becomes operational it needs time to time maintenance. With help of
flowchart maintenance become easier.
• 5) Coding of the Program: Any design of solution of a problem is finally
converted into computer program. Writing code referring the flowchart of
the solution become easy.
The End
Questions