1. Chapter one
Basics of computer system
1.1 What is computer?
Computer is an advanced electronic device that takes raw data as input from the user and
processes it under the control of set of instructions (called program), gives the result
(output), and saves it for the future use.
1.2 Benefits of Computers
The three main benefits of using computers are :
1. Speed
2. Accuracy
3. Capacity to take large amount of work.
Computer work at a very high speed and are much faster than humans. The human equivalent of
an average computer would be more than one million mathematicians working 24 hours a
day. In addition to being fast, computers are very accurate. If the input and the instructions are
accurate the output will also be accurate.
Unlike humans, computers do not get bored or tired. The monotony of repetitive work for long
hours do not affect the computers.
Applications of Computers
Computers are used in various fields ranging from making cartoon films to space research. Some
applications of computers are:
1. Railway reservations
2. Banking and Accounts
3. Weather Forecasts
2. 4. Space Research
5. Medical Diagnoses
6. Chemical Analyses
Generally computer was developed from two parts, i.e from hard ware and software.
1.3 Hard ware component
Computer hardware includes all the electrical, mechanical, and the electronic parts of a
computer. Any part that we can see or touch is the hard ware. Computer hardware includes:
1. System Unit
2. Peripheral devices
3. Input devices i.e. keyboard, mouse etc.
4. Output devices i.e. Display Unit, printer etc.
5. Storage devices like hard disk, floppy disks etc.
1.4 Software component
The functioning of the computer is not dependent on hardware alone. So, what else is required?
It requires a set of instructions that tells the computer what is to be done with the input
data. In computer terminology, this set of instructions is called a program and one or more
programs is termed as software. This software can be categorized as system software and
application software.
Applications Software
Software specially suited for specific applications for example, railway and airline reservation,
billing, accounting or software which enables creation and storage of documents are termed as
application software.
System software
System software In the above airline reservation example, the clerk types your name and
other details through the keyboard. But how does this go to the system unit? This activity is
done by a set of instructions called the Operating Systems.
System software is a set of instructions that serves primarily as an intermediary between
computer hardware and application programs, and may also be directly manipulated by
knowledgeable users.
3. 1.3.1 Data representation in a computer
Computer uses a fixed number of bits to represent a piece of data, which could be a number, a
character, or others. A n-bit storage location can represent up to 2^n distinct entities. For
example, a 3-bit memory location can hold one of these eight binary
patterns: 000, 001, 010, 011, 100, 101, 110, or 111. Hence, it can represent at most 8
distinct entities. You could use them to represent numbers 0 to 7, numbers 8881 to 8888,
characters 'A' to 'H', or up to 8 kinds of fruits like apple, orange, banana; or up to 8 kinds of
animals like lion, tiger, etc.
Integers, for example, can be represented in 8-bit, 16-bit, 32-bit or 64-bit. An 8-bit unsigned
integer has a range of 0 to 255, while an 8-bit signed integer has a range of -128 to 127 - both
representing 256 distinct numbers.
It is important to note that a computer memory location merely stores a binary pattern. It is
entirely up to you, as the programmer, to decide on how these patterns are to be interpreted. For
example, the 8-bit binary pattern "0100 0001B" can be interpreted as an unsigned integer 65,
or an ASCII character 'A', or some secret information known only to you. The interpretation of
binary pattern is called data representation or encoding.
1.3.2 Integer representation
Computers use a fixed number of bits to represent an integer. The commonly-used bit-lengths for
integers are 8-bit, 16-bit, 32-bit or 64-bit. Besides bit-lengths, there are two representation
schemes for integers:
4. 1. Unsigned Integers: can represent zero and positive integers.
2. Signed Integers: can represent zero, positive and negative integers. Three representation
schemes had been proposed for signed integers:
1. Sign-Magnitude representation
2. 1's Complement representation
3. 2's Complement representation
n_bit unsigned integer
Unsigned integers can represent zero and positive integers, but not negative integers. The value
of an unsigned integer is interpreted as "the magnitude of its underlying binary pattern".
Example 1: Suppose that n=8 and the binary pattern is 0100 0001B, the value of this unsigned
integer is 1×2^0 + 1×2^6 = 65D.
Example 2: Suppose that n=16 and the binary pattern is 0001 0000 0000 1000B, the value of
this unsigned integer is 1×2^3 + 1×2^12 = 4104D.
Example 3: Suppose that n=16 and the binary pattern is 0000 0000 0000 0000B, the value of
this unsigned integer is 0.
An n-bit pattern can represent 2^n distinct integers. An n-bit unsigned integer can represent
integers from 0 to (2^n)-1, as tabulated below:
Signed integer
signed integers can represent zero, positive integers, as well as negative integers. Three
representation schemes are available for signed integers:
1. Sign-Magnitude representation
5. 2. 1's Complement representation
3. 2's Complement representation
n-bit sign integers in 1’s compliment representation
In 1's complement representation:
Again, the most significant bit (msb) is the sign bit, with value of 0 representing positive
integers and 1 representing negative integers.
The remaining n-1 bits represents the magnitude of the integer, as follows:
o For positive integers, the absolute value of the integer is equal to "the magnitude
of the (n-1)-bit binary pattern".
o For negative integers, the absolute value of the integer is equal to "the magnitude
of the complement (inverse) of the (n-1)-bit binary pattern" (hence called 1's
complement).
Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
Sign bit is 0 ⇒ positive
Absolute value is 100 0001B = 65D
Hence, the integer is +65D
Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
Sign bit is 1 ⇒ negative
Absolute value is the complement of 000 0001B, i.e., 111 1110B = 126D
Hence, the integer is -126D
n- bit sign integers in 2’s complement representation
6. In 2's complement representation:
Again, the most significant bit (msb) is the sign bit, with value of 0 representing positive
integers and 1 representing negative integers.
The remaining n-1 bits represents the magnitude of the integer, as follows:
o for positive integers, the absolute value of the integer is equal to "the magnitude
of the (n-1)-bit binary pattern".
o for negative integers, the absolute value of the integer is equal to "the magnitude
of the complement of the (n-1)-bit binary pattern plus one" (hence called 2's
complement).
Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
Sign bit is 0 ⇒ positive
Absolute value is 100 0001B = 65D
Hence, the integer is +65D
Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
Sign bit is 1 ⇒ negative
Absolute value is the complement of 000 0001B plus 1, i.e., 111 1110B + 1B = 127D
Hence, the integer is -127D
1.3.3 Decimal (base 10) number system
Decimal number system has ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, called digits. It
uses positional notation. That is, the least-significant digit (right-most digit) is of the order
7. of 10^0 (units or ones), the second right-most digit is of the order of 10^1 (tens), the third right-
most digit is of the order of 10^2 (hundreds), and so on. For example,
735 = 7×10^2 + 3×10^1 + 5×10^0
1.3.4 Binary (base 2) number system
Binary number system has two symbols: 0 and 1, called bits. It is also a positional notation, for
example,
10110B = 1×2^4 + 0×2^3 + 1×2^2 + 1×2^1 + 0×2^0
1.3.5 Hexadecimal (base 16) number system
Hexadecimal number system uses 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F,
called hex digits. It is a positional notation, for example,
A3EH = 10×16^2 + 3×16^1 + 14×16^0
Hexadecimal system is used as a compact form or shorthand for binary bits. Each hex digit is
equivalent to 4 binary bits, i.e., shorthand for 4 bits, as follows:
Conversion from hexadecimal to binary
Replace each hex digit by the 4 equivalent bits, for examples,
A3C5H = 1010 0011 1100 0101B
102AH = 0001 0000 0010 1010B
Conversion from binary to hexadecimal
Starting from the right-most bit (least-significant bit), replace each group of 4 bits by the
equivalent hex digit (pad the left-most bits with zero if necessary), for examples,
8. 1001001010B = 0010 0100 1010B = 24AH
10001011001011B = 0010 0010 1100 1011B = 22CBH
Conversion from base r to decimal (base 10)
Given a n-digit base r number: dn-1 dn-2 dn-3 ... d3 d2 d1 d0 (base r), the decimal equivalent is
given by:
dn-1 × r^(n-1) + dn-2 × r^(n-2) + ... + d1 × r^1 + d0 × r^0
For examples,
A1C2H = 10×16^3 + 1×16^2 + 12×16^1 + 2 = 41410 (base 10)
10110B = 1×2^4 + 1×2^2 + 1×2^1 = 22 (base 10)
Conversion from decimal (base r) to base r
Use repeated division/remainder. For example,
To convert 261D to hexadecimal:
261/16 => quotient=16 remainder=5
16/16 => quotient=1 remainder=0
1/16 => quotient=0 remainder=1 (quotient=0 stop)
Hence, 261D = 105H
The above procedure is actually applicable to conversion between any 2 base systems. For
example,
To convert 1023(base 4) to base 3:
1023(base 4)/3 => quotient=25D remainder=0
25D/3 => quotient=8D remainder=1
8D/3 => quotient=2D remainder=2
9. 2D/3 => quotient=0 remainder=2 (quotient=0 stop)
Hence, 1023(base 4) = 2210(base 3)
General conversion between two base system with factorial part
1. Separate the integral and the fractional parts.
2. For the integral part, divide by the target radix repeatably, and collect the ramainder in
reverse order.
3. For the fractional part, multiply the fractional part by the target radix repeatably, and
collect the integral part in the same order.
Example 1:
Convert 18.6875D to binary
Integral Part = 18D
18/2 => quotient=9 remainder=0
9/2 => quotient=4 remainder=1
4/2 => quotient=2 remainder=0
2/2 => quotient=1 remainder=0
1/2 => quotient=0 remainder=1 (quotient=0 stop)
Hence, 18D = 10010B
Fractional Part = .6875D
.6875*2=1.375 => whole number is 1
.375*2=0.75 => whole number is 0
.75*2=1.5 => whole number is 1
.5*2=1.0 => whole number is 1
Hence .6875D = .1011B
Therefore, 18.6875D = 10010.1011B
Example 2:
10. Convert 18.6875D to hexadecimal
Integral Part = 18D
18/16 => quotient=1 remainder=2
1/16 => quotient=0 remainder=1 (quotient=0 stop)
Hence, 18D = 12H
Fractional Part = .6875D
.6875*16=11.0 => whole number is 11D (BH)
Hence .6875D = .BH
Therefore, 18.6875D = 12.BH
Binary addition
Example1: Add 010011112 to 001000112 using signed-magnitude arithmetic.
We find 010011112 + 001000112= 011100102 in signed-magnitude representation.
Example2: Add 010011112 to 011000112 using signed-magnitude arithmetic.
We obtain the erroneous result of 79 + 99 = 50.
Binary subtractions
Example1: Subtract 010011112 from 011000112 using signed-magnitude arithmetic.
We find 011000112-010011112=000101002 in signed-magnitude representation.
Example2: Add 100100112 (-19) to 000011012 (+13) using signed-magnitude arithmetic.
11. The first number (the augend) is negative because its sign bit is set to 1. The second number (the
addend) is positive. What we are asked to do is in fact a subtraction. First, we determine which of
the two numbers is larger in magnitude and use that number for the augend. Its sign will be the
sign of the result.
Chapter two
Fundamentals of C++ programming
2.1 Introduction to computer program
A computer program is a sequence of instructions written using a Computer Programming
Language to perform a specified task by the computer.
A computer program is also called a computer software, which can range from two
lines to millions of lines of instructions.
Computer program instructions are also called program source code and computer
programming is also called program coding.
A computer without a computer program is just a dump box; it is programs that
make computers active.
There are hundreds of programming languages, which can be used to write computer programs
and following are a few of them:
C
C++
Java
Python
a computer can only understand
binary numbers, so we need to
convert a text file into a binary
format or machine languages, so the
computer can easily understand.
12. PHP
Perl
Ruby
Compiler
Actually, the computer cannot understand your program directly given in the text format, so we
need to convert this program in a binary format, which can be understood by the
computer. The conversion from text program to binary file is done by another software
called Compiler and this process of conversion from text formatted program to binary
format file is called program compilation. Finally, you can execute binary file to perform the
programmed task.
Interpreter
13. Compilers are required in case you are going to write your program in a programming
language that needs to be compiled into binary format before its execution. There are other
programming languages such as Python, PHP, and Perl, which do not need any
compilation into binary format, rather an interpreter can be used to read such programs
line by line and execute them directly without any further conversion. So, if you are
going to write your programs in PHP, Python, Perl, Ruby, etc., then you will need to install
their interpreters before you start programming.
2.2 Algorithm
An algorithm is an effective method of step by step procedure which expressed as a finite
set of well-defined instructions.
2.3 Flowchart
The flowchart is a diagram which visually presents the flow of data through processing systems.
This means by seeing a flow chart one can know the operations performed and the
sequence of these operations in a system. Algorithms are nothing but sequence of steps for
solving problems. So a flow chart can be used for representing an algorithm.
2.3.1 Flowchart Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language
Programs: Terminal, Process, input/output, Decision, Connector and Predefined Process.
14. Symbol Name Function
Process Indicates any type of internal
operation inside the Processor
or Memory
input/output Used for any Input / Output (I/
O) operation.
Decision Used to ask a question that can
be answered in a binary format
(Yes/No, True/False)
Connector Allows the flowchart to be
drawn without intersecting
lines.
Terminal Indicates the starting or ending
of the program, process, or
interrupt program
Flow Lines Shows direction of flow.
2.2.2 Type of Algorithms and flowchart
The algorithm and flowchart can be classified into three types of control structures. They are:
1. Sequence
The sequence is exemplified by sequence of statements place one after the other – the one above
or before another gets executed first. In flowcharts, sequence of statements is usually contained
in the rectangular process box.
Example 1: Write an algorithm and draw flowchart to read two numbers and find their sum.
Inputs to the algorithm:
First num1
Second num2
Expected output:
Sum of the two numbers
Algorithm:
Step1: Start
Step2: Readinput the first num1.
Step3: Readinput the second num2.
15. Step4: Sum num1+num2 // calculation of
sum
Step5: Print Sum
Step6: End
2. Branching (Selection)
It refers to a binary decision based on some condition. If the condition is true, one of the two
branches is explored; if the condition is false, the other alternative is taken. This is usually
represented by the ‘if-then’ construct in pseudo-codes and programs. In flowcharts, this is
represented by the diamond-shaped decision box.
example1: write algorithm and draw flowchart to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
3. Loop (Repetition)
16. It allows a statement or a sequence of statements to be repeatedly executed based on
some loop condition. It is represented by the ‘while’ and ‘for’ constructs in most
programming languages, for unbounded loops and bounded loops respectively. In the
flowcharts, a back arrow hints the presence of a loop.
Example1: An algorithm and draw flowchart to calculate even numbers between 9 and 99
1. Start
2. I ← 10
3. Write I in standard output
4. I ← I+2
5. If (I <=98) then go to line 3
6. End
2.4 Variables
A variable is a symbolic name for a memory location in which data can be stored and
subsequently recalled. Variables are used for holding data values so that they can be utilized in
various computations in a program. All variables have two important attributes:
A type which is established when the variable is defined (e.g., integer, real, character).
Once defined, the type of a C++ variable cannot be changed.
A value which can be changed by assigning a new value to the variable. The kind of
values a variable can assume depends on its type. For example, an integer variable can
only take integer values (e.g., 2, 100, - 12).
Example
#include <iostream>
int main ()
{
int workDays; //workdays is a variable,
int is data type
float workHours, payRate, weeklyPay; //
float is data type the other is variable
workDays = 5; // 5 is value of workDayays
in the memory allocation
workHours = 7.5;
17. payRate = 38.55;
weeklyPay = workDays * workHours *
payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << ' n';
}
2.5 Simple input/output
The most common way in which a program communicates with the outside world is through
simple, character - oriented Input/output (IO) operations. C++ provides two useful operators for
this purpose: >> for input and << for output. #include <iostream.h>
Example
int main ()
{
int workDays = 5;
float workHours = 7.5;
float payRate, weeklyPay;
cout << "What is the hourly pay rate? ";
cin >> payRate;
weeklyPay = workDays * workHours *
payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << ' n';
}
Integer numbers
An integer variable may be defined to be of type short , int , or long . For example, on the
author’s PC, a short uses 2 bytes, an int also 2 bytes, and a long 4 bytes.
short age = 20;
int salary = 65000;
long price = 4500000;
A literal integer (e.g., 1984 ) is always assumed to be of type int , unless it has an L or l and U
or u suffix, in which case it is treated as a long . For example:
1984L, 1984l, 1984U, 1984u, 1984LU, 1984ul
An integer is taken to be octal if it is preceded by a zero ( 0 ), and hexadecimal if it is preceded
by a 0x or 0X . For example:
92 // decimal
0134 // equivalent octal
0x5C // equivalent hexadecimal
Real number
A real variable may be defined to be of type float or double . The latter uses more bytes and
therefore offers a greater range and accuracy for representing real numbers. For example, on the
author’s PC, a float uses 4 and a double uses 8 bytes.
float interestRate = 0.06;
double pi = 3.141592654;
18. A literal real (e.g., 0.06 ) is always assumed to be of type double , unless it has an F or f
suffix, in which case it is treated as a float , or an L or l suffix, in which case it is treated as a
long double .
0.06F 0.06f 3.141592654L 3.141592654l
Characters
A character variable is defined to be of type char. A character variable occupies a single byte
which contains the code for the character. For example, the character A has the ASCII code 65,
and the character a has the ASCII code 97.
char ch = 'A';
A litera l character is written by enclosing the character between a pair of single quotes (e.g.,
'A' ). Nonprintable characters are represented using escape sequences. For example:
'n' // new line
'r' // carriage return
't' // horizontal tab
'v' // vertical tab
'b' // backspace
'f' // formfeed
Single and double quotes and the backslash
character can also use the escape
notation:
''' // single quote (')
'"' // double quote (")
'' // backslash ( )
Strings
A string is a consecutive sequence (i.e., array) of characters which are terminated by a null
character. A string variable is defined to be of type char* (i.e., a pointer to character). For
example, consider the definition:
char *str = "HELLO";
2.6 Names
Programming languages use names to refer to the various entities that make up a program. We
have already seen examples of an important category of such names (i.e., variable names). Other
categories include: function names, type names, and macro names.
C++ imposes the following rules for creating valid names (also called identifiers). A name
should consist of one or mor e characters, each of which may be a letter (i.e., 'A'- 'Z' and 'a'- 'z'),
a digit (i.e., '0' - '9'), or an underscore character ('_'), except that the first character may not be a
digit. Upper and lower case letters are distinct. For example:
salary // valid identifier
19. salary2 // valid identifier
2salary // invalid identifier (begins with a digit)
_salary // valid identifier
Salary // valid but distinct from salary
Reserved words
In C++ there are called reserved words or keywords and are summarized in Table.
Expressions in C++
C++ provides operators for composing arithmetic, relational, logical, bitwise, and conditional
expressions. It also provides operators which produce useful side effects, such as assignment,
increment, and decrement. C++ provides five basic arithmetic operators. These are summarized
in Table.
C++ provides six relational operators for comparing numeric quantities. These are summarized
in Table.
20. C++ provides three logical operators for combining logical expression. These are summarized in
Table.
C++ provides six bitwise operators for manipulating the individual bits in an integer quantity.
These are summarized in Table.
The next table illustrates bit sequences for the sample operands and results in the above Table. To
avoid worrying about the sign bit (which is machine dependent), it is common to declare a bit
sequence as an unsigned quantity:
unsigned char x = ' 011';
unsigned char y = ' 027';
21. The auto increment (++) and auto decrement (--) operators provide a convenient way of,
respectively, a dding and subtracting 1 from a numeric variable. These are summarized in Tabl e
suppose K=5.
The assignment operator is used for storing a value at some memory location (typically denoted
by a variable). Its left operand should be left value, and its right operand may be an arbitrary
expression. The latter is evaluated and the outcome is stored in the location denoted by the left
value.
Any number of assignments can be concatenated in this fashion to form one expression. For
example:
22. int m, n, p;
m = n = p = 100; // means: n = (m = (p = 100));
m = (n = p = 100) + 2; // means: m = (n = (p = 100)) + 2; i.e m=100
m + = n = p = 10; // means: m = m + (n = p = 10)