Unit 1 Computer Programming
UNIT 1
CHARACTERISTICS OF COMPUTERS
The various characteristics of computers are as follows
1. Speed: Speed is the most important characteristics of computer .Computer having more speed
to perform jobs instantaneously.
2. Accuracy: The computers are perfect, accurate and precise. Accuracy signifies the reliability
of the hardware components of computers.
3. Automatic: A computer works automatically, once programs are stored and data are given to
it, constant supervision is not required.
4. Endurance: A computer works continuously and will not get tired and will not suffer from
lack of concentration.
5. Versatility: A computer can be put to work in various fields.
6. Reduction of cost: Though initial investment may be high, computer substantially reduces the
cost of transaction.
EVOLUTION OF COMPUTERS
ABACUS:
ABACUS uses movable beads stung on wires above and below a cross bar and its operation are
based on the idea of the place value notation. The beads of the counter represent digits. The
value of the digit in each position is determined by adding the values of the beads pressed against
the cross piece.
Pascal calculating machine:
It was the first real desktop calculating device that could add and subtract. It consists of a set of
toothed wheels or gears with each wheel or gear having digits 0 to 9 engraved on it. Arithmetic
operation could be performed by tunings these wheels.
Punched card Machine:
The presence and absence of the holes in the card represent the digits.
Charles Babbage’s Engine:
The machine took input from the punched card. The Analytical engine had a memory which will
perform arithmetic operations.
Unit 1 Computer Programming
Generations of Computers:
Generation in computer terminology is a change in technology a computer is/was being used.
Initially, the generation term was used to distinguish between varying hardware technologies.
But nowadays, generation includes both hardware
and software, which together make up an entire computer system.
There are totally five computer generations known till date. Each generation has been discussed
in detail along with their time period, characteristics.
Following are the main five generations of computers
S.N. Generation & Description
1 First Generation
The period of first generation : 1946-1959. Vaccum tube based.
2 Second Generation
The period of second generation : 1959-1965. Transistor based.
3 Third Generation
The period of third generation : 1965-1971. Integrated Circuit based.
4 Fourth Generation
The period of fourth generation : 1971-1980. VLSI microprocessor based.
5 Fifth Generation
The period of fifth generation : [Link] microprocessor based
First Generation:
The period of first generation was 1946-1959.
First generation of computer started with using vacuum tubes as the basic components for
memory and circuitry for CPU (Central Processing Unit). These tubes like electric bulbs
produced a lot of heat and were prone to frequent fusing of the installations, therefore, were very
expensive and could be afforded only by very large organizations. In this generation mainly
batch processing operating system were used. In this generation Punched cards, Paper tape,
Magnetic tape Input & Output device were used. There were Machine code and electric wired
board languages used.
The main features of First Generation are:
Vacuum tube technology
Unreliable
Supported Machine language only
Very costly
Generate lot of heat
Unit 1 Computer Programming
Slow Input/Output device
Huge size
Need of A.C.
Non portable
Consumed lot of electricity
Some computer of this generation were:
ENIAC
EDVAC
UNIVAC
IBM-701
IBM-650
Second Generation
The period of second generation was 1959-1965.
This generation using the transistor was cheaper, consumed less power, more compact in size,
more reliable and faster than the first generation machines made of vaccum tubes.
In this generation, magnetic cores were used as primary memory and magnetic tape and
magnetic disks as secondary storage devices. In this generation assembly language and high level
programming language like FORTRAN, COBOL was used.
There was Batch processing and Multiprogramming Operating system used.
The main features of Second Generation are:
Use of transistors
Reliable as compared to First generation computers
Smaller size as compared to First generation computers
Generate less heat as compared to First generation computers
Consumed less electricity as compared to First generation computers
Faster than first generation computers
Still very costly
A.C. needed
Support machine and assembly languages
Some computer of this generation was:
IBM 1620
IBM 7094
CDC 1604
CDC 3600
UNIVAC 1108
Third Generation
Unit 1 Computer Programming
The period of third generation was 1965-1971.
The third generation of computer is marked by the use of Integrated Circuits (IC's) in place of
transistors. A single I.C has many transistors, resistors and capacitors along with the associated
circuitry. The I.C was invented by Jack Kilby. This development made computers smaller in
size, reliable and efficient. In this generation Remote processing, Time-sharing, Real-time,
Multi-programming Operating System were used.
High level language (FORTRAN-II TO IV, COBOL, PASCAL PL/1, BASIC, ALGOL-68 etc.)
were used during this generation.
The main features of Third Generation are:
IC used
More reliable
Smaller size
Generate less heat
Faster
Lesser maintenance
Still costly
A.C needed
Consumed lesser electricity
Support high level language
Some computer of this generation was:
IBM-360 series
Honeywell-6000 series
PDP(Personal Data Processor)
IBM-370/168
TDC-316
Fourth Generation
The period of Fourth Generation was 1971-1980.
The fourth generation of computers is marked by the use of Very Large Scale Integrated (VLSI)
circuits. VLSI circuits having about 5000 transistors and other circuit elements and their
associated circuits on a single chip made it possible to
have microcomputers of fourth generation. Fourth Generation computers became more powerful,
compact, reliable, and affordable. As a result, it gave rise to personal computer (PC) revolution.
In this generation Time sharing, Real time, Networks, Distributed Operating System were used.
All the Higher level languages like C and C++, DBASE etc. were used in this generation.
The main features of Fourth Generation are:
VLSI technology used
Very cheap
Portable and reliable
Unit 1 Computer Programming
Use of PC's
Very small size
Pipeline processing
No A.C. needed
Concept of internet was introduced
Great developments in the fields of networks
Computers became easily available
Some computer of this generation was:
DEC 10
STAR 1000
PDP 11
CRAY-1(Super Computer)
CRAY-X-MP(Super Computer)
Fifth Generation
The period of Fifth Generation is 1980-till date.
In the fifth generation, the VLSI technology became ULSI (Ultra Large Scale Integration)
technology, resulting in the production of microprocessor chips having ten million electronic
components. This generation is based on parallel processing hardware and AI (Artificial
Intelligence) software. AI is an emerging branch in computer science, which interprets means
and method of making computers think like human beings.
All the Higher level languages like C and C++, Java, .Net etc. are used in this generation.
AI includes:
Robotics
Neural networks
Game Playing
Development of expert systems to make decisions in real life situations.
Natural language understanding and generation.
The main features of Fifth Generation are:
ULSI technology
Development of true artificial intelligence
Development of Natural language processing
Advancement in Parallel Processing
Advancement in Superconductor technology
More user friendly interfaces with multimedia features
Availability of very powerful and compact computers at cheaper rates
Some computer types of this generation are:
Desktop
Laptop
NoteBook
UltraBook
Unit 1 Computer Programming
ChromeBook
Classification of Computers
Computers differ based on their data processing abilities. They are classified according to
purpose, data handling and functionality.
According to purpose, computers are either general purpose or specific purpose. General purpose
computers are designed to perform a range of tasks.
They have the ability to store numerous programs, but lack in speed and efficiency. Specific
purpose computers are designed to handle a specific problem or to perform a specific task. A set
of instructions is built into the machine.
According to data handling, computers are analog, digital or hybrid. Analog computers work on
the principle of measuring, in which the measurements obtained are translated into data. Modern
analog computers usually employ electrical parameters, such as voltages, resistances or currents,
to represent the quantities being manipulated. Such computers do not deal directly with the
numbers. They measure continuous physical magnitudes. Digital computers are those that
operate with information, numerical or otherwise, represented in a digital form. Such computers
process data into a digital value (in 0s and 1s). They give the results with more accuracy and at a
faster rate. Hybrid computers incorporate the measuring feature of an analog computer and
counting feature of a digital computer. For computational purposes, these computers use analog
components and for storage, digital memories are used.
According to functionality, computers are classified as :
Analog Computer
An analog computer (spelt analogue in British English) is a form of computer that
uses continuous physical phenomena such as electrical, mechanical, or hydraulic quantities to
model the problem being solved
Digital Computer
A computer that performs calculations and logical operations with quantities represented as
digits, usually in the binary number system
Hybrid Computer (Analog + Digital)
Unit 1 Computer Programming
A combination of computers those are capable of inputting and outputting in both digital and
analog signals. A hybrid computer system setup offers a cost effective method of performing
complex simulations.
Computers are classified into four categories based on their size:
1. Mainframe Computers
2. Mini Computers
3. Micro Computers
4. Super Computers
1. Mainframe Computers.
Mainframe computers are built for general computing, directly serving the needs of
business and engineering. Although these computing systems are a step below super-
computers, they are still very fast and will process information at about 10 MIPS.
Mainframe computing systems are located in a centralized computing center with 20-
100+ workstations. This type of computer is still very expensive and is not readily found
in architectural/interior design offices.
They are big computer systems sensitive to temperature, humidity, dust etc.
Qualified & trained operators are required to operate them.
They have wide range of peripherals attached.
They have large storage capacity.
They can use wide variety of software.
They are not user friendly.
They can be used for more mathematical calculations.
They are installed in large commercial places or government organizations.
2
2. Mini Computers.
Minicomputers were developed in the 1960's resulting from advances in microchip
technology. Smaller and less expensive than mainframe computers, minicomputers run at
several MIPS and can support 5-20 users. CAD usage throughout the 1960's used
minicomputers due to their low cost and high performance. Examples of minicomputers
are: DEC PDP, VAX 11.
They have less memory & storage capacity than mainframe computers.
They offer limited range of peripherals.
Limited range of softwares can be used by them.
The end users can directly operate it.
They are not very sensitive to the external environment and hence are more
generalized.
They are used for data processing.
3. Micro Computers.
Microcomputers were invented in the 1970's and were generally used for home
computing and dedicated data processing workstations. Advances in technology have
improved microcomputer capabilities, resulting in the explosive growth of personal
computers in industry. In the 1980's many medium and small design firms were finally
Unit 1 Computer Programming
introduced to CAD as a direct result of the low cost and availability of microcomputers.
Examples are: IBM, Compaq, Dell, Gateway, and Apple Macintosh.
They brought revolution in the history of computers.
They are also known as Personal Computers.
They are cheap and user friendly.
The main components are Monitor, CPU, Keyboard, Mouse, Speakers, Modem and
Printer.
They are having limited peripherals attached to them.
This type of computers can use wide range of software.
They are used as desktops either in offices or even homes.
Their operation can be easily learnt by anyone having logical aptitude.
Children enjoy playing games & watching movies in these
computers.
Most popular micro computer’s processing chip manufacturing
company is Intel.
4. Super Computers.
Super-computers are very fast and powerful machines. Their internal architecture enables them
to run at the speed of tens of MIPS (Million Instructions per Second). Super-computers are very
expensive and for this reason are generally not used for CAD applications. Examples of super-
computers are: Cray and
CDC Cyber 205.
They are huge computers installed in space centers, nuclear power stations etc.
They are used for performing complex mathematical calculations. Only scientists
and mathematicians can operate them.
They are having huge memories & tremendous processing speed.
They are used for weather forecasting, animation graphics etc.
BASIC ORGANISATION OF A COMPUTER
A computer system consists of mainly four basic units; namely input unit, storage unit,
central processing unit and output unit. Central Processing unit further includes Arithmetic logic
unit and control unit, as shown in the figure. A computer performs five major operations or
functions irrespective of its size and make. These are
• it accepts data or instructions as input,
• it stores data and instruction
• it processes data as per the instructions,
• it controls all operations inside a computer, and
• it gives results in the form of output.
Unit 1 Computer Programming
INPUT UNIT
An I/O unit is a component of computer. The I/O unit is composed of two parts—input
unit and output unit. The input unit is responsible for providing input to the computer and the
output unit is for receiving output from the [Link] input unit gets the data and programs
from various input devices and makes them available for processing to other units of the
[Link] input data is provided through input devices, such as —keyboard, mouse, trackball
and joy-stick. Input data can also be provided by scanning images, voice recording, video
recording, [Link] of the kind of input data provided to a computer, all input devices
must translate the input data into a form that is understandable by the computer, i.e., in machine
readable form. The trans-formation of the input data to machine readable form is done by the
input interface of input [Link] input unit accepts input data from the user via input device,
Unit 1 Computer Programming
transforms the input data in computer acceptable form using input interface for the input device
and provides the transformed input data for processing.
Central Processing Unit (CPU)
The ALU and the CU of a computer system are jointly known as the central processing
unit. You may call CPU as the brain of any computer system. It is just like brain that takes all
major decisions, makes all sorts of calculations and directs different parts of the computer
functions by activating and controlling the operations.
Arithmetic Logical Unit :
After the data is entered through the input device it is stored in the primary storage unit.
The actual processing of the data and instruction are performed by Arithmetic Logical Unit. The
major operations performed by the ALU are addition, subtraction, multiplication, division, logic
and comparison. Data is transferred to ALU from storage unit when required. After processing
the output is returned back to storage unit for further processing or getting stored.
Control Unit (CU)
The next component of computer is the Control Unit, which acts like the supervisor
seeing that things are done in proper fashion. Control Unit is responsible for co ordinating
various operations using time signal. The control unit determines the sequence in which
computer programs and instructions are executed. Things like processing of programs stored in
the main memory, interpretation of the instructions and issuing of signals for other units of the
computer to execute them. It also acts as a switch board operator when several users access the
computer simultaneously. Thereby it coordinates the activities of computer’s peripheral
equipment as they perform the input and output.
Storage Unit:
The process of saving data and instructions permanently is known as storage. Data has to
be fed into the system before the actual processing starts. It is because the processing speed of
Central Processing Unit (CPU) is so fast that the data has to be provided to CPU with the same
speed. Therefore the data is first stored in the storage unit for faster access and processing. This
storage unit or the primary storage of the computer system is designed to do the above
functionality. It provides space for storing data and instructions.
Unit 1 Computer Programming
The storage unit performs the following major functions:
• All data and instructions are stored here before and after processing.
• Intermediate results of processing are also stored here.
Memory:
Computer’s memory can be classified into two types; primary memory and secondary
memory
Primary Memory can be further classified as RAM and ROM.
RAM or Random Access Memory is the unit in a computer system. It is the place in a
computer where the operating system, application programs and the data in current use are kept
temporarily so that they can be accessed by the computer’s processor. It is said to be ‘volatile’
since its contents are accessible only as long as the computer is on. The contents of RAM are no
more available once the computer is turned off.
ROM or Read Only Memory is a special type of memory which can only be read and
contents of which are not lost even when the computer is switched off. It typically contains
manufacturer’s instructions. Among other things, ROM also stores an initial program called the
‘bootstrap loader’ whose function is to start the operation of computer system once the power is
turned on.
Secondary Memory:
RAM is volatile memory having a limited storage capacity. Secondary/auxiliary memory
is storage other than the RAM. These include devices that are peripheral and are connected and
controlled by the computer to enable permanent storage of programs and data.
CD ROM
Secondary storage devices are of two types; magnetic and optical. Magnetic devices
include hard disks and optical storage devices are CDs, DVDs, Pen drive, Zip drive etc.
Hard Disk
Hard disks are made up of rigid material and are usually a stack of metal disks sealed in a
box. The hard disk and the hard disk drive exist together as a unit and is a permanent part of the
Unit 1 Computer Programming
computer where data and programs are saved. These disks have storage capacities ranging from
1GB to 80 GB and more. Hard disks are rewritable.
Compact Disk
Compact Disk (CD) is portable disk having data storage capacity between 650-700 MB.
It can hold large amount of information such as music, full-motion videos, and text etc. CDs can
be either read only or read write type.
Digital Video Disk
Digital Video Disk (DVD) is similar to a CD but has larger storage capacity and
enormous clarity. Depending upon the disk type it can store several Gigabytes of data. DVDs are
primarily used to store music or movies and can be played back on your television or the
computer too. These are not rewritable.
OUTPUT UNIT: :
The output unit is used for storing the result as output produced by the computer after
[Link] used to get the response or result of a process from the computer is called
[Link] unit is the communication between the user and the [Link] output unit of a
computer provides the information and results of a computation to the outside world. The
processed data, stored in the memory of the computer is sent to the output unit, which then
converts it into a form that can be understood by the user. The output is usually produced in one
of the two ways – on the display device, or on paper (hard copy).
NUMBER SYSTEMS
In the decimal number system, there are ten possible values that can appear in each digit
position, and so there are ten numerals required to represent the quantity in each digit
[Link] decimal numerals are the familiar zero through nine (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
Converting a decimal number to binary:
An example of the process: convert 3710 to binary
37 / 2 = 18 remainder 1 (least significant digit)
18 / 2 = 9 remainder 0
9 / 2 = 4 remainder 1
4 / 2 = 2 remainder 0
Unit 1 Computer Programming
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1 (most significant digit)
The resulting binary number is: 100101
Another example: convert 9310 to binary
93 / 2 = 46 remainder 1 (least significant digit)
46 / 2 = 23 remainder 0
23 / 2 = 11 remainder 1
11 / 2 = 5 remainder 1
5 / 2 = 2 remainder 1
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1 (most significant digit)
The resulting binary number is: 1011101
Conversion of Binary to Decimal
Converting a number from binary to decimal is quite easy. All that is required is to find the
decimal value of each binary digit position containing a 1 and add them up.
For example: convert 1011 to decimal.
1 0 1 1
23 22 21 20
1x8 0x4 1x2 1x1
8+0+2+1=11
Octal Representation
An octal number is represented using base 8. Octal representation is just a simple extension of
binary and decimal representations but using only the digits 0 to7. To convert an octal number to
a decimal number, it is required to multiply each octal digit by the appropriate power of 8 and
add the results.
octal representation of the decimal number 6410
Quotient Remainder
64/8 8 0 (LSB)
8/8 1 0
Unit 1 Computer Programming
1/8 0 1 (MSB)
Hence 6410 = 1008
What is the decimal value of the octal number 7118?
7 * 82 + 1 * 81 + 1 * 80 = 45710
Conversion of Decimal to Hexadecimal
Decimal numbers’ conversion to hexadecimal is similar to binary conversion. Decimal numbers
can be converted into hexadecimal format by the sum of weighted hex digits method and by
repeated division by 16. The sum of weighted hex digits method is suitable for small decimal
numbers of maximum 3 [Link] method of repeated division by 16 is preferable for the
conversion of larger numbers
The steps involved in the repeated division by 16 to obtain the hexadecimal equivalent are as
follows:
· Divide the decimal number by 16 and note the remainder.
Express the remainder as a hex digit.
· Repeat the process until the quotient is zero
Example:
Convert 94810 to hexadecimal
Process quotient remainder
948 / 16 59 4 (LSB)
59 / 16 3 11 (B)
3 / 16 0 3 (MSB)
94810 = 3B416
Convert 3 B 416 to decimal
3 B 4
162 161 160
(3 * 256 + 11 * 16 + 4)
=94810
Decimal fraction to binary fraction conversion
1. Successive multiplication method is used to convert a given fractional decimal
number to its equivalent binary fraction.
Unit 1 Computer Programming
2. In this method of conversion the fractional part of the given decimal number is
multiplied by 2 (i.e. the radix of binary number system).
3. The product obtained has an integer part and fractional part, the integer part here
is also referred as carry.
4. The carry that we obtain at each multiplication iteration becomes a digit in the
fractional binary number.
5. The fractional part obtained is again multiplied and the process is repeated until
the fractional part becomes zero or the number of multiplication iteration equals the
number of significant digits after the decimal point in the given fractional decimal
number.
6. The carry that we obtain at each stage are taken from first iteration to the last
iteration to form the numerals in the fractional binary number i.e. the carry obtained in
the first multiplication iteration is the most significant bit (MSB)after the decimal point
and the carry obtained in the last multiplication iteration is the least significant bit (LSB)
7. This procedure is illustrated in the following example.
Ex1: Convert (0.625)10 decimal number to binary number (?)2 using successive multiplication
method
1st Multiplication Iteration
Multiply 0.625 by 2
0.625 x 2 = 1.25(Product) Fractional part=0.25 Carry=1 (MSB)
2nd Multiplication Iteration
Multiply 0.25 by 2
0.25 x 2 = 0.50(Product) Fractional part = 0.50 Carry = 0
Unit 1 Computer Programming
3rd Multiplication Iteration
Multiply 0.50 by 2
0.50 x 2 = 1.00(Product) Fractional part = 1.00 Carry = 1 (LSB)
The fractional part in the 3rd iteration becomes zero and hence we stop the multiplication
iteration.
Carry from the 1st multiplication iteration becomes MSB and carry from 3rd iteration
becomes LSB.
Hence, the fractional binary number of the given fractional decimal number (0.625)10 is (0.101)2.
Conversion of binary number to decimal
Convert ( 101010.1111 )2= ( ? )10
= 101010.1111
↑ ↑
MSB LSB
= 1 x 2 + 0 x 2 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 . 1 x 2-1 + 1 x 2-2 + 1 x 2-3 + 1 x 2-4
5 4
= 1 x 32 + 0 x 16 + 1 x 8 + 0 x 4 + 1 x 2 + 0 x 1 . 1 x ( 1 / 2 ) + 1 x ( 1 / 4 ) + 1 x ( 1 / 8 ) + 1 x
( 1 / 16 )
= 32 + 0 + 8 + 0 + 2 + 0 . ( 1 / 2 ) + ( 1 / 4 ) + ( 1 / 8 ) + ( 1 / 16 )
= 32 + 8 + 2 . ( 0.5 ) + ( 0.25 ) + ( 0.125 ) + ( 0.0625 )
= 42 . 9375
Convert octal fraction to decimal fraction
Unit 1 Computer Programming
Problem 1: Convert ( 2 1 . 2 1 )8= ( ? )10
= 21. 2 1
↑ ↑
MSD LSD
= 2 x 81 + 1 x 80 . 2 x 8-1 + 1 x 8-2
= 2 x 8 + 1 x 1 . 2 x ( 1 / 8 ) + 1 x ( 1 / 64 )
= 16 + 1 . ( 0. 2 5 ) + ( 0 . 0 1 5 6 2 5 )
= 17 + 0. 265625
= 17 . 265625
Therefore ( 2 1 . 2 1 )8 = ( 1 7 . 2 6 5 6 2 5 )10
Convert hexadecimal fraction to decimal fraction
Problem 1: Convert ( E F . B 1 )16= ( ? )10
= EF. B 1
↑ ↑
MSD LSD
= E x 161 + F x 160 . B x 16-1 + 1 x 16-2
= 14 x 16 + 15 x 1 . 11 x ( 1 / 16 ) + 1 x ( 1 / 256 )
= 224 + 15 . ( 0. 6 8 7 5 ) + ( 0 . 0 0 3 9 0 6 2 5 )
= 239 + 0. 6914
= 239 . 691406
Therefore ( E F . B 1 )16 = ( 2 3 9 . 6 9 1 4 0 6 )10
Unit 1 Computer Programming
Conversion of binary to hexadecimal:
•Conversion of binary numbers to hex simply requires grouping bits in the binary
numbers into groups of four bits.
•Groups are formed beginning with the LSB and progressing to the MSB.
Example 1:
•1110 01112 = E716
Example 2:
•1 1000 1010 1000 01112
= 0001 1000 1010 1000 01112
= 1 8 A 8 716
Conversion of binary to octal
Conversion of binary numbers to octal simply requires grouping bits in the binary number into
groups of three bits
•Groups are formed beginning with the Least Significant Bit and progressing to the MSB.
Start from right hand side and proceed to left. If the left most group contains only a
single digit or a double digit, add zeroes to make it 3 digits.
•Example 1
11 100 1112
= 011 100 1112
= 3 4 78
Example 2
1 100 010 101 010 010 0012
= 001 100 010 101 010 010 0012
= 14252218
Conversion of Octal to binary
Convert each octal code into its 3 bit binary form.
Example 1: 261.4138
2 is replaced by 010,
6 is replaced by 110 and
1 is replaced by 001. The binary equivalent is ( 010 110 001)2
Convert the fractional part into the binary form, digit by digit. This yields (0.100 001 011)
Unit 1 Computer Programming
Now put it all together
010 110 001 . 100 001 0112
Conversion of hexadecimal to binary
Convert each hexadecimal digit into its 4 bit binary form.
Change A3D916 to a binary number.
A3D9
1010 0011 1101 1001 therefore, the binary number is 10100011110110012
Conversion of hexadecimal to Octal and vice Versa
Step 1: Convert Hexadecimal/Octal to Binary form
Step 2: Convert Binary to Octal/Hexadecimal form
The steps to convert 53.78 to hex are shown below:
101011.1112
Regroup the binary digits into groups of four and add zeros where needed to complete groups;
then convert the binary to hex.
Example 1:
0010 1011 . 11102
2 B . E 16
Solution: 53.78 equals 2B.E16
ALGORITHM
An algorithm is a finite set of well defined instructions or step by step procedure to
solve
a given problem.
Programs= Algorithms + Data.
Characteristics of an Algorithm
1)Input : It may accept zero or more inputs
2)Definiteness : Each instruction must be clear, well-defined and precise. There
should not be any ambiguity
Unit 1 Computer Programming
3)Effectiveness : Each instruction must be simple and be carried out in a finite
amount of time.
4)Finiteness : Algorithm should have finite sequence of instructions. That is, it
should end after a fixed time. It should not enter into an infinite loop
5)Output :It should produce at least one output.
Algorithm Notations
1)Name of algorithm: Specifies problem to be solved.
2)Step no- Identification tag of an instruction and it is an unsigned position
number.
3)Explanatory comment within the square brackets
4) Termination
Advantages of Algorithm
Effective communication: Since algorithm is written in English like
language, logic of the problem can be explained easily to others
Easy and efficient coding : It acts as a blue print during a program
development
Program debugging : Helps in debugging so that we can identify the
logical errors in a program
Program maintenance : maintaining the software becomes much easier
Example:To find the sum and average of three numbers
Algorithm
Step 1: Start
Step 2: Read a,b,c
Step 3: Find the sum using sum= a + b + c
Step 4:The average is calculated using avg=sum/3
Step 5: Print sum and avg
Step 6:Stop
Disadvantages of algorithm
Developing algorithm for large and complex problems would be time consuming
and difficult to understand
Unit 1 Computer Programming
Understanding complex logic through algorithms would be difficult
FLOWCHART
A Flowchart
Shows the logic of an algorithm
emphasizes individual steps and their interconnections
control flows from one action to the next
A schematic representation of a sequence of operations in a computer program. Flowcharts can
show how data flows from source documents through the computer to final distribution to users.
Program flowcharts show the sequence of instructions in a single program or subroutine.
Different symbols are used to draw each type of flowchart.
General rules for flowcharts
1. All symbols of the flowchart are connected by flow lines (note arrows, not lines)
2. Flowlines enter the top of the symbol and exit out the bottom, except for the
3. Decision symbol, which can have flow lines exiting from the bottom or the sides
4. Flowcharts are drawn so flow generally goes from top to bottom
5. The beginning and the end of the flowchart is indicated using the Terminal symbol
Symbols Used
Circle
Circles represents data in most flowchart diagrams. With GIS circles are used to distinguish data
input to a process and data that result from processing. It is convenient to use different colors to
identify various states of data: input, temporary, output, final product, and the like. ESRI's Model
Builder also uses circles in the case of processes that require constants or other input other than
spatial data
Lines with Arrows
You read a flowchart by following the lines with arrows from shape to shape. The lines with
arrows determine the flow through the chart. Flowcharts are usually drawn from top to bottom or
left to right. Numbering shapes is optional. Numbering is helpful if you have to refer to a shape
in a discussion. Numbering does not determine the flow of control. That is determined by the
lines with arrows. If two lines cross, you ignore the perpendicular line and continue following
the line to the next shape. If there may be confusion as to the path, you can insert a bridge or
crossover, which is a little bump on crossed lines.
Unit 1 Computer Programming
Rectangle
In most flowcharts, the rectangle is the most common shape. It is used to show a process, task,
action, or operation. It shows something that has to be done or an action that has to be taken. The
text in the rectangle almost always includes a verb.
Decision
A decision asks a question. The answer to the question determines which arrow you follow out
of the decision shape. Decisions can take place at points where results need to be evaluated
before the process may continue. Another use can be in the case of conditional operations
embedded in the flow.
Alternate Process
The rounded rectangle is sometimes used instead of the standard rectangle and can have the same
meaning. However, it is also used as an alternate process. The lines associated with the alternate
process are usually dashed. It allows you to specify a process in a sharp cornered rectangle and
an alternate process in a rounded rectangle. Some people also use the rounded rectangle for an
automated activity.
Manual input
This shape looks like a side view of a keyboard and often represents entering data into a
computer via a keyboard
Manual Operation
The trapezoid shape represents a manual operation. That is any operation or adjustment that has
to be done by hand rather than automatically.
Unit 1 Computer Programming
Preparation
This shape was originally used by IBM programmers to set a switch, modify an index register, or
initialize a routine. Currently you can use it to differentiate between steps that prepare for work
versus steps that actual do the work. It is probably best not to use this shape at all.
Database
A cylinder represents a data file or database. This shape can also represent the magnetic disc
itself. A hard drive is referred to as direct access storage since any sector on the drive can be
accessed. Compared to the circle symbol for data input and output the database is best used to
represent instances where collections of data are assembled in an organized manner like an ESRI
geodatabase.
Comment
This shape was used to add a comment or additional descriptive text to a software flowchart. A
dotted line should extend from the comment to the symbol it references. This shape is seldom
used today.
Input / Output
A parallelogram is used to show input or output. Examples of input are receiving a report, getting
an e-mail, getting an order, receiving data in some format, etc. Examples of output are generating
a report, sending an e-mail, faxing a message, etc.
Document
A rectangle with a curved bottom represents a document or report.
Unit 1 Computer Programming
Terminator
This shape tells you where the flowchart begins and ends. It shows the entry point of your
flowchart and the exit point. It is useful to represent an end result of a flow process. It needs a
suitably descriptive label to be useful.
Off-page connector
This shape means the flow continues on another page. A letter or page number in the shape tells
you where to go. It is an alternative to using a circle.
Flowchart for generating Fibonacci series
Advantages Of Using FLOWCHARTS: -
1. Communication: - Flowcharts are better way of communicating the logic of a system to
all concerned.
2. Effective analysis: - With the help of flowchart, problem can be analysed in more
effective way.
3. Proper documentation: - Program flowcharts serve as a good program documentation,
which is needed for various purposes.
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
Unit 1 Computer Programming
5. Proper Debugging: - The flowchart helps in debugging process.
6. Efficient Program Maintenance: - The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more efficiently on
that part
Disadvantages Of Using FLOWCHARTS: -
1. Complex logic: - Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
2. Alterations and Modifications: - If alterations are required the flowchart may require
re-drawing completely.
3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
4. The essentials of what is done can easily be lost in the technical details of how it is done.
Control structures
• Sequence – follow instructions from one line to the next without skipping over any lines
• Decision - if the answer to a question is “Yes” then one group of instructions is executed.
If the answer is “No,” then another is executed
• Looping – a series of instructions are executed over and over
Sequence Structure
Start
Read the
input
Do step 1
Do step 2
Stop
Unit 1 Computer Programming
Decision Structure
Looping Structure
PSEUDOCODE (or Program Design Language)
Consists of natural language-like statements that precisely describe the steps of an
algorithm or program
Focuses on the logic of the algorithm or program
Avoids language-specific elements
Written at a level so that the desired programming code can be generated almost
automatically from each statement
Steps are numbered. Subordinate numbers and/or indentation are used for dependent
statements in selection and repetition structures
Pseudocode Language Constructs
Unit 1 Computer Programming
Computation/Assignment
Compute var1 as the sum of x and y
Assign expression to var2
Increment counter1
Input/Output
Input: Get var1, var2, …
Output: Display var1, var2, …
Selection
Single-Selection IF
IF condition THEN (IF condition is true, then do subordinate statement 1, etc. If
condition is false, then skip statements)
statement 1
Double-Selection IF
IF condition THEN (IF condition is true, then do subordinate statement 1, etc. If
condition is false, then skip statements and execute statements under ELSE)
statement 1
ELSE (else if condition is not true, then do subordinate statement 2, etc.)
statement 2
statement 3
SWITCH expression TO
case 1: action1
case 2: action2
default: actionx
Repetition
WHILE condition (while condition is true, then do subordinate statements)
statement 1
DO – WHILE structure (like WHILE, but tests condition at the end of the loop. Thus,
statements in the structure will always be executed at least once.)
DO
statement 1
WHILE condition
FOR structure (a specialized version of WHILE for repeating execution of statements a
specific number of times)
FOR bounds on repetition
statement 1
Unit 1 Computer Programming
Pseudocode Example
Express an algorithm to get two numbers from the user (dividend and divisor),
testing to
make sure that the divisor number is not zero, and displaying their quotient using pseudocode
Declare variables: dividend, divisor, quotient
Prompt user to enter dividend and divisor
Get dividend and divisor
IF divisor is equal to zero, THEN
DO
Display error message, “divisor must be non-zero”
Prompt user to enter divisor
Get divisor
WHILE divisor is equal to zero
ENDIF
Display dividend and divisor
Calculate quotient as dividend/divisor
Display quotient