C++ Chapter 1
C++ Chapter 1
Introduction to computer
What is computer?
Software is the series of instructions that tells the hardware how to perform tasks.
Characteristics of computer
The characteristics of computers that have made them so powerful and universally useful
are speed, accuracy, diligence, versatility and storage capacity. Let us discuss them
briefly.
Speed
Accuracy
Nowadays computers are being used for surgical purposes, which need almost a hundred
percent accuracy. From this we can understand that computers are accurate and
consistent. Unless there is an error in the input data or unreliable program the computer
processes with a very high accuracy.
Diligence
Unlike human beings, computers are highly consistent. They do not suffer from human
traits of boredom and tiredness resulting in lack of concentration. Computers, therefore,
are better than human beings in performing voluminous and repetitive jobs.
Versatility
Computers are versatile machines and are capable of performing any task as long as it
can be broken down into a series of logical steps. The presence of computers can be seen
in almost every sphere – Railway/Air reservation, Banks, Hotels, Weather forecasting
and many more.
Automatic
Once necessary information and program is fed, the computer performs processing
without human intervention.
Storage capacity
Today’s computers can store large volumes of data. A piece of information once recorded
(or stored) in the computer, can never be forgotten and can be retrieved almost
instantaneously. And the time it took to retrieve or process single information is not more
than a micro or nanoseconds.
o They need 200KW of power to operate, which contains 18,000 Vacuum Tubes
manufactured separately
Computer storage switched from magnetic cores to integrated circuit boards that
provide modularity (expandable storage) and compatibility (interchangeable
equipment
New input/output methods such as optical scanning and plotters.
SSD Punched card Magnetic Tape Magnetic disk Mass storage device
Language Machine& assembly Fortran, COBOL etc. Structured language Application oriented
Operating system Operator control Batch system Application oriented Time sharing
ENIAC, UNIVAC,
Examples UDVAC IBM7090, 7094 IBM system Late IBM product
Introduction to computer & programming
Types of Computers
Analog
Analog computers operate by measuring physical properties. They deal with continuous
variables; they don’t compute directly with numbers, rather, they operate by measuring physical
magnitude such as pressure, temperature, voltage, current etc.
Digital
Digital computers deal with discrete variables; they operate by counting rather than measuring.
They operate directly up on numbers (or digits) that represent numbers, letters, or other special
symbols.
Hybrid
Hybrid computers inherit the best features of both analog and digital computers. Usually the
Input is continuous data (analog). Since Digital Processing is more accurate, processing takes
place digitally. The processed information – the output – could be either digital or analog,
depending on the user preference or the type of application.
Page 6
Introduction to computer & programming
Super computer
The term supercomputer has been coined to describe a category of extremely powerful computer
designed for high-speed processing. A supercomputer is generally characterized as being the
fastest, most powerful, and most expensive computer.
Mainframe computer
Mainframe computers are large, powerful computers that are physically larger than micros and
minis and usually have processors with faster instruction processing speeds. For example, they
may be able to process from 10 to 200 million instructions per second (MIPS). Mainframe
computers also support multiple users and are expensive.
Mini computer
Minicomputers are midrange computers that are larger and more powerful than most
microcomputers but are smaller and less powerful than mainframe computer systems.
Minicomputers are being used for a large number of business and scientific applications. They
are popularly used in scientific laboratories, research centers, universities and colleges,
engineering firms, industrial process monitoring and control, etc.
Micro computers
The smallest computers ever produced in the history of computers are microcomputers. Since
they are designed to be used by a single user, they have the least capacity as compared to the
other types of computers. They are also the least expensive of all types. There two different types
of microcomputers are desktop computers and portable computers (laptops, notebook computers
and palmtops)
Page 7
Introduction to computer & programming
Computers can be applied or used for different purposes. Based upon their application, they are
classified as special purpose or general purpose computers.
Limitation of computers
Page 8
Introduction to computer & programming
Computer Hardware
Computer hardware is the physical part of the computer system that can be seen and felt. The
hardware part of a computer system is composed of a number of interacted physical parts. E.g.
keyboard, mouse, CPU
i) Input Devises
ii) Storage Dévies
iii) Output Dévies
iv) Central Processing Unit (CPU)
Page 9
Introduction to computer & programming
i) Input Devices
Input devices are used to enter information into computer. They convert the data we give
them into the form that can be manipulated in the computer (electronic format).
Some examples of input devices are Keyboard, mouse, scanner, Bar Code Reader, mice
1 GHz = 1000MHZ
Current CPUs are as fast as 2-3GHz (2-3 billion cycles per second)
Control Unit
As human brain controls the body, control unit controls the computer hardware. Control
Unit does not execute instruction by itself, i.e. does not carry out instruction processing,
but it directs other processing elements to execute instructions.
Page 10
Introduction to computer & programming
Arithmetic operation
Logic Operation
Arithmetic operation: this includes mathematical operations like addition, subtraction,
multiplication, division, etc. If you give your computer the instruction 2+3, this will be
included in arithmetic operation and it is executed by Arithmetic Unit.
Logical Operation: this is concerned with the comparison of data and it is called logical
operation. It includes operators like less than, greater than, equal to, less or equal to,
greater or equal to, different from, etc. e.g. if mark>80, grade is 'A'.
A processor register (CPU register) is one of a small set of data holding places that are part of the
computer processor. A register may hold an instruction, a storage address, or any kind of data (such as a
bit sequence or individual characters). Some instructions specify registers as part of the instruction.
Examples
Page 11
Introduction to computer & programming
Primary memory, also called Main memory, refers to integrated circuit that stores program
instructions and data. The CPU closely works with the main memory to perform its activities.
Memory stores three things:
Why is it volatile? It uses electric power to store data. When you write anything on your
computer, first it is stored on RAM. When you save the file, it is transferred into secondary
storage.
RAM has differing capacity, the common ones being 128, 256, and 512.It is directly accessible
by CPU. It is called RAM because each memory location can be accessed randomly using
memory address. Each unit in RAM has memory address by which it can be easily
accessed/referenced.
Page 12
Introduction to computer & programming
ROM stores data and programs that are permanently required by the computer. They have
programs built into them at the factory and that program could not be changed or erased by the
user, but read. It is non-volatile, read-only (not changeable) memory. Read-only means data can't
be altered or erased but read.
2. Secondary Storage
Secondary storage (also called auxiliary storage) supplements the primary memory. It takes
many forms. It includes punched cards, punched paper tape, magnetic tape, magnetic disk and optical
disk. Based on information access, secondary storage devices are divided into two:
Computer Software
Computer hardware is directed by a set of instructions. Without these instructions, computers can
do nothing. These set of instructions are called software (also called programs). One or more
programs are termed as software. We use programming languages to write these instructions.
Examples of programming languages include C, C++, Visual Basic, Java, etc.
System Software
Application Software
System Software
System software consists of programs that are related to controlling the actual operations
of the computer equipment/resource.
Page 13
Introduction to computer & programming
Operating System
Utility Software
Language translators
Operating system manage resources, provides a user interface, and run application software. It
organizes resources such as keyboard, mouse, printer, monitor, etc. It also presents GUI (Graphical User
Interface) to the user for easy use of computer. It makes complex hardware more users friendly i.e. it acts
between the user and hardware. Operating system coordinates the activity between the user and the
computer
Utilities software is programs that make computing easier. They perform specific tasks related
to managing computer resources or files. There are different utility programs:
Page 14
Introduction to computer & programming
Application Software
Application software performs useful work for the user. These useful works could be:
Information is data that is organized, meaningful, and useful. To process data into information,
a computer uses hardware and software.
Data Representation
Every computer stores numbers, letters, & other special characters in a coded form. But a
computer stores those data’s in a binary number system. There are two main code forms that
every number, letter and special character is represented in a computer system. Before discussing
those codes let have a look at binary number system.
Computers are electronic devices that use electrical patterns to represent numbers. Modern digital
computers recognize only two electrical states—ON and OFF— but their memories contain
millions of transistors that can be either on or off. Working with numbers in computers is like
making numbers out of a row of lights that can be switched on and off independently. Because
there are only two ways to represent a number, computers use the binary number system (base 2
or radix 2).
Page 15
Introduction to computer & programming
12 C 14 1100
13 D 15 1101
14 E 16 1110
15 F 17 1111
16 10 20 0001 0000
1. The 1st reason is that the electronic & electrical components, by their very nature, operate in
a binary mode. Information is handled in the computer by electronic/electrical components such
as transistors, semiconductors, wires, etc all of which can only indicate 2 states or conditions –
on(1) or off(0).
Transistors are either conducting (1) or non conducting (0); a voltage is present (1) or absent (0)
in wire. The binary number system, which has only two digits (0&1), is most suitable for
expressing the two possible states
2. The second reason is that the computer circuits only have to handle two binary digits rather
than ten decimal digits. This greatly simplifies the internal circuit design of computers, resulting
in less expensive & more reliable circuits.
Page 16
Introduction to computer & programming
3. Finally, the binary system is used because everything that can be done in decimal number system
(addition, subtraction, division & multiplication) can also be done in binary number system.
When data is stored, processed, or communicated within the computer system, it is “packed” in units.
Arranged from the smallest to the largest, the units are called bits, bytes, & words.
Bits
A bit is a single binary digit. A bit may have a value of 0 or 1. In a computer, a switch or transistor
that is off represents a 0, and a switch or transistor that is on represents a 1. A bit is represented by the
numbers 1, & 0, which correspond to the states on & off, true & false, or yes & no.
Bytes
Most computers work with groups of 8 bits, which is called a byte. To make it easier to read, the 8
binary digits in a byte are divided into two groups of four, called nibbles, when they are written.
One byte may hold binary numbers ranging in value from 0000 0000 (base 2) to 1111 1111 (base 2),
or from 0 (base 10) to 255 (base 10). Counting 0 as a value, one byte can contain 256 values. For
many computer variables, the maximum value is 255 because the computer wants to store the value in
a single byte, or you are limited to 256 choices.
Words
Page 17
Introduction to computer & programming
Page 18
Introduction to computer & programming
0-9 = 0-9
A = 10 D = 13
B = 11 E = 14
C = 12 F = 15
59 is 3B in hexadecimal
Page 19
Introduction to computer & programming
Solution:
90 dividing by 16 and collecting the remainder of the digit we can verify it.
A binary number can be converted into octal or hexadecimal number using a shortcut
method. The shortcut method is based on the following information:-
Steps
Steps 1 - Divide the decimal number to be converted by the value of the new base.
Step 2 - Get the remainder from Step 1 as the rightmost digit (least significant digit) of
new base number.
Step 3 - Divide the quotient of the previous divide by the new base.
Step 4 - Record the remainder from Step 3 as the next digit (to the left) of the new base
number.
Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in
Step 3.
The last remainder thus obtained will be the most significant digit (MSD) of the new base
number.
1.3.1 Example
Step 1 29 / 2 14 1
Step 2 14 / 2 7 0
Page 20
Introduction to computer & programming
Step 3 7/2 3 1
Step 4 3/2 1 1
Step 5 1/2 0 1
As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that
the first remainder becomes the least significant digit (LSD) and the last remainder becomes the
most significant digit (MSD).
Steps
Steps 1 - Determine the column (positional) value of each digit (this depends on the
position of the digit and the base of the number system).
Step 2 - Multiply the obtained column values (in Step 1) by the digits in the
corresponding columns.
Step 3 - Sum the products calculated in Step 2. The total is the equivalent value in
decimal.
1.4.1 Example
Page 21
Introduction to computer & programming
Steps
1.5.1 Example
Step 1 21 / 2 10 1
Step 2 10 / 2 5 0
Step 3 5 / 2 2 1
Step 4 2 / 2 1 0
Step 5 1 / 2 0 1
Page 22
Introduction to computer & programming
Steps
Step 1 - Divide the binary digits into groups of three (starting from the right).
Step 2 - Convert each group of three binary digits to one octal digit.
1.5.4 Example
10
Step 1 010 101
1012
Step 2 101012 28 58
An easy way to convert from binary to octal is to group binary digits into sets of three, starting
with the least significant (rightmost) digits.
011 100 101 Pad the most significant digits with zeros if
necessary to complete a group of three.
Octal: 0 1 2 3 4 5 6 7
Page 23
Introduction to computer & programming
Steps
Steps 1 - Convert each octal digit to a 3-digit binary number (the octal digits may be
treated as decimal for this conversion).
Step 2 - Combine all the resulting binary groups (of 3 digits each) into a single binary
number.
1.6.1 Example
Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.
Octal: 0 1 2 3 4 5 6 7
Page 24
Introduction to computer & programming
Octal = 3 4 5
Steps
Step 1 - Divide the binary digits into groups of four (starting from the right).
Step 2 - Convert each group of four binary digits to one hexadecimal symbol.
1.6.2 Example
An equally easy way to convert from binary to hexadecimal is to group binary digits into sets of
four, starting with the least significant (rightmost) digits.
Page 25
Introduction to computer & programming
Hexadecimal: 0 1 2 3 4 5 6 7
Hexadecimal: 8 9 A B C D E F
Hexadecimal = E 5 = E5 hex
Example
Steps
Steps 1 - Convert each hexadecimal digit to a 4-digit binary number (the hexadecimal
digits may be treated as decimal for this conversion).
Step 2 - Combine all the resulting binary groups (of 4 digits each) into a single binary
number.
1.8.1 Example
Page 26
Introduction to computer & programming
Hexadecimal: 0 1 2 3 4 5 6 7
Hexadecimal: 8 9 A B C D E F
Hexadecimal = A 2 D E
When converting from octal to hexadecimal, it is often easier to first convert the octal number
into binary and then from binary into hexadecimal. For example, to convert 345 octal into hex:
Drop any leading zeros or pad with leading zeros to get groups of four binary digits (bits):
Binary 011100101 = 1110 0101
Page 27
Introduction to computer & programming
Hexadecimal: 0 1 2 3 4 5 6 7
Hexadecimal: 8 9 A B C D E F
Hexadecimal = E 5 = E5 hex
Therefore, through a two-step conversion process, octal 345 equals binary 011100101 equals
hexadecimal E5.
When converting from hexadecimal to octal, it is often easier to first convert the hexadecimal
number into binary and then from binary into octal. For example, to convert A2DE hex into
octal:
Add leading zeros or remove leading zeros to group into sets of three binary digits.
Octal: 0 1 2 3 4 5 6 7
Page 28
Introduction to computer & programming
Hexadecimal: 0 1 2 3 4 5 6 7
Decimal: 012 3 4 5 6 7
Hexadecimal: 8 9 A B C D E F
Decimal: 8 9 10 11 12 13 14 15
A2DE hexadecimal:
= ((A) * 163) + (2 * 162) + ((D) * 161) + ((E) * 160)
= (10 * 163) + (2 * 162) + (13 * 161) + (14 * 160)
= (10 * 4096) + (2 * 256) + (13 * 16) + (14 * 1)
= 40960 + 512 + 208 + 14
= 41694 decimal
Hexadecimal
Decimal Number Operation Quotient Remainder
Result
1792 ÷ 16 = 112 0 0
112 ÷ 16 = 7 0 00
Page 29
Introduction to computer & programming
7 ÷ 16 = 0 7 700
0 Done.
The only addition to the algorithm when converting from decimal to hexadecimal is that a table
must be used to obtain the hexadecimal digit if the remainder is greater than decimal 9.
Decimal: 01234567
Hexadecimal: 0 1 2 3 4 5 6 7
Decimal: 8 9 10 11 12 13 14 15
Hexadecimal: 8 9 A B C D E F
Page 30