1 Algorithm
1 Algorithm
In mathematics and computing, an algorithm is a procedure (a finite set of well-defined instructions) for
accomplishing some task which, given an initial state, will terminate in a defined end-state. Generally the
word is used to include all definite procedures for solving problems or performing tasks.
Algorithms are essential to the way computers process information, because a computer program is
essentially an algorithm that tells the computer what specific steps to perform (in which sequence) in
order to carry out a specified task, such as calculating employees’ salaries or printing students’ result
cards.
Alternatively an algorithm may be defined as a specific set of instructions for carrying out a procedure or
solving a problem, usually with the requirement that the procedure terminate at some point. Algorithms
can be expressed in many kinds of notation, including natural languages like English or Gujarati,
flowcharts, and programming languages like C. Informally, the concept of an algorithm is often
illustrated by the example of a recipe, although many algorithms are much more complex; algorithms
often have steps that repeat (iterate) or require decisions (such as comparison).
Step 1: BEGIN
Step 2: Read the value of n
Step 3: Divide n by 2 and store the remainder in rem
Step 4: If rem is 0, go to step 7
Step 5: Print "n is an odd number"
Step 6: Go to step 8
Step 7: Print "n is an even number"
Step 8: END
Step 1: BEGIN
Step 2: Read the value of a,b,c
Step 3: If a>b, go to step 4 else go to step 7
Step 4: If a>c, go to step 5 else go to step 6
Step 5: Print "a is greatest", go to step 9
Step 6: Print "c is greatest", go to step 9
Step 7: If b>c, go to step 8 else go to step 6
Step 8: Print "b is greatest"
Step 9: END
Example of an algorithm to find whether the given number is prime or composite
Step 1: BEGIN
Step 2: Read the value of n
Step 3: If n=2, go to step 9
Step 4: Let I=3, Repeat steps 5 through 8
Step 5: Divide n by I and store the remainder in rem
Step 6: If rem is 0, Print "n is a composite number", go to step 10
Step 7: Increment I by 1
Step 8: If I < n, go to step 5
Step 9: Print "n is a prime number"
Step 10: END
Step 1: BEGIN
Step 2: Read the value of n
Step 3: Divide n by 7 and store the remainder in rem
Step 4: If rem is 0, go to step 5, else go to step 6
Step 5: Print "n is a multiple of 7", go to step 7
Step 6: Print "n is not a multiple of 7“
Step 7: END
FLOWCHARTING
Meaning of a flowchart
A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed
to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating
computer solutions. These flowcharts play a vital role in the programming of a problem and are quite
helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it
becomes easy to write the program in any high level language. Often we see how flowcharts are helpful
in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better
documentation of a complex program.
Symbols used to draw flowcharts
Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be
developed when required. Some standard symbols, which are frequently required for flowcharting, are
given below:
Start
Input a, b, c
Is a > b?
YES NO
Is a > c? Is b > c?
NO NO
Output c is greatest
YES YES
End
End End
Flowchart to find sum of n numbers
Start
Input n
I=0
SUM = 0
I=I+1
SUM = SUM + I
Is I < n?
YES
NO
Output SUM
End
Advantages and disadvantages of using flowcharts
Load actual_salary
Add overtime_salary
Store gross_salary
Although such code is clearer to humans, it is incomprehensible to computers until translated to machine
language. An assembler is a translator program that translates assembly-language programs to machine
language. Computer usage increased rapidly with the arrival of assembly languages, but programmers
still had to use many instructions to accomplish even simpler tasks. To speed up the programming
process, high-level languages were developed in which single statements could be written to accomplish
big tasks. Translator programs called compilers convert high-level language programs into machine
language. High-level languages allow programmers to write instructions that look almost like everyday
English and contain commonly used mathematical notations. A program that calculates gross salary may
be written in a high-level language as follows:
From the programmer’s viewpoint, high-level languages are preferable to machine and assembly
language. C, C++ and Java are among the most widely used high-level programming languages.
The program that translates a high-level language program into machine language is called a compiler.
The process of compiling a high-level language program into machine language can take a considerable
amount of time. Interpreters were developed to execute high-level language programs directly.
Procedural Programming Vs. Object Oriented Programming
Divisions into functions : When programs become larger, a long single Introduction of objects : The fundamental idea
list of instructions is difficult to understand and so it is broken down into behind object-oriented languages is to combine
smaller units. For this reason, function was adopted as a way to make into a single unit both data and functions that
programs more comprehensible to their human creators. Later on, the idea operate on that data. Such a unit is called an
of breaking a program into functions can be further extended by grouping object.
a number of functions together into a large entity called a module.
Poor programming model : There are two related problems with Introduction of encapsulation : In OOP, an
procedural programming. First, functions have unrestricted access to object’s functions, called member functions,
global data. Second, unrelated functions and data. In large programs, typically provide the only way to access its data.
where there are many functions and many data variables, it becomes You cannot access data directly. The data is
difficult to manage both. Any accidental alteration to the global data may hidden, so it is safe from any accidental
change the logic of the program and thus result in an error. change. Data and functions are said to be
encapsulated into a single entity.
Poor real-world modeling : Third and more important problem with the Better real-world modeling : Complex real
procedural program paradigm is that its arrangement of separate data and world objects have both attributes and
functions does a poor job of modeling things in the real world. In the behavior. A car can have attributes like Color,
physical world we deal with objects such as people and cars. Such objects ModelNo etc. It also behaves in a certain
are not like data and they are not like functions. manner when we speed up or slow down. This
is better handled by objects.
New data types & extensibility : We cannot create new data types in Allows user-defined data types and thus
procedural programming. Computer languages have many built-in data extensible : By introduction of objects and
types but we cannot invent a data type of our own by mixing some or all operator overloading, you can not only create
of the built-in types. In other words, you cannot extend programs of your data types of your own but also make them
own. behave as similar as do built-in types.