Chapter 2- Introduction to Progr new
Chapter 2- Introduction to Progr new
E R a tion
P T e
e n
s re
t
A Re ruc
p r tu
H
C orithm a s t
at
lg D
A
1
Out line
2.1. Introduction to flow chart and pseudo code
2.2. How to write a pseudo code and draw a flow chart for a given
algorithm?
2.3. Translating algorithms to programming tools
2.4. Definition of data structures
2.5. Role of data structures in writing programs
2.6. Algorithms Vs Data Structure
2
PROBLEM SOLVING TECHNIQUES
Problem solving is the process of transforming the description of a
problem into the solution by using knowledge of problem domain
and appropriate problem-solving strategies, techniques, and tools.
There are two approaches of problem solving:
3
CONT…
Top down design: The approach that break a larger problem into
4
ALGORITHM, PSEUDO CODE AND FLOW CHART
Algorithm
• The sequence of steps to be performed in order to solve a problem
by computer is known as an algorithm.
• In mathematics, computer science, and related subjects, an algorithm
is a finite sequence of steps expressed for solving a problem.
• An algorithm can be defined as a process that performs some
sequence of operations in order to solve a given problem.
5
CONT…
• A good algorithm is like using the right tool in workshop. It does the
job with the right amount of effort.
• It is a step-by-step description of how to arrive at the solution of the
given problem.
• It may be formally defined as a sequence of instructions designed in
such a way that if the instructions are executed in the specified
sequence, the desired results will be obtained.
6
CONT…
• Using the wrong algorithm or one that is not clearly defined is like
trying to cut a piece of plywood with a pair of scissors although the
job may get done, you have to wonder how effective you were in
completing it.
7
CONT…
Instruction sequence in algorithm must possess the following
characteristics:
1. Every instruction should be precise and unambiguous.
2. Each instruction should be such that it can be performed in a
finite time.
3. One or more instructions should not be repeated infinitely.
4. After the algorithm terminates, the desired results must be
obtained.
8
CONT…
Quality of Algorithm
• In given a solvable problem, there are often many methods to solve it.
All of these methods may not be equally good.
• Similarly, for a given problem, there may be many algorithms, not all
have equal quality.
So, primary factors that are often used to judge the quality of an
algorithm are:
9
CONT…
1. Time requirement:
• This is the time required to execute the corresponding program on
a given computer system.
2. Memory requirement:
• This is the memory space required to execute the corresponding
program on a given computer system.
10
CONT…
3. Accuracy of solution:
• Although multiple algorithms may provide correct solutions to a
given problem, some of these may provide more accurate results
than others. Therefore, we have to choose the better algorithm that
give accurate results.
4. Generality:
• A generalized algorithm which can handle a range of input data is
better than one which has been designed to solve a problem for a
single input data. 11
CHARACTERISTICS OF A GOOD ALGORITHM
Output unambiguity
6 1
Input Characteristics of Flexibility
5 Algorithm
2
Language
Finiteness
4 3 independent
12
Guidelines for Developing an Algorithm:
An algorithm will be enclosed by start (or begin) and stop (or end).
To accept data from the user, generally used statements are input, read,
get or obtain.
To display the result or any message, generally used statements are
print, display, or write.
Generally, compute or calculate is used while describing mathematical
expressions, and based on the situation relevant operators can be used.
13
Example
Example1:Algorithm to find the square of the number.
• Step 1: start
• Step 2: read the number
• Step 3: compute square = number * number
• Step 4: print square
• Step 5: stop
14
CONT…
Example 2: Algorithm to add two numbers
Step 1: start
Step 2: read two numbers n1 and n2
Step 3: sum = n1 + n2
Step 4: print sum
Step 5: stop
15
CONT…
Example 3: Algorithm to find the larger number from two numbers
Step 1: start
Step 2: read two numbers n1 and n2
Step 3: if n1 > n2 then
Big = n1
else
Big = n2
Step 4: print Big
Step 5: stop
16
CONT…
Example 4: Algorithm to find largest number from three numbers
Step 1: start
Step 2: read three numbers n1, n2 and n3
Step 2: if n1 > n2 and n1 > n3 then
Big = n1
else
If n2 > n1 and n2 > n3 then
Big = n2
else
Big = n3
Step 3: print big 17
Step 4: stop
CONT…
Example 5: Algorithm to find sum of N positive integer numbers
Step 1: start Step 8: if count < N then go to step 5
Step 2: read N Step 9: print sum
Step 3: sum = 0 Step 10: stop
Step 4: count = 0
Step 5: read num
Step 6: sum = sum + num
Step 7: count = count + 1
18
CONT…
Example 6: Algorithm to find factorial of a given Number. (N! =
1*2*3*…*N)
Step 1 : read N
Step 2: fact = 1
Step 3 : count = 1
Step 4 : fact = fact * count
Step 5 : count = count + 1
Step 6 : if count < = N then go to step 5
Step 7 : print fact
19
Step 8 : stop
Presentation of Algorithms
An algorithm can be expressed in many ways. Here, we only consider
two of such methods:
I. Narrative format ( i.e. Put descriptions of an algorithm in a list of
orders or pseudo code)
II. Flow chart format (i.e. Graphical representation of an algorithm)
20
Pseudo code
Pseudo code is a generic way of describing an algorithm without use
of any specific language syntax.
Program design technique that uses English words.
Has no formal syntactical rules/ syntax.
It is a simple way of writing programming code in English.
Pseudo code is not an actual programming language. it uses short
phrases to write code for programs before you actually create it in a
specific language. 21
CONTT...
Pseudocode needs to be complete. it describes the entire logic of the
algorithm, so that implementation becomes translating line by line into
source code.
As the name suggests, it cannot be executed on a real computer, but it
models and resembles real programming code, and is written at roughly
the same level of detail.
Pseudo code, by nature, exists in various forms although most borrow
syntax from popular programming languages (like C, Lisp, or Fortran).
Natural language is used whenever details are unimportant or distracting.
22
RULES USED TO WRITE PSEUDOCODE
25
CONT…
Example 1: Suppose you are required to design an algorithm for
finding average of six numbers, and the sum of the numbers is given.
The pseudo code will be as follows
Start
Get the sum
Average = sum / 6
Output the average
Stop
26
CONT…
Example 2: This is the pseudo-code required to input three numbers
from the keyboard and calculate the sum as output result.
Start
Use variables: sum, number1, number2, number3 of type integer
Accept number1, number2, number3
Sum = number1 + number2 + number3
Print sum
End program 27
CONT…
• Example 3: The following pseudo-code describes an algorithm which will
accept two numbers from the keyboard and calculate the sum and product
displaying the answer on the monitor screen.
Start
Use variables sum, product, number1, number2 of type real
Display “input two numbers”
Accept number1, number2
Sum = number1 + number2
Print “the sum is “, sum
Product = number1 * number2
Print “the product is “, product 28
End program
CONT…
• Example 4: Write a Pseudocode to find the greater number between two
numbers.
Start /BEGIN
NUMBER A, B
INPUT A
INPUT B
IF A > B THEN
OUTPUT A "is higher"
ELSE
OUTPUT B "is higher"
ENDIF
END
29
CONT…
ADVANTAGES
Can be done easily on a word processor
Easily modified
Implements structured concepts well
DISADVANTAGES
There is no accepted standard, so it varies widely from company to
company.
30
Flowcharts
A Flowchart is a type of diagram (graphical or symbolic) that
represents an algorithm or process.
Each step in the process is represented by a different symbol and
contains a short description of the process step.
It is basically a pictorial or diagrammatic representation of an
algorithm using standard symbols.
It is a graph used to depict/show a step-by-step solution using
symbols which represent a task. 31
CONT…
• A flowchart describes what operations (and in what sequence) are
required to solve a given problem.
• Generally, Flowcharts are a pictorial or graphical representation of a
process.
• The flow chart symbols are linked together with arrows showing the
process flow direction.
• A flowchart typically shows flow of data in a process, detailing
operations/steps in a pictorial format which is easier to understand
than reading it in a textual format. 32
CONT…
Once the flowchart is drawn, it becomes easy to write the program
using any high level language.
• Hence, it is correct to say that a flowchart is a must for the better
documentation of a complex program.
33
Flowchart Symbols & Guidelines:
Terminator
34
CONT…
• Process: A rectangular flow chart shape used to indicates
mathematical operation in process flow. For example, “Add 1 to X”,
“M = M*F” and the like. Process
Decision
35
CONT…
• Connector: Circular flow chart shape used to indicate a jump in the
process flow. Connectors are generally used in complex or multi-
sheet diagrams. It may be on page and off page.
Connector
Input/Out put
36
CONT…
• Arrow: used to show the flow of control/relationship between d/t
shape in a process. An arrow coming from one symbol and ending at
another symbol represents that control passes to the symbol the arrow
points to.
37
CONT…
Now, the basic guidelines for drawing a flowchart with the above symbols
are :
In drawing a proper flowchart, all necessary requirements should be
listed out in logical order.
The flowchart should be neat, clear and easy to follow. There should
not be any room for ambiguity in understanding the flowchart.
The flowchart is to be read left to right or top to bottom.
A process symbol can have only one flow line coming out of it
38
CONT…
For a decision symbol, only one flow line can enter it, but multiple
lines can leave it to denote possible answers.
The terminal symbols can only have one flow line in conjunction
with them.
39
CONT…
• Example 1: The problem is , to find the sum, average and product of 3
numbers given by the user.
• Algorithm & flow chart for given problem is as follows: Start
Step 1: Start
Read X, Y, Z
Step 2: Read X, Y, Z
Step 3: Compute sum (S) as X + Y + Z S= X+Y+Z
Step 4: Compute average (A) as S / 3 A=S/3
P=X.Y.Z
Step 5: Compute product (P) as X * Y * Z
Step 6: Write (display) the sum, average and product Write S, A, P
Step 7: Stop 40
Stop
CONT…
• Example 2: Write a Pseudocode and flow chart to find the greater number
between two numbers?
Start /BEGIN
READ NUMBER A, B
INPUT A
INPUT B
IF A > B THEN
OUTPUT “A is higher"
ELSE
OUTPUT “B is higher"
ENDIF
END 41
CONT…
Advantages of Using Flowcharts
• Communication: flowcharts are better way of communicating the
logic of a system to all concerned.
• Effective analysis: with the help of flowchart, problem can be
analyzed in more effective way.
• Proper documentation: program flowcharts serve as a good
program documentation, which is needed for various purposes.
42
CONT…
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. 43
CONT…
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 than 3 symbols
6. Subroutines and interrupt programs have their own and
independent flowcharts.
44
CONT…
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
45
Overview of Data Structure
What is “data structure”?
Data structure is a way to store and organize data in memory so that it
can be used efficiently.
It is not a programming language like c, c++, java, etc. but, it is a set of
algorithms that we can use in any programming language to structure the
data in memory.
46
CONT…
DATA STRUCTURE CLASSIFICATION
DATA STRUCTURE
Non-Primitive Data
Primitive Data Structure
Structure
int
Linear Non-Linear
char
doubl
e Linked Queu
Array Stack
pointe List e
r
47
CONTT…
i. Primitive data structure
● Primitive data structures are primitive data types.
● The int, char, float, double, and pointer are the primitive data
structures that can hold a single value.
ii. Non-primitive data structure
● Non-primitive data structure is divided into two types:
Linear data structure: data in a sequential manner.
Non-linear data structure: data in a random manner.
48
Algorithm Vs Data Structure
Algorithm
• The sequence of steps /order performed to solve a problem by computer .
• It is a finite sequence of steps expressed for solving a given problem.
Data Structure
• Systematic storage , construction and organization of data in computer
memory.
49
PROGRAM DEVELOPMENT CYCLE:
1. Analyze: define the problem
5. Debug and test: locate and remove any errors in the program.
6. Complete the documentation: organize all the materials that describe the
program.
50
Basic Program Development Tips
Program design in any programming language need to be:
Reliable: The program should always do what it is expected to do and
handle all types of exception.
Maintainable: The program should be in a way that it could be modified
and upgraded when the need arises.
Portable: The software needs to be adapt one type of computer to
another with minimum modification.
Efficient: The program should be designed to use optimal time, space and
other resources of the computer.
51
N D
E 52