Top-down design
Top Down Design Approach is one of several problem-solving approaches and is based on the dictum – “Divide and rule”. A small portion of problem is easier to solve at a time than the whole problem taken together. In this approach, the problem is broken down into smaller fragments and fragments, further into still smaller fragments till the fragments are small enough to be solved easily are separately.
The top-down design approach is based on the fact that large problems become more manageable if they are divided into a number of smaller and simpler tasks, which can be tackled separately.
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Introduction to Programming
1. For More Visit: Https://www.ThesisScientist.com
Unit 1
Introduction to Programming
Introduction to Problem Solving
Problem Definition
Problems are undesirable situations, that prevent one from achieving an objective in a given situation, and
therefore, must be solved in order that the objective is achieved.
They occur frequently in our day-to-day life. Some of the problems are well understood, and people have
developed generic solutions to such problems, while some may be quite unique and hence, a solution is
required to be found each time they occur.
Problem Solving
The problems are so inevitable that from time immemorial people have been continuously developing tools
and techniques for handling them. In the latest sequel, the modern computers were invented. But the tools
are tools, after all. They do not solve the problems themselves nor do they suggest any solution or method
arriving at a solution. They only help solving the problems. The method of solving a problem remains more
or less in the domain of human endeavor so far.
Some problems can be defined fairly accurately while others are open-ended and hence it may not be
possible to define them accurately. One of the important factors in solving problems is that the problem
must be defined clearly and unambiguously.
It may be noted here that problem solving is both a science and an art in respective domains. A great deal of
domain-knowledge, intuition, human reasoning and experience are required in this process of problem
solving and therefore, each individual has his/her own unique way of solving a problem.
Some methods may be more general than others but a single set of solutions cannot be applied in all the
situations nor can a single method of finding solution may be applied always. A general method of problem
solving simply does not exist despite the existence of host of sophisticated tools.
Nonetheless, the steps involved in solving any problem may be generalized to some extent, more so in the
computer-based problem solving. One such sets of generalized methodology is sketched in Fig. 1.1
2. For More Visit: Https://www.ThesisScientist.com
Become aware of the problem
Gather information to increase the
understanding of problem situation
Define the problem in terms of
objectives, variables, and their interaction
Develop evaluation criteria Develop alternative solutions
Choose the most appropriate solution
Implement solution
Monitor and control the implementation
Figure 1.1: Steps in Problem Solving
Approaches to problem solving, remain guidelines at best. A number of such approaches have been
proposed for the purpose of problem solving. With little modifications these approaches may be employed
to attempt a solution to the given problem. They are general in the sense that they may be customized in
their applicability in various situations.
Top-down design
Top Down Design Approach is one of several problem-solving approaches and is based on the dictum –
“Divide and rule”. A small portion of problem is easier to solve at a time than the whole problem taken
together. In this approach, the problem is broken down into smaller fragments and fragments, further into
still smaller fragments till the fragments are small enough to be solved easily are separately.
The top-down design approach is based on the fact that large problems become more manageable if they
are divided into a number of smaller and simpler tasks, which can be tackled separately.
Example
Problem: How to reach Kolkata from Delhi by train.
3. For More Visit: Https://www.ThesisScientist.com
Let us apply top-down design approach on this problem. Break this problem into smaller problem as
follows:
Figure 1.2: Top-down Design
The problem has been broken into smaller problems each easily manageable and solvable. If need be,
these smaller tasks may be further broken into still smaller tasks. These smaller tasks – viz. how to buy a
ticket, can be solved easily than the whole problem at a time.
The advantages of this approach are : 1. The sub-problems may be solved independent of each other and 2.
They may be tested and modified, if necessary, separately.
Bottom-up Design
The bottom-up approach is opposite to that of the top-down. Here, the problem solver has to solve the most
basic (smallest) units in the hierarchy first and then use them to build more sophisticated bigger units. The
pure bottom-up approach is generally not recommended because it is difficult to anticipate which low level
units will be needed for solution of a particular problem. It can often be a useful first step to produce a
library of basic solutions before embarking on solving the macro problem.
The bottom-up approach as applied to the Delhi to Kolkata problem:
How to reach Kolkata from Delhi by train
How to reach
Delhi Rly
station
How to
buy
ticket
How to get
to the right
platform
How to
board the
train
How to alight
from the train
How to find
the right
platform No.
How to use
escalator to go
to a platform
4. For More Visit: Https://www.ThesisScientist.com
Figure 1.3: Bottom-up approach
In the bottom-up approach it is usually assumed that the basic sub-tasks created will be general enough to
be used more than once. Use of the solutions to sub-tasks, to construct a solution to the whole problem,
saves you repeating the same instructions time and again.
The obvious advantage of this approach is that the solutions to sub-problems may be re-used as and when
required saving time and effort.
Algorithm
An algorithm is a finite list of well-defined steps for solving a particular problem. These instructions, when
executed in the specified order, solve a problem, which the algorithm intends to solve. The algorithm must
be expressed in a language that is understood by the problem solver.
Writing an algorithm (Elements of Program Style)
An algorithm has three execution-sequences:
1) Sequence Logic or Sequential Logic
2) Selection or Conditional Logic
3) Iteration or Repetitive Logic
In "Sequence Logic" execution the instructions are executed in the obvious linear sequence one by one. The
sequence may be presented explicitly, by means of numbered steps, or implicitly, by the order in which the
instructions are written.
"Selection Logic" employs a number of conditions, which lead to a selection of one out of several
alternative instructions.
"Iteration Logic" allows execution of one or more instructions repetitively, a specified number of times or
until some condition becomes true.
How to reach Kolkata from Delhi by train
How to reach
Delhi Rly
station
How to
buy ticket
How to get to
the right
platform
How to board
the train
How to alight
from the train
How to find the
right platform
No.
How to use
escalator to go to
a platform
5. For More Visit: Https://www.ThesisScientist.com
Any language may be employed to write an algorithm as long as the executor of the algorithm understands
it. We will, however, use the following convention in writing an algorithm throughout this book:
1. An algorithm begins with a START instruction.
2. It ends with a STOP instruction.
3. One instruction is written on one line.
4. Each line is numbered sequentially for identification.
5. The instructions used are well understood by the problem-solver who will use it.
6. A container of a value (known as variable) is represented by any word that does not have language
specific meaning, e.g. A, B, and C etc.
7. A ¬¬ 6 means: store 6 in the variable named A.
8. A A + 1 means: add 1 to the value contained in the variable A and store the result back into it.
Similarly, A B * C would mean multiply values in B and C and store the result into A.
9. Standard arithmetic and logical operators may be employed with their respective meanings as
follows:
Arithmetical Operator Logical Operator
Add + Equal to =
Subtract - Not equal to <>
Multiply ¬ * Less than <
Divide / Greater than >
Remainder % Less than or equal to <=
Greater than or equal to >=
10. Input-Output instructions: Simple verbs like PRINT is used to output value(s) to the user and READ
for reading input value(s) from the user.
PRINT A means: print the value stored in the variable A on the screen.
PRINT A, 3 means: print value stored in variable A and then print 3.
READ A means: take a value from the user and store this value in the variable A.
READ A, B means read two values from user, store first in variable A and second in variable B.
11. Conditional instruction: A condition is an expression that evaluates to either TRUE or FALSE. For
example, (A>5) is a condition expression. If the value stored in variable A is 3, then this condition is
FALSE (i.e. 3 > 5) and if it is 7 then the condition is TRUE (i.e. 7 > 5). An IF instruction is written as
– IF (<condition>) then <action>.
IF (A>4) THEN PRINT 5
This instruction means, if the value contained in the variable A is more than 4, then print the value of
A on the screen. If not, do nothing. There is another form of IF instruction also.
IF (A>4) THEN PRINT 5 ELSE PRINT 10
6. For More Visit: Https://www.ThesisScientist.com
The meaning of the above instruction is that if the value contained in the variable A is more than 4
then print 5 on the screen otherwise print 10.
12. Loop instruction:
A GOTO instruction takes the execution sequence control to the specified instruction No.
GOTO 200
The above instruction means that now on execute the instruction No. 200 onwards.
A DO - WHILE instruction executes a given set of instructions repeatedly as long as the given
condition is true. Execution stops when the condition becomes false.
DO (STEP 2 TO 10) WHILE (A<5)
It means, execute step 2, step 3,…..step 10. Then check the condition (A<5). If the condition is
true then again execute step 2, step 3,…..step 10. Check the condition again. Repeat this process.
Execution stops when the condition (A<5) becomes false. The instructions enclosed within the
loop are executed once, at least.
A WHILE – DO instruction executes a given set of instructions repeatedly as long as the given
condition is true. Execution stops when the condition becomes false.
WHILE (A<5) DO (STEP 2 TO 10)
It means, check the condition (A<5). If the condition is true then execute step 2, step 3,…..step
10. Again check the condition. If the condition is true then again execute step 2, step 3,…..step
10. Check the condition again. Repeat this process. Stop when the condition (A<5) becomes
false.
Loop instruction: REPEAT – UNIT instruction executes a given set of instructions repeatedly
until the given condition becomes false.
REPEAT (STEP 2 TO 10) UNTIL (A<5)
It means, execute step 2, step 3,…..step 10. Then check the condition (A<5). If the condition is
false then again execute step 2, step 3,…..step 10. Check the condition again. Repeat this process.
Execution stops when the condition (A<5) becomes true.
Example
To print all even numbers from 2 to 16 on the screen. Assume that Print instruction prints the given value
on the screen. Use sequential logic.
1. START Begin the algorithm execution
2. PRINT 2 Print value 2
3. PRINT 4 Print value 4
4. PRINT 6 Print value 6
5. PRINT 8 Print value 8
6. PRINT 10 Print value 10
7. For More Visit: Https://www.ThesisScientist.com
7. PRINT 12 Print value 12
8. PRINT 14 Print value 14
9. PRINT 16 Print value 16
10. STOP Terminate the algorithm
As, is evident, this sequential method of printing, is not very nice way of doing the job. If you have to print
values up to 200, you may have to write as many as 100 such lines. Let us see how this algorithm may be
improved using branching (If – then) and looping (go to) in the next example.
Another algorithm for the same problem using selection and iteration logic:
1. START Begin the algorithm execution
2. A 1 Store 1 in the variable 1
3. IF (A % 2 = 0) THEN
PRINT A
If (A%2 is 0 then A contains even number) is true then
print the value stored in A, otherwise move to next line
without doing anything
4. A A + 1 Add 1 to value A contains and store the result back into A
5. IF (A <= 16) THEN GOTO
3
If the value contained in A is smaller than or equal to 16
then do not go to next line, go to line number 3 instead,
otherwise move to next line. In other words, repeat steps
(3,4,5) as long as value contained in A is less than or equal
to 16
6. STOP Terminate the algorithm
Obviously, this algorithm is far better than the previous one. If you have to print the values up to 200, all
you have to do is to change the loop termination condition (A<=16) to (A<=200) instead of writing 100
lines of code.
Another algorithm for the same problem using selection and iteration logic without checking for the
condition for A being even or odd:
1. START Begin the algorithm execution
2. A 2 Store 2 in the variable A
3. PRINT A Print the value stored in A
4. A A + 2 Add 2 to value A contains and store the result back into A
5. IF (A <= 16) THEN GOTO
3
If the value contained in A is smaller than or equal to 16
then do not go to next line, go to line number 3 instead,
otherwise move to next line. In other words, repeat steps
(3,4,5) as long as value contained in A is less than or equal
to 16
6. STOP Terminate the algorithm
8. For More Visit: Https://www.ThesisScientist.com
The above algorithm may also be written using while-do looping logic as below:
1. START Begin the algorithm execution
2. A 2 Store 1 in the variable 1
3. WHILE (A <=16) DO
(STEPS 4 TO 5)
Keep on executing steps 4 and 5 as long as condition (A <=
16) is true, i.e. as long as value stored in A is less than or
equal to 16, otherwise go to step next to the one specified
in the loop condition, i.e. No. 6
4. PRINT A Print the value stored in A
5. A A + 2 Add 2 to value A contains and store the result back into A
6. STOP Terminate the algorithm
Characteristics of Algorithm
Any list of instruction does not constitute an algorithm. An algorithm must have the following
characteristics:
Input: Zero or more values externally supplied to the algorithm.
Output: Zero or more values produced by the algorithm.
Definiteness: Each instruction must be clear and unambiguous, i.e. having one and only
meaning.
Finiteness: For all cases, the algorithm must terminate after a finite number of steps.
Effectiveness: Every instruction must be sufficiently basic and also feasible as far as execution
is concerned.
Implementation of Algorithm
Starting from a problem to be solved, constructing an algorithm for the solution of the same and solving the
problem using the algorithm, is known as algorithm implementation. Algorithm implementation involves
the following different phases of activities:
Problem definition: Understanding the information given and what result is asked for.
Problem Analysis: To analyze the problem to understand and formulate a possible line of attack in
order to solve it.
Algorithm Design: Applying top-down or bottom-up approach and describing the algorithm.
9. For More Visit: Https://www.ThesisScientist.com
Algorithm analysis: To evaluate the designed algorithm and if necessary design alternative algorithm to
suit the requirements better.
Execution: Algorithms, thus developed, may be employed by a person or a machine to solve
the problem.
In order that a computer may execute the algorithm, the algorithm must be translated into a language that
computers understand. An algorithm written in a computer language is known as a program. And
using an algorithm by translating it into a suitable computer language is known as its implementation. It
involves, among others, conversion of the data variables stated in the algorithm into the language specific
data types, selecting suitable data structures etc.
An algorithm may be implemented either in the hardware or in the software. It may also be implemented as
software embedded into the hardware, when it is known as firmware to distinguish it from software.
Developing an algorithm needs no knowledge of any computer language. But its implementation requires
the knowledge of the target computer language. Computer languages have different capabilities. Each
computer language has been designed to facilitate a particular type of algorithm implementation. Hence,
COBOL is more suited for business-related algorithms while FORTRAN for engineering-related
algorithms. C is, in this regard, a general-purpose language and is suitable for all types of algorithms.
Algorithm:
1. START
2. A 1
3. DO (STEP 3 TO 5) WHILE (A < 11)
4. PRINT A
5. A A + 1
6. STOP
Implementation in C language:
#include<stdioh>
main()
{
int a;
a=1;
while(a<11)
{
printf(“%d”,a);
a++;
}
}
Note that the only language a digital computer understands is binary coded instructions. Even the above
implementation will not execute on a computer without further translation into binary (machine) code. This
translation is not done manually, however. There are programs available to do this job. These translation
programs are called compilers and interpreters.
Compilers and interpreters are programs that take a program written in a language as input and translate it
into machine language. Thus a program that translates a C program into machine code is called C compiler;
BASIC program into machine code is called a BASIC compiler and so on.
Therefore, to implement an algorithm on a computer you need to have a compiler for the language you have
chosen for writing the program for the algorithm.
Analysis and Efficiency of Algorithm
10. For More Visit: Https://www.ThesisScientist.com
The correctness of an algorithm in solving a problem is of paramount importance. An algorithm giving
incorrect solution is no better than no algorithm at all.
Besides, an algorithm may solve a problem but the method may not be cost effective. The cost that incurs in
executing an algorithm is in terms of how much space and how much time is required in its execution. Each
variable requires space and each instruction takes some time to execute.
Therefore, analysis of algorithms for their correctness and space and time requirements is a major task in
algorithm design. In order to compare correct algorithms these two bases of space and time are taken into
consideration.
To determine the execution time the following information is required:
Time taken by the executor of the algorithm to read one instruction.
Time taken to understand and interpret the instruction.
Time taken to execute the instruction
The actual time is not easy to determine because each executor of the algorithm takes its own time. The
time taken to execute an instruction also depends on instructions themselves. Some instructions take more
time to execute than others. Another approach called Frequency Count method also exists. In this method
number of operations are counted. The actual time will be proportional to this count and can be computed
by suitably multiplying by some factor. The highest order of the frequency count variable in the total time
expression is known as Order of the complexity denoted by O(). Less the order of complexity, more
efficient the algorithm is.
The space requirement can be calculated by maximum number of variables used by the algorithm in some
arbitrary space unit for the purpose of comparison.
These measurements may be different in different situations, but in case, a computer executes the
algorithm, these values may be found out more accurately.
Consider the following examples:
Example 1
Consider the algorithm to print character “A” N square times:
1. START
2. I 1
3. IF (I > N) THEN GOTO 11
4. J 1
5. IF (J > N) THEN GOTO 9
6. PRINT “A”
7. J J + 1
8. GOTO 5
9. I I + 1
10. GOTO 3
11. For More Visit: Https://www.ThesisScientist.com
11. STOP
Let us count the number of times operations are executed. Assume that START takes 1 unit of time,
assignment () takes one unit of time, comparison (>=) takes two units of time, arithmetic operation (+)
takes one unit of time, GOTO and STOP take one unit of time each. Let us compute the frequency count of
this algorithm.
1. START : 1 unit
2. I 1 : 1 unit
3. IF (I > N) THEN GOTO 11 : (2 + 1 = 3) units
4. J 1 : (1 * N) units
5. IF (J > N) THEN GOTO 9 : (2 + 1) * N units
6. X X + 1 : (N * N) * 1 units
7. J J + 1 : (N * N) * 1 units
8. GOTO 5 : (1 * N) units
9. I I + 1 : (1 * N) units
10. GOTO 3 : (1 * N) units
11. STOP : 1 units
Total units of time taken = 1+1+3+N+3*N+N*N+N*N+N+N+N+1
= 5+7*N+2*N2
The order of complexity = highest order of the frequency count variable
= 2
= O(N2
)
Fundamental Algorithms: Array Techniques
An array is a group of same type of variables, having same name. Different they are only in the way they
are referred to. By same type means the variables can store same type of data items.
There are several types of data items, different in size and value. Mainly, there are two types – Numeric (on
which arithmetic operations may be performed) and Alpha-numeric (on which arithmetic operations cannot
be performed). Thus a value 123 is a numeric value while “123” is an alpha-numeric value. Alphanumeric
values are enclosed within quotes to be distinguished from normal word. 1, 2 and 3 are three characters in
“123” and not one hundred and twenty three.
A variable can store either a numeric type data or alpha-numeric type data but not both. Size of a variable
has technical meaning in the context of computers. But as of now, we can afford to ignore it. More about
array in Chapter 13.
An array is defined in different ways in different languages. We will define it in the following way in our
present discussion:
int A[10]
12. For More Visit: Https://www.ThesisScientist.com
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10]
This instruction creates 10 storage variables, each capable of storing an integer value and having name A.
Different variables represented by this array would be A[1], A[2], A[3]…A[10]. The value enclosed within
the [] is called index of the array.
Similarly,
char BB[50]
will create 50 storage variables, each capable of storing a character value(i.e. „a‟, „b‟,….‟z‟, „A‟, „B‟….‟Z‟,
„0‟,‟1‟,‟2‟…‟9‟, „!‟, „?‟, „/‟ etc.) and having name BB. Different variables represented by this array would
be BB[1], BB[2], BB[3]…BB[50]. The value enclosed within the [] is called index of the array. A[I] would
mean ith
A variable and BB[5], 5th
element of array BB.
Example 4
Consider the following algorithm:
1. START : Start the algorithm
2. char N[6] : Create an array(N) of 6 characters
3. N[1]‟H‟ : Store „H‟ in N[1]
4. N[2]‟e‟ : Store „e‟ in N[2]
5. N[3]‟l‟ : Store „l‟ in N[3]
6. N[4]‟l‟ : Store „l‟ in N[4]
7. N[5]‟o‟ : Store „o‟ in N[5]
8. N[6]‟!‟ : Store „!‟ in N[6]
9. I 1 : Store 1 in variable I
10. IF (I > 6) THEN GOTO 14 : Execute 11 to 13, 6 times
11. PRINT N[I] : Print N[1],N{2]….N[6]
12. I I + 1 : Increment I by 1
13. GOTO 10 : Take the execution control to 10
14. STOP : Terminate the algorithm
This algorithm prints Hello! on the screen. The column on the right is comment column, just to make the
algorithm more understandable and is not part of the algorithm.
Searching, Sorting and Merging Techniques
Some of the most fundamental algorithms applicable on arrays are described below.
Searching
13. For More Visit: Https://www.ThesisScientist.com
There are a number of occasions when searching a value in a group of variables is required. There are a
number of standard algorithms for this purpose. They are known by standard names like – Linear search,
Binary search etc. Here we will discuss these two search algorithms as applied on arrays.
Linear search
This search algorithm is the simplest and relies on comparing each value stored in the array of values, one-
by-one until either the value is found or all the elements have been searched.
1. Situation : Given an array of N number of integers.
2. Problem : To find whether an integer P exists in this array or not. If yes, where?
3. Solution : Search the array, from one element to the last, for this value P.
4. Algorithm :
1. START
2. int A[N] (loaded with values)
3. I 1
4. IF (I > N) THEN GOTO 8
5. IF (A[I] = P) THEN GOTO 8
6. I I + 1
7. GOTO 4
8. IF (I > N) THEN GOTO 11
9. PRINT “VALUE P WAS FOUND IN THE ARRAY AT”, I, “th POSITION”
10. GOTO 12
11. PRINT “VALUE P WAS NOT FOUND IN THE ARRAY”
12. STOP
Binary search
This search algorithm is applicable on the array, which are sorted either in ascending or descending order.
This algorithm is much faster than linear search.
1. Situation : Given an array of N number of integers sorted in ascending order.
2. Problem : To find whether an integer P exists in this array or not.
3. Solution : Divide the array into two halves. Take the half to which P belongs.
Continue this process until either you get the P or its absence is indicated.
4. Algorithm :
1. START
2. int A[N] (loaded with values in ascending order)
14. For More Visit: Https://www.ThesisScientist.com
3. J 1
4. K N
5. I Integral part of((K + J)/ 2)
6. IF (A[I] = P) THEN GOTO 9
7. IF (A[I] > P) THEN GOTO 11
8. IF (A[I] < P) THEN GOTO 14
9. PRINT “P WAS FOUND”
10. GOTO exit
11. IF (I <= 1) THEN GOTO 17
12. J I
13. GOTO 5
14. IF (I <= 1) THEN GOTO 17
15. K I
16. GOTO 5
17. PRINT “VALUE P WAS NOT FOUND IN THE ARRAY”
18. STOP
Sorting
Sorting refers to the operation of arranging data in some given order, such as increasing or decreasing, with
numerical data or alphabetically, with character data. There exist a number of standard algorithms for this
purpose like – Bubble sort, Quick sort, Insertion sort, Selection sort, Heap sort, Bin sort etc. Each algorithm
has its advantages and conditions for its applicability. We will discuss here sorting with an input and an
output array.
Sorting with two arrays
In this sorting algorithm, the smallest value from the array is selected and is inserted into the second array.
Then next smallest value is inserted and so on.
1. Situation : Given an array of N number of integers to be sorted in another array.
2. Problem : To sort this array into an output array. Let the given array be A[] and result array
be B[]
3. Solution : Starting from the first smallest value to the last – pick one from the given array
and insert into the result array.
4. Algorithm:
1. START
2. int A[N] (loaded with values to be sorted in ascending order)
3. int B[N] (empty result array)
15. For More Visit: Https://www.ThesisScientist.com
4. I 1
5. M A[1]
6. IF (I > N) THEN GOTO 16
7. J 1
8. IF (J > N) THEN GOTO 13
9. IF (A[J] > M) THEN GOTO 11
10. M A[J]
11. J J + 1
12. GOTO 8
13. B[I] M
14. I I + 1
15. GOTO 6
16. STOP
Merging
Merging is an operation that takes two sorted (either ascending or descending order) arrays and creates a
third array, which contains all the values from both the arrays in sorted form. The arrays A and B, both
sorted in ascending order have been merged to get array C, as shown below.
A[5] of integers
5 7 9 11 18
B[7] of integers
1 4 10 13 17 19 24
C[12] of integers
1 4 5 7 9 10 11 13 17 18 19 24
In this merging algorithm, the smaller of the values from the two input arrays A and B, is taken at a time an
inserted into the result array C.
1. Situation : Given two arrays A and B of N and M number of integers in sorted order.
2. Problem : To merge these arrays into the result array C.
3. Solution: Take the smaller value of the two arrays A and B, at a time an insert
into the result array C.
4. Algorithm:
1. START
2. int A[N] (loaded with values sorted in ascending order)
16. For More Visit: Https://www.ThesisScientist.com
3. int B[N] (loaded with values sorted in ascending order)
4. int C[N + M] (result array with sufficient number of cells)
5. I 1
6. X A[1]
7. Y B[1}
8. IF (I > N+M) THEN GOTO 24
9. J 1
10. IF (J > N) THEN GOTO 15
11. IF (A[J] > X) THEN GOTO 13
12. X A[J]
13. J J + 1
14. GOTO 10
15. P 1
16. IF (P > M) THEN GOTO 21
17. IF (B[P] > Y) THEN GOTO 19
18. Y B[P]
19. P P + 1
20. GOTO 17
21. IF (X < Y) THEN C[I] <- X ELSE C{I} <- Y
22. I I + 1
23. GOTO 8
24. IF (J >= M) THEN GOTO 34
25. C[I] J
26. J J+1
27. I I+1
28. GOTO 24
29. IF (P >= N) THEN GOTO 34
30. C[I]B[P]
31. P P+1
32. I I+1
33. GOTO 29
17. For More Visit: Https://www.ThesisScientist.com
34. STOP
Text Processing and Pattern Search
Text Processing
A text or string is an array of characters. A terminating character, usually called a NULL character,
terminates each string. A string constant is denoted by any set of characters included in double-quote
marks. Thus, “Hello World!” is a text having 12 characters.
Text processing involves accessing individual character of the text and processing it in desired way. A
number of operations are possible on text. A complete set forms a subject of its own and is dealt with in
Chapter-20. We, shall, however, consider a few basic text-processing operations and corresponding
algorithms.
Example 5
1. Situation : Given an array of 5 characters A[].
2. Problem : To print the characters in reverse order.
3. Solution : Take the last value first and print, then second last and so on.
4. Algorithm:
1. START
2. int A[5] (loaded with a word WORLD)
3. I 5
4. IF (I < 1) THEN GOTO
5. PRINT A[I]
6. I I - 1
7. GOTO 4
8. STOP
Example 6
1. Situation : A large piece of text is given in a character array A[].
2. Problem : To print the count of a word stored in array B[] occurring in A[].
3. Solution: Take one word at a time from A[], if this is also the word contained in B[]
increase the count by 1, take next and so on.
4. Algorithm:
1. START
2. C 0
3. IF (No word left in A[]) THEN GOTO
18. For More Visit: Https://www.ThesisScientist.com
4. READ a word from A[] into C[]
5. IF (C[] = B[]) THEN C C + 1
6. GOTO 3
7. PRINT “COUNT =”, C
8. STOP
Pattern Matching
A text may be contained within another text. A piece of text forms a pattern of the constituent characters.
Searching for the occurrence of a text pattern into another text is known as pattern matching (more strictly
text-pattern matching). The pattern to be searched in a given text is known as sub-string.
The pattern to be searched is:
u s
Suppose, the given text is:
T H E B U S W A S C R O W D E D
The pattern was found at position 6 in the text.
When one is searching for a sub-string within a given text, there must be some method of returning the
position of the sub-string within the string. If the sub-string is found, this position is given by an integer
value indicating the character position of the left-most character of the sub-string being sought. Pattern
matching has been dealt with in detail in chapter 20.
Dynamic Data Structure Algorithms
A variable is a data storage space that can be identified by a name.
A group of variables that can be treated as a single unit and has some pre-specified properties as well as
operations defined on it, is known as data-structure. Array is one such data structure.
A data structure, like an array, whose number of elements must be specified before it is used, is called static
data structure. Such data structures cannot accommodate more values than its specified capacity.
A dynamic data structure, on the other hand, is a data structure whose number of elements need not be
specified before it is used. It can increase and decrease to accommodate varying number of elements. More
number of data members may be added and deleted as and when required.
This dynamic nature of a data structure is achieved by the use of the concept of pointer. A pointer is a
variable that can store address of another variable. Thus, a pointer introduces the possibility of assembling a
collection for storage building blocks, called nodes, into flexible structures. By alerting the values of
pointers, nodes can be attached, detached, and reassembled in patterns that grow and shrink as execution of
algorithm progresses. A node usually has data storage and at least a pointer storage associated with it. This
pointer points to the next node of the data structure.
Integer
type
storage
Pointer
type
storage
1st
Node
Integer
type
storage
Pointer
type
storage
2nd Node
Start pointer End pointer
19. For More Visit: Https://www.ThesisScientist.com
Under the array implementation, a fix set of nodes represented by an array is established at the start of
execution. A pointer to a node is represented by the relative position of the node within the array. The
disadvantage of that approach is two fold. First, the number of nodes that are needed often cannot be
predicted when an algorithm is written. Usually, the data with which the algorithm is executed determines
the number of nodes necessary. Thus no matter how many elements the array of nodes contains, it is always
possible that the algorithm will be executed with input that requires a larger number of nodes.
The second disadvantage of the array approach is that, whatever number of nodes is declared, it must
remain allocated to the program throughout its execution. For example, if 500 nodes of a given type are
declared, the amount of storage required for those 500 nodes is reserved for that purpose. If the algorithm
actually uses only hundred or even ten nodes in its execution the additional nodes are still reserved and their
storage cannot be any other purpose.
The solution to this problem is to allow nodes that are dynamic, rather than static. That is, when a node is
needed, storage is reserved for it, and when it is no longer needed, the storage is released. Thus the storage
for nodes that are no longer in use is available for another purpose. Also, no predefined limit on the number
of nodes is established. As long as sufficient storage is available to the job as a whole, part of that storage
can be reserved for use as a node.
There are a number of fundamental algorithms to carry out standard operations on these dynamic data
structures.
Traversal algorithm:
This algorithm scans the whole of data structure, starting with the start node to the end node, visiting each
node in between.
1. START
2. P Start node of the data structure
3. IF(Next-node-pointer of P is End-pointer) THEN GOTO 6
4. P Next-node-pointer of P
5. GOTO 3
6. STOP
Searching algorithm:
The algorithm to search a value (say X) in a data structure:
1. START
2. P Start node of the data structure
3. Q 0
4. IF(Next-node-pointer of P is End-pointer) THEN GOTO 6
5. IF (The value stored at this node is equal to X THEN Q 1 AND GOTO 8
6. P Next-node-pointer of P
20. For More Visit: Https://www.ThesisScientist.com
7. GOTO 3
8. IF(Q = 0) THEN PRINT “NOT FOUND” AND GOTO 10
9. PRINT “FOUND”
10. STOP
The other dynamic data structures such as linked list, trees and graph and the related algorithms are
discussed in later chapters of data structure.
Recursive Algorithms
We have seen that an algorithm is an entity having a STARting point and a STOPping point. Between these
two limits lies its definition. Executing an algorithm may be thought of as calling the algorithm passing
values to it and receiving what it returns after execution. When an algorithm uses or calls itself within its
definition, it is known as recursive algorithm. Recursion requires comparatively more space for execution.
In each call to itself, the algorithm creates a copy of itself and the associated variables.
In order to understand recursion in an algorithm, we will use slightly modified form of an algorithm. We
will give a name to an algorithm along with the inputs required by it to carry out its task as also the output
produced. Thus, using this notation, an algorithm would look like as follows:
Algorithm: ABC (input X, output Y)
1. START
2. –
3. –
4. OUTPUT Y
5. STOP
In this notation, wherever, ABC (the name of the algorithm) is mentioned elsewhere, it is replaced by the
output value of the algorithm ABC.
Let us see, through an example, how recursion works. A detailed treatment to recursion is done in later
chapters.
Example 7
1. Problem: Compute mathematical factorial of a positive integer N. Factorial of a +ve
integer N is given by the product N*(N-1)*(N-2)…..3*2*1
2. Solution: Since N! = N*(N – 1)! , execute the algorithm recursively on (N-1) etc.
3. Algorithm:
1. Algorithm: Factorial(input N, output Y)
2. START
3. Y N
4. IF (N = 1) THEN GOTO 5
5. Y Y * Factorial(N – 1)
21. For More Visit: Https://www.ThesisScientist.com
6. OUTPUT Y
7. STOP
Note that the algorithm Factorial is using itself in its definition. The value N is passed to the algorithm, the
algorithm executes itself with input value (N-1), which executes itself again with input value (N-2) and so
on, till it reaches 1. At this point it starts giving back the calculated values. Each output value is multiplied
and the final result is the factorial required as explained below with input value 3:
Algorithm: Factorial(input 3, output Y)
1. START
2. Y 3
3. IF (N = 1) THEN GOTO 5
4. Y 3 * Factorial(3 – 1) :Factorial 2 replaced by 2*1 = 2
5. OUTPUT Y
6. STOP
Algorithm: Factorial(input 2, output Y)
1. START
2. Y 2
3. IF (N = 1) THEN GOTO 5
4. Y 2 * Factorial(2 – 1) : Factorial 1 replaced by 1
5. OUTPUT Y
6. STOP
Algorithm: Factorial(input 1, output Y)
1. START
2. Y
3. IF (N = 1) THEN GOTO 5
4. Y 1 * Factorial(1 – 1)
5. OUTPUT Y
6. STOP
One important requirement for a recursive algorithm to be correct is that it does not generate an infinite
sequence calls on itself. Clearly, any algorithm that does generate such a sequence can never terminate.
Flow Charting
In the previous section we had described algorithms to be one of the ways for expressing a solution to the
given problem. As stated earlier, an algorithm may be expressed in any language provided that the executor
of the algorithm understands the language. For this purpose a sub-set of English language was used so far.
Output 3*2*1 = 6
22. For More Visit: Https://www.ThesisScientist.com
One can appreciate that the construct of sentences differs from person to person and leaves enough scope
for misinterpretation. Towards the standardization of the representation of the solution another tool of
expression, called Flowchart is available. It is very commonly used for logic representation.
Instead of using English like sentences, a flowchart uses a set of symbols to represent
the solution. This technique of representing the solution in pictorial form is called
flowcharting.
Flowchart Symbols
Flowcharts involve the use of simple geometric symbols to represent the beginning or end of an algorithm
(oval), a process (rectangle), a decision (rhombus), or an input/output process (parallelogram) as show in
following figure.
Symbols Specification
Start of End of the Program
Computational steps
Input or Output instruction
Decision making and
branching
Connectors of two parts of
a program
Flow Lines
To sum it up,
Flowcharts are tools to express algorithms.
Being graphical in nature, they are easier to understand, at a glance, than a narrative description.
Algorithms can be reviewed and corrected easily, if found faulty.
They provide effective algorithm documentation.
With a flowchart drawn, it is easier to explain an algorithm or discuss the solution.
Every flowchart must have an oval symbol at the beginning and one at the end, representing the
start and the end of the algorithm.
The following example will help you to understand the use of the different symbols in a flowchart.
Example 9
Problem: To find the sum of two numbers.
23. For More Visit: Https://www.ThesisScientist.com
Flowchart
Branching in Flowchart
Branching is the process of choosing one of the several available paths of computations. Consider the
following example.
Book title Author Price
Learn C XXX $45
Learn Data structure YYY $12
Programming
Techniques
ZZZ $ 24
You need to understand that in branching only one path can be selected, based upon a particular condition.
In the above case, the condition is that you need to buy the cheapest book. Only one book can satisfy this
condition and that happens to be the book “Learning Data structure”.
The branching condition is specified using the rhombus symbol. The condition can evaluate to either TRUE
or FALSE. If the condition evaluates to TRUE, then one action is performed else some other action is
performed.
Consider the following example.
Example 10
Problem Check if a given number is even or odd.
Flowchart
Start
Read A,B
A A + B
Print A
Stop
24. For More Visit: Https://www.ThesisScientist.com
According to the above flowchart, the remainder of the division is checked. If the remainder is 0, then the
path labeled “Yes” is selected and the even number is displayed. Else the path labeled “No” is selected and
the odd number is displayed.
Example 11
Problem: To choose the largest of three numbers a, b, c.
Flowchart
Start
Read
A
Divide A by
2
Is
Remainder = 0
Print
“Odd”
Print
“Even”
Stop
YesNo
Start
Read
A,B,C
IS A>B
Yes No
25. For More Visit: Https://www.ThesisScientist.com
Connecters
When flowchart is very big
It may not fit in a single page.
It may be difficult to interconnect all boxes directly.
In such cases, flowcharts can be broken into parts and connectors can be used to indicate the location of the
joins. Each connector symbol has a unique number that is specified within the connector and an arrow is
drawn into it. At the point where the chart is broken, another connector with the same number and an arrow
pointing away from it is drawn at the point where the broken chart is to be joined again.
Start
Read A,B,C
IS A>B
Yes No
IS A>C IS C>B
Print A Print C Print B
Stop
Yes NoYesN
o
26. For More Visit: Https://www.ThesisScientist.com
Connectors are inserted at the point where the flowchart splits. However, the location of each join is clearly
indicated by the corresponding number.
Flow Chart Types
SEQUENCE DECISIONS (Selection)
1 2
IS A>C IS C>B
Print A Print C Print B
Stop
Yes NoYesNo
1 2
27. For More Visit: Https://www.ThesisScientist.com
OTHER DECISION STRUCTURES
28. For More Visit: Https://www.ThesisScientist.com
LOOPING STRUCTURES
Repeat Until While/ While-end
Loop Using an Automatic Counter
29. For More Visit: Https://www.ThesisScientist.com
Practice of good flow-charting
1. There should be only one start and one stop symbol in the entire flow-charting
sequence.
2. For micro level flowcharts, start and stop symbols are represented by the pre-defined process‟s
identification code and the RETURN symbol.
3. The direction of arrows should always be from top to bottom and left to right.
4. Crossing of lines should always be avoided by using the connector syntax.
5. The symbols should be drawn neatly especially the arrowheads.
Benefits of Flowcharting
1. Easy to understand
2. Conciseness
1. Guide for coding
2. A tool for documentation
CASE STRUCTURE
30. For More Visit: Https://www.ThesisScientist.com
Limitations of Flowcharting
1 Not suitable for large algorithms spanning over multiple pages
2 Since it cannot be directly converted into programs, it is not worth the effort it requires.
Decision Table
In the section of flowchart we had mentioned that the following symbol could be used to illustrate the
condition and appropriate action.
To depict the logic in this way, one has to make sure of unambiguity in conditions and corresponding
actions. Ambiguity automatically arises when conditions are complex and if not handled properly, different
persons may interpret them in different way. Such complex conditions can unambiguously be depicted with
the help of Decision Tables.
A decision table is a table of contingencies and corresponding actions to be taken. The decision table has
three parts – Condition part, Action part and Rule part(see fig below).
Rules
Condition
(IF)
A
B
Actions
(THEN)
C
D
The conditions on which the decision has to be taken are listed in the conditionpart of the table(A,B). The
actions, similarly, are listed in the actions part (C,D). Each column of this table is a decision. To understand
the concept let us consider the following example.
Example 12
A company has decided to award a sum of Rs. 3500/- to its employees who meet the following criterion of
selection. “Working in the company for more than 5 years or salary is less than
Rs. 5000 and attendance is more than 300 days in the last year”.
Now the question is that who are the employees who get the sum of Rs 3500/- as an award.
A person may interpret the rule as: (Working in the company for more than five years OR salary is less than
Rs. 5000/-) and (Attendance of last year is more than 300 days)
Condition
Yes No
31. For More Visit: Https://www.ThesisScientist.com
If the rule is interpreted in this way then a person with: (Six years experience, Salary Rs. 4000- and 200
days of attendance will not get Rs. 3500/- as award.
Another person may interpret the rule in a different way as described below.
(Working in the company for more than five years) OR (Salary less than Rs. 5000/- and attendance of last
year is more than 300 days)
According to this interpretation of the rule the same person will get the award of Rs. 3500/- and this may be
what management wanted.
Readers should note that if the rules are not defined properly action might lead to erroneous results. Now
the question is, how to convey the rules and actions properly.
For this, let us analyze the condition in the following manner.
There are three conditions
(a) Working in the company for more than 5 years
(b) Salary less than Rs. 5000/-
(c) Attendance is more than 300 days
These conditions can either be true or false. “Y” represents the true condition and “N” false. All these three
conditions combined together can form 23
= 8 combinations. These conditions and possible values can be
represented in the form of a table:
C1 Working > 5 YEARS Y Y Y N Y N N N
C2 Salary < Rs. 5000 Y Y N Y N Y N N
C3 Attendance > 300
Days
Y N Y Y N N Y N
Against all these combinations of conditions there are two actions that can be taken.
(a) A sum of Rs. 3500/- is awarded
(b) A sum of Rs. 3500/- is not awarded
If these actions are combined with above table in which X against action represents that action is taken,
then the complete table will represent the rule clearly.
C1 Working > 5 YEARS Y Y Y N Y N N N
C2 Salary > Rs. 5000 Y Y N Y N Y N N
C3 Attendance > 300
Days
Y N Y Y N N Y N
A1 Sum Awarded X X X X X
C1 Sum Not Awarded X X X
This table representing the condition action and rules of action is termed as Decision Table.
Advantage of Decision Tables
1. They ensure that the user makes a clear and complete statement of the problem.
2. They provide an efficient means of communication between the user and the algorithm developer.
32. For More Visit: Https://www.ThesisScientist.com
3. They ensure that algorithm descriptions are structured in modules for ease.
4. Alterations and additions can easily be made (and, incidentally, the redrawing of flowcharts is
unnecessary since typists can make fair copies.
5. Decision tables provide a standard format, and deviations from the standard are immediately
apparent.
6. Algorithm can be automatically created from tables.
Pseudo-code
As stated earlier, an executor can execute an algorithm if he/she understands the language of the algorithm.
In case it is desired to execute the algorithm by computer, the algorithm must be translated into a computer
language. An algorithm written in computer language is known as a computer program. There exist a
number of programming languages, each with their own advantages and limitations.
However, since it is not known, a-priori, which programming language will be used for coding the
algorithm into a program, an intermediate language, which is somewhat between English and a true
programming language, is used. This language is called Pseudo-code.
Pseudo-code defines the logic to be used in the program, and is referred to as being "neat notes to yourself".
As notes the pseudo-code you write for a program is not the detail code for the program - if it were then it
would not be notes! Therefore, when you are creating the pseudo-code, you are providing enough
information to "jog your memory" as to what is needed, and work through the logic flow of the program.
Keep in mind also that someone else could, in theory, use the pseudo-code you create for writing the
program in FORTRAN, C++, Visual Basic, etc. Because of this reason, you don't want to use too many
"language specific" references, but rather use "generic" programming constructs.