Chapter II
Chapter II
Introduction :
Algorithm is a step by step logical approach and program is a set of instructions for a given
task.If you want to know more about this then read this article.
It should be simple, generic, and can be executed using the available resources.
It must be language-independent.
Recursive Algorithm – The Algorithm will call itself repeatedly until the problem is
solved. Example: Computing factorials
Greedy Algorithm – Algorithms that are used for solving optimization problems.
Example: Huffman tree
Brute Force Algorithm – It is one of the simplest algorithms in the concept. It iterates
all possible solutions to search for one or more possible solutions that may solve a
function. Example: Matching algorithm
Backtracking Algorithm – This algorithm follows an incremental approach to finding
a solution to a problem. The algorithm will solve problems recursively. It will find a
solution to a problem by solving one piece of the problem at a time. If one of the
solutions fails, it is removed, and the algorithm backtracks to find another solution.
Example: Queens Problem
Divide and Conquer Algorithm – As the name suggests, the algorithm is divided into
two parts. The first part divides the problem into smaller subproblems of the same type.
In the second part, these smaller problems are solved and then combined to produce the
final solution to the problem. Example: Merge sort
IV. What is a Program?
A program is a set of instructions a computer follows to perform a specified task. Many
programming languages can be used to write computer programs. Some popular
programming languages include pascal ,Python, Java, C++, JavaScript, PHP, and Ruby.
These high-level programming languages are human-readable and writable. These
languages are converted into low-level machine languages by compilers, interpreters, and
assemblers within the computer system.
A program tells the computer how to accept input, manipulate that input, and display the
output in some form that humans find useful.
The input will be the characters you type from the keyboard
The program will format the text and correct the spelling
5)Finiteness
The algorithm must stop, eventually.Stopping may mean that you get the expected output
OR you get a response that no solution is possible. Algorithms must terminate after a finite
number of steps.An algorithm should not be infinite and always terminate after definite
number of steps.
There is no point in developing an algorithm which is infinite as it will be useless for us.
6)Independent
An algorithm should have step-by-step directions, which should be independent of any
programming code.It should be such that it could be run on any of the programming
languages.
Thus,these are the characteristics that an algorithm should have for its fruitfulness.
VI. 1.Advantages of Algorithms:
It is easy to understand.
An algorithm is a step-wise representation of a solution to a given problem.
In an Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.
IV.2. Disadvantages of Algorithms:
Writing an algorithm takes a long time so it is time-consuming.
Understanding complex logic through algorithms can be very difficult.
Branching and Looping statements are difficult to show in Algorithms(imp).
VI.3. How to Design an Algorithm?
To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Then the algorithm is written with the help of the above parameters such that it solves the
problem.
Example: Consider the example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, to write an algorithm, its prerequisites must be fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while solving the problem:
The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem is solved: The sum of the three numbers
taken as the input i.e. a single integer value.
5. The solution to this problem, in the given constraints: The solution consists of adding
the 3 numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other
method.
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3
respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Step 3: Testing the algorithm by implementing it.
To test the algorithm, let’s implement it in C or pascal language.
Flowcharting is a tool developed in the computer industry, for showing the steps involved
in a process. A flowchart is a diagram made up of boxes, diamonds and other shapes,
connected by arrows - each shape represents a step in the process, and the arrows show the
order in which they occur. Flowcharting combines symbols and flowlines, to show
figuratively the operation of an algorithm.
In computing, there are dozens of different symbols used in flowcharting (there are even
national and international flowcharting symbol standards). In business process analysis, a
couple of symbols are sufficient. A box with text inside indicates a step in the process,
while a diamond with text represents a decision point. See the figure for an example.
If the flowchart is too messy to draw, try starting again, but leaving out all of the decision
points and concentrating on the simplest possible course. Then the session can go back
and add the decision points later. It may also be useful to start by drawing a high-level
flowchart for the whole organisation, with each box being a complete process that has to
be filled out later.
From this common understanding can come a number of things - process improvement
ideas will often arise spontaneously during a flowcharting session. And after the session,
the facilitator can also draw up a written procedure - a flowcharting session is a good way
of documenting a process.
Process improvement starts with an understanding of the process, and flowcharting is the
first step towards process understanding.
VI.5. Flowcharting Symbols
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.
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.
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.
Flowcharting uses symbols that have been in use for a number of years to represent the
type of operations and/or processes being performed. The standardised format provides a
common method for people to visualise problems together in the same manner. The use
of standardised symbols makes the flow charts easier to interpret, however, standardising
symbols is not as important as the sequence of activities that make up the process.
Flowcharting Tips
Chart the process the way it is really occurring. Do not document the way a written
process or a manager thinks the process happens.
People typically modify existing processes to enable a more efficient process. If the
desired or theoretical process is charted, problems with the existing process will not
be recognised and no improvements can be made.
The problem with this algorithm is that, some of the steps appear more than once, i.e. step
5 get second number, step 7, get third number, etc. One could shorten the algorithm or
flowchart as follows:
1. Start
2. Sum = 0
3. Get a value
4. sum = sum + value
5. Go to step 3 to get next Value
6. Output the sum
7. Stop
This algorithm and its corresponding flowchart are a bit shorter than the first one. In this
algorithm, step 3 to 5 will be repeated, where a number is obtained and added to sum.
Similarly the flowchart indicates a flowline being drawn back to the previous step
indicating that the portion of the flowchart is being repeated. One problem indicates that
these steps will be repeated endlessly, resulting in an endless algorithm or flowchart. The
algorithm needs to be improved to eliminate this problem. In order to solve this problem,
we need to add a last value to the list of numbers given. This value should be unique so
that, each time we get a value, we test the value to see if we have reached the last value.
In this way our algorithm will be a finite algorithm which ends in a finite number of steps
as shown below. There are many ways of making the algorithm finite.
The new list of numbers will be 26, 49, 498, 9387, 48962, 1, -1. The value –1 is a unique
number since all other numbers are positive.
1. Start
2. Sum = 0
3. Get a value
4. If the value is equal to –1, go to step 7
5. Add to sum ( sum = sum + value)
6. Go to step 3 to get next Value
7. Output the sum
8. Stop
Corresponding flowchart
3. DATA TYPES
Although some contemporary languages allow programmers to invent his own data
types, and define their related operations, there are a number of traditional data types
found in most languages:
Integer
Integers are numeric data items, which are either positive or negative including zero, i.e.
1, 488, -22, 0, 456. Some programming languages put restrictions on the magnitude of
integers which may be used in program instructions. These restrictions are usually
dependent on the size of the memory location of the computer in which the language
may run.
Real Numbers
There are two types of real numbers, Fixed-Point and Floating Point.
Fixed Point
Fixed point data items are numbers which have embedded decimal point i.e. 1.5,
458.4589, -0.569.
Floating Point
Floating point data items are numbers, which are, held as binary fractions by a computer.
The numbers are expressed in a form where you have a mantissa and an exponent, for
example
Floating point representation of data is used to overcome the restrictions placed on the
magnitude of numbers by the size of computer’s memory locations.
Character
Character data, sometimes referred to as “string” data, may consist of any digits, letters of
the alphabet or symbols which, the internal coding system of the computer is capable of
representing. Many programming languages require character data to be enclosed by
quotation marks when used in program instructions, for example PRINT “HAPPY NEW
YEAR”.
Boolean
Boolean data items are used as status indicators and may contain only one of two possible
values: True or False.
DATA ITEM
Data items are sometimes required to keep their values throughout the program, hence the
term constant. A constant is a specific value or character string used explicitly in an
operation. Consider the constant values 47.5, 1, and 13 in the example below.
Multiply … by 47.5
Add 1 to …
If … = 13
Writt “PLEASE INPUT TODAY’S DATE”
b) Variables and Variable names