II PUC Computer Science Textbook 1-190
II PUC Computer Science Textbook 1-190
%
*,-,#.2.))%
#
REVISED EDITION
2017
I
Director’s Message
Dear Students,
Sd/-
C. Shikha, IAS
Director
Department of Pre University Education
Bengaluru
II
Preface
Dr. P. Nagabhushan
BE,MTech,Phd,FIE,FIETE
Professor, Department of Studies in Computer Science
Chief Nodal Officer,
University of Mysore, Mysore-570 006
III
Computer Science Syllabus Review and Textbook Development
Committee
Department of Pre-University Education, Govt. of Karnataka
AUTHORS
Rajappa
Chairperson
Empress Govt. Pre-University College
Tumakuru
Co-ordinator:
Santus Xavio B K
Lecturer
Stracey Memorial Pre-University College
#52, St. Marks' Road, Bengaluru
Members:
Nagaraje Urs
Lecturer
Maharaja Govt. Pre-University College
JLB Road, Mysore
Sharon Mednora
Lecturer
Govt. Pre-University College
18th Cross, Malleshwaram
Bengaluru
Ravindra K V
Lecturer
Govt. Pre-University College
Sagara, Shimoga District
Nagamani M R
Lecturer
SBRR Mahajana Pre-University College
Jayalakshmipuram, Mysore
Padmashree R
Lecturer
NMKRV Pre-University College
Jayanagar, Bengaluru
IV
Acknowledgements
I am very fortunate to have esteemed personality Dr. Nagabhushan P,
Professor, Dept. of Computer Science, Manasagangothri, University of
Mysore who guided us in shaping the syllabus. We are also grateful to
our reviewers Dr. Somashekar M T, Associate Professor, Dept. of Com-
puter Science, Bengaluru University and Prof. Mukundappa , HOD, Dept.
of Computer Science, University Science College, Tumkur. I whole
heartedly thank them personally and on behalf of the committee.
I also recall the support of my wife Shylaja. Last but not least, I acknowl-
edge the support of family and friends of our committee members who
directly or indirectly helped us in bringing out this text book.
Rajappa
Chairperson
Computer Science Syllabus Review and
Textbook Development Committee
Department of Pre-University Education
Govt. of Karnataka
V
Government of Karnataka
Computer Science Syllabus Review Committee
Department of Pre-University Education, Bengaluru
Rajappa Santus Xavio B K
Chairperson Co-ordinator
Empress Govt. Pre-University College Stracey Memorial PU College
Tumakuru Bengaluru
e-mail: rajappatumkur @gmail.com [email protected]
Reviewers:
Dr. Nagabhushan P, Dept. of Computer Science, Manasagangothri, Mysore
Dr. Somashekar M T, Dept. Computer Science, Gnanabharathi, Bengaluru
Prof. Mukundappa, HOD, Dept of Computer Science, University Science
College, Tumkur
Members:
Nagaraje Urs
Lecturer
Maharaja Govt. Pre-University College
JLB Road, Mysore
Sharon Mednora
Lecturer
Govt. Pre-University College
18th Cross, Malleshwaram, Bengaluru
Ravindra K V
Lecturer
Govt. Pre-University College
Sagara, Shimoga District
Naveen Kumar B
Lecturer
Govt. Pre-University College
13th Cross, Malleshwaram, Bengaluru
Nagamani M R
Lecturer
SBRR Mahajana Pre-University College
Jayalakshmipuram, Mysore
Padmashree R
Lecturer
NMKRV Pre-University College
Jayanagar, Bengaluru
VI
Chapter No Topics Page No.
UNIT A BACKDROP OF COMPUTERS 35 Hrs
Chapter 1 Typical configuration of Computer system 1
1.1 Introduction 2
1.2 Motherboard 4
1.2.1 Introduction to Motherboard 4
1.2.2 Types of Motherboards 5
1.2.3 Components of Motherboard 6
1.3 Memory 15
1.4 Power supply to the computer system 18
1.5 Assembling the computer system 19
Chapter 2 Boolean Algebra 24
2.1 Introduction 25
2.2 Binary valued quantities-constants and variables 25
2.3 Logical operations 26
2.3.1 Logical functions or compound statements 26
2.3.2 Logical operators 26
2.4 Evaluation of Boolean expressions using truth table 29
2.4.1 Basic logic gates 34
2.5 Basic postulates of Boolean Algebra (with proof ) 36
2.5.1 Properties of 0 and 1 38
2.5.2 Indempotence law 41
2.5.3 Involution law 42
2.5.4 Complementarity law 43
2.5.5 Commutative law 44
2.5.6 Associative law 46
2.5.7 Distributive law-different forms 47
2.5.8 Absorption law 48
2.6 De Morgan’s theorems 50
2.6.1 De Morgan’s I theorem 50
2.6.2 De Morgan’s II theorem 51
2.6.3 Applications of De Morgan’s theorems 53
2.6.4 Basic duality of Boolean algebra 53
2.7 Derivation of Boolean expressions 54
2.7.1 Min terms 54
2.7.2 Max terms 57
2.7.3 Canonical expressions 57
2.7.4 Minimization of Boolean expressions 64
2.8 Simplification using Karnaugh map 65
2.8.1 Sum-of-product reduction using Karnaugh map 66
2.8.2 Product-of-sum reduction using Karnaugh map 76
VII
Chapter 3 Logic gates 86
3.1 Introduction 87
3.1.1 Invertor (NOT gate) 87
3.1.2 OR gate 88
3.1.3 AND Gate 89
3.2 Derived Gates 90
3.2.1 NOR Gate 90
3.2.2 NAND Gate 91
3.2.3 XOR Gate 91
3.2.4 XNOR Gate 93
3.2.5 Circuit diagram 94
3.2.6 NAND,NOR as universal Gates 95
Chapter 4 DATA STRUCTURE 103
4.1 Introduction 104
4.2 Data representation 104
4.3 Classification of Data structures 105
4.3.1 Primitive Data structure 105
4.3.2 Operations on primitive data structures 106
4.3.3 Non-primitive Data structures 106
4.3.4 Linear data structure 107
4.3.5 Non-Linear data structure 107
4.4 Operations on linear data structures 107
4.5 Arrays 107
4.5.1 Types of array Memory representation of data 108
4.5.2 One dimensional array 109
4.5.3 Memory representation one dimensional array 109
4.5.4 Basic operations on one-dimensional array 109
4.5.5 Traversing using one dimension array 110
4.5.6 Searching an element 111
4.5.7 Insertion of an element 114
4.5.8 Deletion of an element 116
4.5.9 Sorting the elements 118
4.5.10 Two dimension Array 119
4.6 Stacks 123
4.6.1 Introduction 123
4.6.2 Representation of stacks in memory 124
4.6.3 Operations on stacks 127
4.6.4 Applications of Stacks 128
4.7 Queues 133
4.7.1 Introduction 133
4.7.2 Types of Queues 134
VIII
4.7.3 Operations on queues 136
4.7.4 Memory representation of queues 136
4.7.5 Applications of Queues 138
4.8 Linked lists 139
4.8.1 Introduction 139
4.8.2 Types of linked list 139
4.8.3 Operations on single linked lists 141
4.9 Non-Linear data structure 153
4.9.1 Introduction 153
4.9.2 Trees 153
4.9.3 Graphs 155
UNIT B COMPUTING IN C++ 45 Hrs
Chapter 5 Review of C++ 158
5.1 Review of c++ language 158
5.2 Fundementals of c++ 160
5.3 Structure of c++ program 164
5.4 Libraray functions 164
5.5 Data types 165
5.6 Input and output operations 166
5.7 Control statements 167
5.8 Arrays 169
5.9 Functions 172
5.10 User-defined Functions 175
5.11 Structures 180
Chapter 6 Basic concepts of OOP 181
6.1 Introduction 182
6.2 Basic concepts of OOP 183
6.2.1 Objects 183
6.2.2 classses 183
6.2.3 Data Abstraction 184
6.2.4 Data Encapsulation 184
6.2.5 Inheritance 184
6.2.6 Overloading 185
6.2.7 Polymorphism 185
6.2.8 Dynamic Biding 185
6.2.9 Message passing 185
6.3 Advantages of OOP over earlier programming methods 186
6.4 Limitations of OOP 186
6.5 Applications of OOP 186
IX
Chapter 7 Classes and objects 189
7.1 Introduction 190
7.2 Definition and declaration of classes and objects 191
7.3 Access specifiers (scope of class & its members) 193
7.3.1 Private 193
7.3.2 Public 193
7.3.3 Protected 194
7.4 Members of the class 194
7.5 Member functions 196
7.5.1 Member functions inside class definition 196
7.5.2 Member functions out side class definition 196
7.6 Defining objects of a class 198
7.7 Arrays as members of class 199
7.8 Array of objects 200
7.9 Objects as function arguments 202
7.10 Diffrences between structures and classes in C++ 204
Chapter 8 Function overloading 207
8.1 Introduction 208
8.2 Need for function overloading 208
8.3 Definition and declaration of overloaded function 208
8.4 Restrictions on overloaded function 209
8.4.1 Calling over loaded functions 209
8.5 Other functions in a class 210
8.5.1 Inline function 211
8.5.2 Friend function 212
Chapter 9 Constructor and Destructor 216
9.1 Introduction 217
9.2 Declaration and definition of constructor 218
9.3 Types of constructors 219
9.3.1 Default constructor 219
9.3.2 Parameterized constructor 221
9.3.3 Copy constructor 224
9.4 Constructor overloading 227
9.5 Destructor 228
Chapter 10 Inheritance(Extending classes) 232
10.1 Introduction 233
10.2 Base class 233
10.3 Derived class 233
10.3.1 Defining derived class 233
10.3.2 Public derived class 234
10.3.3 Private derived class 235
X
10.3.4 Protected dervied class 235
10.4 Visibility modes 235
10.4.1 Public inheritance 235
10.4.2 Private inheritance 236
10.4.3 Protected inheritance 236
10.5 Levels of inheritance 236
10.5.1 Single level inheritance 237
10.5.2 Multilevel inheritance 237
10.5.3 Multiple inheritance 238
10.5.4 Hierarchical inheritance 238
10.5.5 Hybrid inheritance 238
10.6 Relationship between classes 240
10.6.1 Virtual base classes 240
10.6.2 Abstract classes 242
10.6.3 Constructors in Derived classses 242
10.6.4 Destructors in Dervied classes 243
Chapter 11 Pointers 247
11.1 Introduction 248
11.2 Memory representation of pointers 248
11.3 Declaration & initialization of pointers 249
11.4 Address operator 249
11.5 Pointer operator(indirection operator) 250
11.6 Pointer arithmetic 250
11.7 Pointer and arrays 251
11.8 Arrays of pointers 252
11.9 Pointers to strings 253
11.10 Pointer as function parameters 253
11.11 Pointer and structures 254
11.12 Memory allocation of pointers(static and dynamic) 254
11.12.1 Static allocation of memory 254
11.12.2 Dynamic allocation of memory-new and delete 254
11.13 Free store (heap memory) 256
11.14 Memory leak 256
11.15 Self Referential Structure 256
11.16 Pointers and functions 257
11.16.1 Invoking functions by passing the references 257
11.16.2 Invoking functions by passing the pointers 258
11.17 Memory comes and memory goes 260
11.18 Pointer and objects 260
11.19 this pointer 261
XI
Chapter 12 Data file handling 266
12.1 Introduction 267
12.2 Header files(fstream.h) 268
12.2.1 Classes for file stream operation 269
12.3 Types of data files 270
12.3.1 Text file 270
12.3.2 Binary file 270
12.4 Opening & closing files 270
12.4.1 Opening file using constructor 270
12.4.2 Using open() 271
12.4.3 File modes -In ,out, app modes 272
12.4.4 closing files 273
12.5 Input and output operation in text files 273
12.6 Detecting end of file 275
12.7 File pointers -tellg(), tellp(), seekg(), seekp() functions 275
UNIT C LARGE DATA, DATABASE & QUERIES 20 HRs
Chapter 13 Database Concepts 282
13.1 Introduction 283
13.2 Appllications of database 284
13.3 Origin of Data : Facts,data,information,features 284
13.4 Evolution of database 285
13.5 Data processing cycle 286
13.6 Data base terms 287
13.7 Data Types in DBMS 288
13.8 DBMS 289
13.9 Data abstraction 291
13.10 Data independence 293
13.11 Database Model 298
13.11.1 Hierarchial data model 299
13.11.2 Network data Model 300
13.11.3 Relational Data model 301
13.12 Codd's Rules 302
13.13 Logical data concepts 304
13.13.1 Normalization 304
13.13.2 Entity-relationship Model 308
13.13.3 Cardinality 313
13.14 KEYS-Primary,Secondary,Candidate,Foreign, Alternate 315
13.15 Relational Algebra 318
13.16 Data warehousing 327
13.17 Data Mining 329
XII
Chapter 14 Structured Query Language 333
14.1 Introduction 334
14.1.1 SQL Architecture 335
14.2 SQL commands 337
14.2.1 DDL 337
14.2.2 DML 338
14.3 Data types in SQL 339
14.3.1 Exact Numeric data types 339
14.3.2 Floating point Numeric data types 339
14.3.3 Date and time data types 339
14.3.4 Character and string data type 340
14.4 Operators in SQL 340
14.4.1 SQL arithemetic operators 340
14.4.2 Comparison operators 341
14.4.3 Logical operators 341
14.5 SQL expressions 342
14.5.1 SQL Boolean Expression 342
14.5.2 SQL Numeric expression 343
14.5.3 Date expression 343
14.6 SQL constraints 344
14.6.1 Primary key 344
14.6.2 Foreign Key or Referential integrity 346
14.6.3 Not NULL constraint 347
14.6.4 Unique Key 347
14.6.5 Check constraint 348
14.7 Implementation of SQL Commands 348
14.7.1 Create table statement 348
14.7.2 Alter 349
14.7.3 Insert Statement 350
14.7.4 Select statement 351
14.7.5 AND operator 353
14.7.6 OR operator 354
14.7.7 Update statement 354
14.7.8 Delete Statement 356
14.7.9 Order by 357
14.7.10 Group by 357
14.7.11 Distinct statement 359
14.7.12 Join 361
14.7.13 NULL 363
14.8.1 Create View 365
14.9.1 Commit 365
XIII
14.10 DCL commands 365
14.10.1 Grant command 365
14.10.2 Revoke command 366
14.11 Built-In Function 368
14.11.1 Single row function 368
14.11.2 Group function 368
UNIT D ADVANCED CONCEPTS IN COMMUNICATION TECHNOLOGY 20Hrs
Chapter 15 Networking Concepts 375
15.1 Introduction 376
15.1.1 Networking Goals 376
15.1.2 Need of networking 376
15.2.1 Arpanet 376
15.2.2 OSI reference Model 377
15.2.3 TCP/IP 378
15.3.1 HTTP 380
15.3.2 FTP 381
15.3.3 SLIP 381
15.4.1 Internet 381
15.4.2 Interspace 382
15.4.3 Elementary terminologies of networking 382
15.4.4 Types of services 382
15.4.5 Types of networking 383
15.4.6 Networking Topologies 386
15.4.7 Transmission medium 393
15.4.8 Switching techniques 398
15.4.9 Communication modes 399
15.4.10 Networking devices 400
15.5.1 Gateway 403
15.6.1 SIM 404
15.7.1 GPRS 406
15.8.1 Applications of Networking 410
15.8.2 Wi-fi 411
15.9.1 Network security 411
15.10.1 Cookies 413
15.11.1 Virus 413
Chapter 16 Internet and Open source concepts 416
16.1 Introduction 416
16.1.2 Free software 417
16.1.3 Open source software 417
16.1.4 OSS and FLOSS 418
XIV
16.1.5 GNU 419
16.1.6 FSF 419
16.2.1 OSI 419
16.2.2 W3C 419
16.2.3 Proprietary software 419
16.2.4 www 420
16.2.5 Telnet 420
16.2.6 Web browser 420
16.2.7 Webserver 420
16.2.8 Webpage 421
16.3 URL and domain 421
16.4 E-Commerce 422
16.4.1 Types of E-commerce 424
16.4.2 Advantages of e-commerce 425
16.5 IPR issues 426
Chapter 17 Web designing 428
17.1 Introduction 429
17.1.1 HTML structure 430
17.2.1 Advanced HTML tags/commands 432
17.2.2 Text formating 432
17.2.3 Resizing text 432
17.2.4 Example for resizing text 433
17.2.5 Text layout 434
17.2.6 Number listing 435
17.2.7 Links 437
17.2.8 Inserting images 438
17.2.9 Background 439
17.2.10 Background color and fixed images 440
17.2.11 Tables 440
17.2.12 Frames 442
17.2.13 Forms 443
17.2.14 Settings and text fields 444
17.3.1 Web Hosting 447
17.3.2 Domain registration 448
17.4.1 Uploading HTML file 449
17.5.1 XML 450
17.6.1 DYNAMIC HTML 451
17.7.1 Web scripting 453
Model Question Paper 455
XV
DESIGN OF QUESTION PAPER
Time : 3Hours 15 Minutes(of which minutes for reading the questions Paper).
Max.Marks:70
Weightage to Objectives:
XVI
VSA SA LA E Total
UNIT DESCRIPTION
(1 Mark) (2 Marks) (3 Marks) (5 Marks) Marks
BACKDROP OF
A
35 Hrs COMPUTERS 3 2 3 3 31
Chapter 1 Typical
5 Hrs configuration of 1 ------ 1 ------- 4
Computer system
Chapter 2 Boolean algebra
-------- 2 ----- 1 09
10 Hrs
Chapter 3 Logic Gates
1 ------ 1 ------- 04
5 Hrs
Chapter 4 Data structures
1 ------ 1 2 14
15 Hrs
B COMPUTING IN
C++ 2 3 2 5 39
45Hrs
Chapter 5 Review of C++
3 Hrs covered in First ------- ------- ------- ------ ----
PUC
Chapter 6 OOP concepts
---- 1 ---- 1 07
4 Hrs
Chapter 7 Classes and objects
1 ------ ----- 1 06
6 Hrs
Chapter 8 Function
------ ------ ------ 1 05
3 Hrs Overloading
Chapter 9 Constructors and
---- 1 ---- 1 07
8 Hrs Destructors
Chapter 10 Inheritance
------ ----- ------ 1 05
8 Hrs
Chapter 11 Pointers
1 ----- 1 ------ 04
7 Hrs
Chapter 12 Data File handling
------- 1 1 ------ 05
6 Hrs
LARGE DATA,
C
DATABASE & 1 2 1 2 18
20Hrs
QUERIES
Chapter 13 Database concepts
1 1 1 1 11
8 Hrs
Chapter 14 SQL commands
------- 1 ----- 1 07
12 Hrs
ADVANCED
D CONCEPTS IN
4 1 2 1 17
20Hrs COMMUNICATIO
N TECHNOLOGY
Chapter 15 Networking
2 1 ---- 1 9
10 Hrs Concepts
Chapter 16 Internet and Open
1 ---- 1 ----- 4
5 Hrs source concepts
Chapter 17 Web Designing
5 Hrs 1 ----- 1 ------ 4
Total Marks
10 16 24 55 105
Total No of
Questions to be 1X 10=10 2X 5/8=10 3X5/8=15 5X7/11=35 70/37
answered
XVII
List of programs to be conducted in practical sessions
Section A C++ and Data structure
1. Write a program to find the frequency of presence an element in an array.
2. Write a program to insert an element into an array at a given position.
3. Write a program to delete an element from an array from a given position
4. Write a program to sort the elements of an array in ascending order using
insertion sort.
5. Write a program to search for a given element in an array using Binary
search method.
6. Write a program to create a class with data members principle, time and
rate. Create member functions to accept data values to compute simple
interest and to display the result.
7. Write a program to create a class with data members a, b, c and member
functions to input data, compute the discriminant based on the following
conditions and print the roots.
If determinant=0, print the roots that are equal
If the discriminant is>0, print the real roots
If the discriminant<0, print that the roots are imaginary
8. Program to find the area of a square/rectangle/triangle using function
overloading.
9. Program to find the cube of a number using inline functions.
10. Write a program to find the sum of the series 1+ x + x2 + … + xn using
constructors.
11. Create a base class containing the data members roll number and name.
Also create a member function to read and display the data using the
concept of single level inheritance. Create a derived class that contains
marks of two subjects and total marks as the data members.
12. Create a class containing the following data members register No., name
and fees. Also create a member function to read and display the data
using the concept of pointers to objects.
13. Write a program to perform push items into the stack.
14. Write a program to pop elements from the stack.
15. Write a program to perform enqueue and dequeue.
16. Write a program t o create a linked list and appending nodes.
Section B SQL
17. Generate the Electricity Bill for one consumer
18. Create a student database and compute the result.
19 Generate the Employee details and compute the salary based on the
department.
20. Create database for the bank transaction.
Section C HTML
21. Write a HTML program to create a study time-table.
22. Create an HTML program with table and Form.
XVIII
Typical configuration of computer system 1
Chapter 1
Typical Configuration of Computer system
Objectives
1.1 Introduction:
The computer has evolved as a result of man’s search for fast, accurate
calculating devices. Computers have thus become an integral part of everyone’s
life and useful in many ways by increasing man’s efficiency and enhancing his
abilities.
Computers are used to perform various tasks in different fields like science,
engineering, business, education, training, entertainment. The computer works
at high speed, can handle large volumes of data with greater accuracy and have
the ability to carry out a specified sequence of operations without human
intervention.
The term computer basically includes a series of electrical and electronic
circuits together to form a single unit to perform the required operations for the
user. The computer has no intelligence of its own and thus cannot perform any
task by itself. Thus it requires the hardware, software, data and users which
form the computer system to perform the different operations.
The related terms and definitions in the study of computer systems are:
1. Input Unit
The user interacts with the computer via the input unit. The Input unit
accepts data from the user and converts it into a form that is understandable by
the computer. The input data can be characters, word, text, sound, images,
document, etc. The input is provided to the computer using input devices like
keyboard, mouse, joystick, trackball, microphone, scanner etc.
2. Central Processing Unit (CPU)
CPU controls, coordinates and supervises the operations of the computer. It is
also responsible for processing of the input data. CPU consists of Arithmetic
Logic Unit (ALU) and Control Unit (CU).CPU also has a set of registers for
temporary storage of data, instructions addresses and intermediate results of
calculation.
a. ALU performs all the arithmetic and logic operations on the input data.
b. CU controls the overall operations of the computer i.e. it checks the
sequence of execution of instructions, controls and co-ordinates the overall
functioning of the units of computer.
c. Registers are high speed storage units within the CPU, but have least
storage capacity. Registers are not referenced by their address, but are directly
accessed and manipulated by the CPU during instruction execution. They are
referred to as the CPU’s working memory as they are used to store data,
instructions, addresses and intermediate results of processing.
4 Typical configuration of computer system
3. Storage Unit
There are two types of memory associated with storage unit. They are:
primary memory and secondary memory.
The primary memory also called as the main memory of the computer,
consists of RAM (Random Access Memory) and ROM (Read Only Memory)
memories. Main memory stores the data, instructions, intermediate results and
output, temporarily, during the processing of data. The input data that is to be
processed is brought into the main memory before processing. The instructions
required for processing of data, any intermediate results are also stored in the
main memory. The output is stored in main memory before being transferred to
the output device. CPU can work with the information stored in the main memory.
The secondary memory also called as the external memory of the computer
stores permanently the data, programs and the output. Magnetic disks, magnetic
tapes, optical disks and flash drives are examples of secondary memory.
4. Output Unit
The output unit provides the processed data i.e. the result generated after
processing of data. The output may be in the form of text, sound, image, document,
etc. The computer may display the output on a monitor, sends output to the
printer or plotter for printing, and also sends sound output to the speaker, etc.
1.2 Motherboard
The computer is built up around a motherboard. The motherboard is the
most important part of any computer. It is a large Printed Circuit Board (PCB)
having many chips, ports, controllers and other electronic components mounted
on it.
1.2.1 Introduction to Motherboard
The motherboard or the system board is the main circuit board inside a
computer. Every component inside the computer has to communicate through
the motherboard, either by directly plugging into it or by communicating through
one of the motherboard ports. The motherboard provides a platform for all the
components and peripherals to communicate with each other.
The electronic components mounted on the motherboard are processor,
memory chips, interfaces, various ports and all expansion cards. The motherboard
is the hub, which is used to connect all the necessary components of a computer.
The RAM, hard drive, disk drives and optical drives are all plugged into interfaces
on the motherboard.
The motherboard may be characterized by the form factor, chipset and
type of processor socket used.
Typical configuration of computer system 5
ATX Motherboard:
ATX stands for Advanced Technology eXtended. Latest motherboards all are
called as ATX motherboards, designed by ATX form factor. In this motherboard,
we find MPGA processor sockets, DDRRAM slots, PCI slots, AGP slots, Primary
and secondary IDE interfaces, SATA connectors, 20 pin and 24 pin ATX power
connector and ports.
Example: Pentium-IV, Dual Core, Core 2 Duo, Quad Core, i3, i5 and i7 processors.
1.2.3 Components of Motherboard
The motherboard components are:
Processor (CPU)
BIOS
CMOS
Slots
Disk Controllers
I/O Ports and Interfaces
BUS
of data and instructions. CU controls and coordinates the activity of the other
units of computer.
During processing the CPU gets data and instructions from the memory
and interprets the program instructions and performs the arithmetic and logic
operations required for the processing of data. It then sends the processed data
to the memory.
The clock speed of a CPU is defined as the frequency with which a
processor executes instructions or the data that is processed. Higher clock
frequencies mean more clock ticks per second. The computer’s operating speed
is linked to the speed of the system clock. The clock frequency is measured in
millions of cycles per second or megahertz (MHz) or gigahertz (GHz) which is
billions of cycles per second. A CPU’s performance is measured by the number
of instructions executed per second, i.e. MIPS or BIPS. PCs presently come with
a clock speed of more than1GHz.
In Windows OS, the System Properties dialog box is selected to see
the processor name and clock frequency.
The diagram of the CPU is given in Figure 1.3 CPU with Buses and Controllers
The CPU is fabricated as a single Integrated Circuit (IC) chip and is also known
as the microprocessor. This tiny chip contains the entire computation engine.
The microprocessor is plugged into the motherboard of the computer. Intel is
one of the leading processor manufacturers in the world today.
General Structure of motherboard
The primary function of the processor is to execute the instructions given
to it and to produce the results. It fetches instructions and data from the primary
memory and performs the required operations. This movement of data between
8 Typical configuration of computer system
CPU
Graphics Clock
card slot Generator
Internal
Bus PCI
Onboard
Bus
Southbridge graphics
(I/O controller controller
PCI Bus
hub) IDE
SATA
USB Cables and
Ethernet ports leading
Audio Codec off-board
CMOS Memory
PCI Slots
operating system into memory. Most new PCs come with Flash BIOS-these BIOS
can be software upgraded to support new devices.
CMOS (Complementary Metal Oxide Semiconductor)
CMOS is a type of memory chip
to store the date, time and system
setup parameters. These
parameters are loaded every time
the computer is started. That is
why we observe, when the
computer is turned ON, the system
still displays the correct clock time.
BIOS as well as CMOS are kept
powered by a small lithium Ion
battery located on the motherboard
It can be seen in the figure 1.6
below.
Figure 1.6 CMOS battery
Slots
A slot is an opening in a computer where you can insert a printed circuit
board. Slots are often called expansion slots because they allow you to
expand the capabilities of a computer.
Expansion Slots These slots are located on the motherboard. The
expansion cards are inserted in the expansion slots. These cards
give the computer new features or increased performance. There
are several types of slots:
ISA (Industry Standard Architecture) slot – ISA slot is used to
connect modem and input devices.
PCI (Peripheral Component Inter Connect) slot – PCI slots are
used to connect graphics accelerator cards, sound cards, internal
modems or SCSI cards. They are much faster than ISA cards.
AGP (Accelerated Graphic Port) slot – AGP slot is meant to provide
faster access to a graphics accelerator card, thus enhancing the
visual experience for the user. All Celeron and Pentium-III
motherboards come with an AGP slot.
RAM slot – RAM slot is used to install memory and is of two types.
They are SIMM (Single Inline Memory Module) slot and DIMM (Dual
Inline Memory Module) slot. The original Pentium systems typically
have either four 72-pin SIMM slots, or two 168-pin DIMM slot to
install memory.
Typical configuration of computer system 11
Serial port
Serial port is also known as
communication (COM) ports or RS-
232-c ports. They are used for
connecting communication devices
like mouse and modem. This port
transfers data serially one bit at a
time. It needs a single wire to transmit
1 bit of data. Hence it takes 8 times
longer to transfer a byte. There are
two varieties of Com ports, the 9-pin
ports and 25-pin ports.
Parallel port:
Parallel ports are used to
connect external input/output Figure 1.7 Types of ports in the motherboard
devices like printers or scanners. This
port facilitates the parallel transfer of data, usually one byte (8-bits) at a time.
Parallel ports come in the form of 25-pin connector.
IDE (Integrated Digital Electronics) port
IDE devices like CD-ROM drives or hard disk drives are connected to the
motherboard through the IDE port.
USB (Universal Serial Bus) port
USB port gives a single, standardized, easy-to-use
way to connect a variety of newer peripherals to a
computer. These devices include printers, scanners,
digital cameras, web cameras, speakers etc. USB
supports a data speed of 12 megabits per second,
supporting up to 127 devices. USB is a plug- and-play
interface between a computer and add-on devices. i.e. a
new device can be added to the computer without adding
an adapter card or even turning the computer off. The
figure 1.8 shows the symbol used to represent a USB Figure 1.8 USB port
port.
PS-2 port (Personal System-2 port)
The PS-2 port was developed by IBM to interface keyboards and pointing
devices like mouse, trackballs and touch pads. This port is also called as mouse
port as most computers now have a PS-2 port to connect a mouse. This port uses
synchronous serial signals to communicate between the keyboard and a mouse
to the computer.
Typical configuration of computer system 13
DVI (Digital Video Interface) port connects a Flat panel LCD monitor to the
computer’s high-end video graphic cards. It is very popular among video card
manufacturers.
the associated timing ( instead of recording digitized sound waves). The port
and interface are required for connectivity.
Control bus
System bus
Address bus
Data bus
Registers
The registers are high speed temporary storage areas located inside the
CPU. After the CPU gets the data and instructions from the cache or RAM, the
data and instructions are moved to registers for processing. These registers work
under the direction of the control unit (CU) to accept, store and transfer
instructions or data, and perform arithmetic or logical comparisons at high speed.
Since CPU uses registers for the processing of data, the number of registers in a
CPU and the size of each register affect the power and speed of a CPU.
Cache memory
The cache memory is a very high speed memory placed in between RAM
and CPU. Cache memory stores data that is used more often, temporarily and
makes it available to CPU at a fast rate. Hence it is used to increase the speed of
processing. During processing, the CPU first checks cache for the required data.
If data is not found in cache, then it looks in the RAM for data.
The Cache memory is a high speed memory available inside CPU
to speed up access of data and instructions stored in RAM memory.
Processor
Offline UPS
Online UPS
Activity
Typical configuration of computer system 21
Points to remember
The motherboard is the main circuit board inside a computer which
provides a platform for all the components and peripherals to communicate
with each other.
The motherboard may be characterized by the form factor, chipset and
type of processor socket used.
The motherboard types are XT, AT, Baby AT and ATX motherboards.
The motherboard components are Processor (CPU), BIOS, CMOS, Slots,
Disk Controllers, I-O Ports/Interfaces and BUS
Review questions
One marks questions:
1. What is a motherboard?
2. What is microprocessor?
3. What is the purpose of registers in the CPU?
4. How does the computer communicate with other devices?
5. What is system bus?
6. What is the function of control bus?
7. What is a data bus?
8. What is a port?
9. What is an interface?
10. Expand PCI.
11. How many bits of data are sent in a serial port?
12. Expand USB.
13. Give one feature of USB port.
14. What is meant by plug and play device?
15. Name any one USB device.
16. Is device controller a hardware or software?
17. What is cache memory?
18. Where is L1 cache located?
19. Where is L2 cache located?
20. Expand SDRAM.
21. Give the expansion of DDRRAM.
22. Expand SMPS.
23. What is the use of SMPS?
24. What is the approximate power consumed by a PC?
25. Expand UPS.
26. What is the use of UPS?
27. List the types of UPS.
Two marks questions:
28. Name any two types of motherboard.
29. Mention any two characteristics of motherboard.
30. Mention the components of motherboard.
31. Explain system bus.
32. What is data bus and address bus?
33. What is the purpose of expansion slot?
34. What is the purpose of AGP slot?
35. Name the different types of I/O ports.
36. Explain serial port.
Typical configuration of computer system 23
******
24 Boolean Algebra
Chatpter 2
Boolean Algebra
Objectives:
values. The truth values are depicted by logical constants TRUE and FALSE or 1
and 0 respectively. 1 means TRUE and 0 means FALSE. The variables which can
store these truth values are called logical variables or binary valued variables as
these can store one of the two values 1 or 0 (TRUE or FALSE).
The decision which results into either YES (TRUE or 1) or NO (FALSE or 0)
is called a Binary Decision.
Also, if an equation describing logical circuitry has several variables, it is
still understood that each of the variables can assume only the values 0 and 1. For
instance, in the equation A + B = C , each of the variables A , B and C may have
only the values 0 or 1.
NOT Operator
This operator operates on single variable and operation performed by NOT
operator is called complementation and the symbol we use for it is (bar). Thus
X means complement of X and YZ means complement of YZ. As we know, the
variables used in Boolean equations have a unique characteristic that they may
assume only one of two possible values 0 and 1, where 0 denotes FALSE and 1
denotes TRUE value. Thus the complement operation can be defined quite simply.
0=1 or NOT (FALSE) = TRUE and
1=0 or NOT (TRUE) = FALSE and
The truth table for the NOT operator is
X X Several other symbols like ‘~’ are also used for the
complementation symbol. If ~ is used then ~X is read as ‘negation
0 1
of X’ and if symbol ’ is used then X’ is read as complement of X.
1 0
OR operator
A second important operator in Boolean algebra is OR operator which
denotes operation called logical addition and the symbol we use for it is +. The
+ symbol, therefore, does not mean arithmetic addition, but is a logical addition
or logical OR symbol. Thus, X + Y can be read as X OR Y. For OR operation,
the possible input and output combinations are as follows :
0+0=1
0+1=1
1+0=1
1+1=1
The truth table of OR operator is given below:
X Y X+Y Note that when any one or both X and Y is 1, X + Y is 1.
0 0 0
0 1 1 and both X and Y is 0, X+Y is 0
1 0 1
1 1 1
Table 1.4 : Truth Table for OR operator
To avoid ambiguity, there are other symbols e.g., and have been
recommended as replacements for the + sign. Computer people still use the + sign,
however, which was the symbol originally proposed by Boole.
Venn diagram for X + Y is given below where the shaded area depicts X + Y.
AND Operator
AND operator performs another important operation of Boolean Algebra
called logical multiplication and the symbol for AND operation is ‘.’ (dot). Thus
X.Y will be read as X AND Y. The rules for AND operation are :
0.0 = 0
0.1 = 0
1.0 = 0
1.1 = 1
Boolean Algebra 29
Table 1.10 : Truth Table for AND operator FIGURE 1.3 : Venn Diagram for (X.Y)
Note that only when both X and Y are 1’s, X.Y has the result 1. If any one
of X and Y is 0, XY result 0. Venn diagram for X.Y is given in the figure above where
the shaded area depicts X.Y
2.4.0 Evaluation of Boolean Expressions Using Truth Table
Logical variables are combined by means of logical operators AND, OR
and NOT to form a Boolean expression. For example, X+Y.Z+Z is a Boolean
expression.
It is often convenient to shorten X.Y.Z to XYZ and using this convention,
above expression can be written as X+YZ+Z
To study a Boolean expression, it is very useful to construct a table of values
for the variables and then to evaluate the expression for each of the possible
combinations of variables in turn. Consider the expression X+YZ. Here three
variables X, Y, Z are forming the expression. Each variable can assume the value
0 or 1. The possible combinations of values may be arranged in ascending order
as in Table 1.11
One more column is now added to list the values of YZ (Table 1.13)
X Y Z Y.Z YZ
0 0 0 0 1
0 0 1 0 1
0 1 0 0 1 Note that YZ contains complemented values of YZ.
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 0 1
1 1 1 1 0
Now values of X are ORed (logical addition) to the values of YZ and the
resultant values are contained in the last column (Table 1.14).
X Y Z Y.Z YZ X+YZ
0 0 0 0 1 1 Now observe the expression X+YZ, after
0 0 1 0 1 1 ANDing Y and Z, the result has been
0 1 0 0 1 1 complemented and then ORed with X. Here
0 1 1 1 0 0 the result is 0 only when both the columns
1 0 0 0 1 1 X and YZ have 0, otherwise if there is 1 in
1 0 1 0 1 1 any of the two columns X and YZ , the result
1 1 0 0 1 1 is 1.
1 1 1 1 0 1
Example 1.13: In the Boolean algebra, verify using truth table that X+XY = X for
each X, Y in 0 and 1.
As the expression X+XY=X is a two variable expression, so we require
four possible combinations of values of X, Y. Truth Table will be as follows:
X Y XY X+XY
0 0 0 0
0 1 0 0
1 0 0 1
1 1 1 1
Comparing the columns X+XY and X, we find, contents of both the columns
are identical, hence verified.
Example 1.14: In the Boolean Algebra, verify using truth table that
X+Y = X . Y in 0 and 1.
Solution: As it is a 2-variable expression, truth table will be as follows:
X Y X+Y X+Y X Y X.Y
0 0 0 1 1 1 1
0 1 1 0 1 0 0
1 0 1 0 0 1 0
1 1 1 0 0 0 0
Comparing the columns X+Y and X . Y both the columns are identical, hence
verified.
Example 1.15: Prepare a table of combinations for the following Boolean algebra
expressions:
(a) X Y + X Y (b) XY Z + X Y Z (c) X Y Z + X Y
32 Boolean Algebra
X Y X Y XY XY XY+XY
0 0 1 1 1 0 1
0 1 1 0 0 1 1
1 0 0 1 0 0 0
1 1 0 0 0 0 0
X Y Z X Y Z XYZ X YZ XYZ+XYZ
0 0 0 1 1 1 0 0 0
0 0 1 1 1 0 0 1 1
0 1 0 1 0 1 0 0 0
0 1 1 1 0 0 0 0 0
1 0 0 0 1 1 0 0 0
1 0 1 0 1 0 0 0 0
1 1 0 0 0 1 1 0 1
1 1 1 0 0 0 0 0 0
X Y Z X Y Z XYZ XY XYZ+XY
0 0 0 1 1 1 0 0 0
0 0 1 1 1 0 0 0 0
0 1 0 1 0 1 1 0 1
0 1 1 1 0 0 0 0 0
1 0 0 0 1 1 0 1 1
1 0 1 0 1 0 0 1 1
1 1 0 0 0 1 0 0 0
1 1 1 0 0 0 0 0 0
Example 1.16 Prepare truth table for the following Boolean algebra
expressions:
(a) X(Y+Z) +X Y (b) XY (Z+YZ)+Z (c) A[(B+C)+C]
Boolean Algebra 33
Table 1.15 Truth Table for NOT gate Table 1.16 Alternative truth table for NOT gate
A low input or 0 produces high output or 1 and vice versa. The symbol for inverter
is given in adjacent Fig. 1.4.
X X
Fig. 1.4. Not gate symbol
Boolean Algebra 35
NOT Gate is a gate or an electronic circuit that accepts only one input and
produces one output signal. The output state is always the complement of
the input state.
OR Gate
The OR Gate has two or more input signals, but only one output signal. This
gate gives the logical addition of the inputs. If any of the input signals or both is 1
(high), the output signal is 1 (high). The output will be low if all the inputs are low.
An OR gate can have as many inputs as desired. No matter how many
inputs are there, the action of OR gate is the same.
The OR gate has two or more input signals, but only one output signal. The
out will be the logical addition of the inputs.
Following tables show OR action
X Y F X Y Z F
0 0 0 0 0 0 0
0 1 1 0 0 1 1
1 0 1 0 1 0 1
1 1 1 0 1 1 1
1 0 0 1
Table : F=X+Y
1 0 1 1
1 1 0 1
1 1 1 1
Table : F=X+Y+Z
The symbol for OR gate is given below:
A A F A F
F B B
B C C
D
a) 2 input OR gate b) 3 input OR gate c) 4 input OR
gate Figure 1.5
AND gate
The AND Gate can have two or more than two input signals and produce
an output signal. When all the inputs are 1 or high only then the output is 1,
otherwise output is 0 only.
If any one or all the inputs is 0, the output is 0. To obtain output as 1, all
inputs must be 1.
An AND gate can have as many inputs as desired.
The AND Gate has two or more input signals, but only one output signal.
The out will be the logical multiplication of the inputs.
36 Boolean Algebra
0+1=1 0 1
OR
1
1 1
1+0=1 OR
0
1 1
1+1=1 OR
1
Boolean Algebra 37
0.0 = 0
0.1 = 0
1.0 = 0
1.1 = 1
IV Complement Rules
0=1 0 1
1=0 1 0
PRINCIPLE OF DUALITY
This is a very important principle used in Boolean algebra. This states
that starting with a Boolean relation another Boolean relation can be derived by
i. Changing each OR sign (+) to an AND sign (.)
ii. Changing each AND sign (.) to an OR sign (+)
iii. Changing each 0 by 1 and each 1 by 0.
The derived relation using duality principle is called dual of original
expression.
For instance, we take postulates of OR relation, which states that
(a) 0+0=0 (b) 0 + 1 = 1 (c) 1 + 0 = 1 (d) 1 + 1 = 1
Now working according to above guidelines, '+' is changed to '.' 0's are replaced by
1’s and 1’s are replaced by 0’s, these equations become
(i) 1.1=1 (ii) 1.0=0 (iii) 0.1=0 (iv) 0.0=0
These are nothing but postulate III related to AND relations. We’ll be applying
this duality principle in the theorems of Boolean algebra.
38 Boolean Algebra
Proof a) 0+x = x
If x = 0, then LHS = 0 + x
=0+0
=0 { By OR relation }
=x
= RHS
If x = 1, then LHS = 0 + x
=0+1
=1 { By OR relation }
=x
= RHS
Boolean Algebra 39
(a) 1+x=1
Proof: If x = 0, LHS = 1 + x
=1+0
=1 { By OR relation }
If x = 1, LHS =1+x (
=1+1
=1 { By OR relation }
Thus, for every value of x, 1 + x = 1 always.
Truth table for above expression is given below in Table 1.21, where R signifies
the output or result.
1 x 1+x
1 0 1
1 1 1
If x = 1, LHS = 0.x
= 0.1
=0 { By AND relation }
= RHS
Thus, for every value of x, 0.x = 0 always.
(d) 1.X = X
Proof: If x = 0, LHS = 1.x
= 1.0
=0 { By AND relation }
=x
=RHS
If x = 1, LHS = 1.x
= 1.1
=1 { By AND relation }
=y
=RHS
Thus, for every value of x, 1.x = x always.
1 X 1.X
1 0 0
1 1 1
X
a) X + X = X OR X (gate representation for (a))
X
X
AND X
b) X . X = X X (gate representation for (b))
Proof :
(a) X+X=X
If x = 0, consider LHS = x + x
=0+0
=0 { By OR relation }
=x
=RHS
If x = 1, consider LHS = x + x
=1+1
=1 { By OR relation }
=x
=RHS
Thus, for every value of x, x + x = x always.
To prove this law, we will make truth table for above expression. As X is to
be ORed with itself only, we will prepare truth table with the two possible values
of X (0 and 1).
42 Boolean Algebra
X X X+X
0 0 0
1 1 1
Table 1.25 Truth Table for x + x = x
(b) X.X = X
Here X if ANDed with itself.
Proof: If x = 0, consider LHS = x.x
= 0.0
=0 { By AND relation }
=x
=RHS
If x = 1, LHS = x.x
= 1.1
=1 { By AND relation }
=x
=RHS
Thus, for every value of x, x + x = x always.
x x x.x
0 0 0
1 1 1
Table 1.26 Truth Table for X.X = X
2.5.3 Involution
This law states that the complement of a variable is complemented again,
we get the same variable.
X
(X) = X ie., X X=X
X X X
0 1 0
1 0 1
Proof: If x = 0, LHS = x + x
=0+1 (x = 1)
=1 { By OR relation }
= RHS
If x = 1, LHS = x + x
=1+0
=1 { By OR relation }
= RHS
Thus, for every value of x, x + x = 1 always.
We will prove x + x=1 with the help of truth table which is given below :
44 Boolean Algebra
X X X+X
0 1 1
1 0 1
Here, in the first column possible values of X have been taken, second column
consists of X values (complement values of X), X and X values are ORed and the
output is shown in third column. As the equation holds true for both possible
values of X, it is a tautology.
(b) X.X = 0
Proof: If x = 0, LHS = x . x
= 0 . 1 ( x = 1)
=0 { By AND relation }
If x = 1, LHS = x . x
= 1 . 0 ( x = 0)
=0 { By AND relation }
Thus, for every value of x, x . x = 0 always.
Truth table for the expression is as follows:
X X X.X
0 1 0
1 0 0
Table 1.29 Truth table for X . X = 0
The equations X.X = 0 as it holds true for both the values of X. Hence
proved. Observe that X.X=0. It is a fallacy. It is the dual of X+X=1.
If x = 0 then LHS = x + y
=0+y
=y
RHS = y + x
=y+0
=y
Therefore, for x = 0, x+y=y+x
If x = 1 then LHS = x + y
=1+y
=1
RHS = y + x
=y+1
=1
Therefore, for x = 1, x + y = y + x. Hence the proof.
X Y X+Y Y+X
0 0 0 0
0 1 1 1
1 0 1 1
1 1 1 1
Compare the columns X + Y and Y +X, both of these are identical. Hence also
proved by truth table.
(b) Truth Table for X . Y = Y . X is given below:
Proof: If x = 0 then LHS = x . y
=0.y
=0
RHS = y . x
=y.0
=0
Therefore, for x = 0, x+y=y+x
If x = 1 then LHS = x . y
46 Boolean Algebra
=1.y
=y
Therefore, for x = 1, x + y = y + x. Hence the proof.
X Y X.Y Y.X
0 0 0 0
0 1 0 0
1 0 0 0
1 1 1 1
a) X+(Y+Z) = (X+Y)+Z
Proof: If X = 0 the LHS = X + (Y + Z)
= 0 + (Y+ Z)
= Y+Z
RHS = (X+Y)+Z
= (0+Y)+Z
= Y+Z
Therefore for X=0, X+(Y+Z) = (X+Y)+Z
If X=1, then LHS =X+(Y+Z) RHS = (X+Y)+Z
=1+(Y+Z) =1+(Y+Z)
=1 =1+Z
Therefore X=1, X+(Y+Z) = (X+Y)+Z =1
Boolean Algebra 47
Compare the columns X+(Y+Z) and (X+Y)+Z, both of these are identical.
Hence proved. Note : Give proof with table for rule (b). Since rule (b) is a dual of
rule (a), hence it is also proved.
Both the columns X(Y+Z) and XY+XZ are identical, hence proved.
Note : Since rule (b) is dual of rule (a), hence it is also proved
(b) X + YZ = (X+Y) (X+Z)
Proof: RHS = (X+Y) (X+Z)
= XX + XZ +XY + YZ
= X+XZ+XY+YZ (XX=X)
= X(1 + Z + Y) + YZ
= X + YZ (1 + z + y = 1)
= LHS Hence the proof
Truth table for X+YZ = (X+Y)(X+Z) is given below
2.5.8 Absorption Law
According to this law
a) X+XY=X b) X(X+Y)=X
X X
X
XY OR X+Y AND
AND OR
Y Y
Logic diagram (a) Logic diagram (b)
Proof: a) X+XY = X
LHS = x + xy
= x(1 + y)
= x.1 (1+Y=1)
= x (X.1=X)
= RHS
Boolean Algebra 49
X Y X+Y X(X+Y)
0 0 0 0
0 1 1 0
1 0 1 1
1 1 1 1
X
X
R = X R=X+Y
XY Y
Y
50 Boolean Algebra
Proof : LHS = X + XY
= (x+x)(x+y) { x+x= 1}
= 1.(x+y)
= x+y
= RHS
All the theorems of Boolean algebra, which we have been covered so far, are
summarized in the following table:
Table 1.35 Boolean algebra rules
1 0+X=X Properties of 0
2 0.X = 0
3 1+X=1 Properties of 1
4 1.X = X
5 X+X=X Indempotence law
6 X.X=X
7 X =X Involution
8 X+X=1 Complementarity law
9 X. X = 0
10 X+Y=Y+X Commutative law
11 X . Y = Y. X
12 X + (Y + Z) = (X+Y)+Z Associative law
13 X(YZ) = (XY) Z
14 X (Y+Z) = XY+XZ Distributive law
15 X+YZ=(X+Y) (X+Z)
16 X+XY=X Absorption law
17 X . (X+Y) = X
18 X+XY=X+Y
X X
X X.Y R
= R
Y Y Y
Proof: Again to prove this theorem, we will make use of complementarity law i.e.,
X + X = 1 and X.X = 0
If XY’s complement is X + Y then it must be true that
(a) X Y + (X + Y) =1 and (b) X Y (X + Y) = 0
52 Boolean Algebra
L.H.S. = XY + (X + Y)
= (X + Y) + XY
= (X + Y + X) . (X + Y + Y)
= (X + X + Y) . (X + Y + Y)
= (1 + Y) . (X + 1) (ref. X + X = 1)
= 1.1 (ref. 1 + X = 1)
= 1 = R.H.S.
Now, the second part. i.e., XY . (X + Y) = 0
L.H.S. = XY. (X + Y) (ref. X(Y+Z)=XY+XZ)
= XYX + XYY
= XXY + XYY
= 0.Y + X.0 (ref. X . X = 0)
= 0+0
=0
= RHS
XY.(X + Y) = 0 and XY(X + Y) = 1
Thus, X.Y = X+Y Hence the theorem.
2.7.2 Maxterms
A maxterm is a sum of all the literals (with or without the bar) within the
logic system.
Trying to be logical about logic, if there is something called minterm, there
surely must be one called maxterm and there is.
If the value of a variable is 1, then its complement is added otherwise the
variable is added as it is.
Example: If the values of variables are X=0, Y=1 and Z=1 then its Maxterm will
be X + Y + Z(Y and Z are 1’s, so their complements are taken; X= 0, so it is taken
as it is).
Similarly if the given values are X=1, Y=0, Z =0 and W=1 then its Maxterm is
X + Y + Z + W.
Maxterms can also be written as M (Capital M) with a subscript which is
decimal equivalent of given input combination e.g., above mentioned Maxterm
X+Y+Z+W whose input combination is 1001 can be written as M9 as decimal
equivalent of 1001 is 9.
2.7.3 Canonical Expression
Canonical expression can be represented in following two forms:
(i) Sum-of-Products (SOP)
(ii) Product-of-sums (POS)
Sum-of-Products (SOP)
A logical expression is derived from two sets of known values:
Various possible input values
The desired output values for each of the input combinations.
Let us consider a specific problem.
A logical network has two inputs X and Y and an output Z. The relationship
between inputs and outputs is to be as follows:
(i) When X=0 and Y=0 then Z=1
(ii) When X =0 and Y=1 then Z=0
(iii) When X =1 and Y=0, then Z=1
58 Boolean Algebra
Here, we have added one more column to the table consisting list of product
terms or minterms. Adding all the terms for which the output is 1. i.e., Z=1 we get
following expression:
XY + XY + XY = Z
Now see, it is an expression containing only minterms. This type of expression
is called minterm canonical form of Boolean expression or canonical sum-of-
products form of expression.
X Y Z F Product Terms/
Minterms
0 0 0 0 XYZ
0 0 1 1 XYZ
0 1 0 1 XYZ
0 1 1 0 XYZ
1 0 0 1 XYZ
1 0 1 0 XYZ
1 1 0 0 XYZ
1 1 1 1 XYZ
Solution: Add a new column containing Maxterms. Now the table is as follow:
X Y Z F Maxterms
0 0 0 1 X + Y + Z
0 0 1 0 X + Y + Z
0 1 0 1 X + Y + Z
0 1 1 0 X + Y + Z
1 0 0 1 X + Y + Z
1 0 1 0 X + Y + Z
1 1 0 1 X + Y + Z
1 1 1 1 X + Y + Z
62 Boolean Algebra
Now by multiplying maxterms for the output 0’s, we get the desired product
of sums expression which is (X + Y + Z) (X + Y + Z) (X + Y + Z)
Short hand = M4, M5, M0, M4, M1, M5, M5, M7, M1, M5 = M(0, 4, 5, 7)
Rule 5: Removing all the duplicate terms, we get
(X + Y + Z) (X + Y + Z) (X + Y + Z) (X + Y + Z) (X + Y + Z)
This is the desired canonical product of sums form of expression.
Shorthand maxterm notation
Shorthand notation of the above given canonical product of sums expression is
F= (0,1,4,5,7) or F = M(0,1,4,5,7)
This specifies that output F is product 0th, 1st, 4th and 7th Maxterms
i.e., F = M0.M1.M5.M5.M7
Here, M0 means Maxterm for Binary equivalent of 0 i.e., 000 ie., X=0, Y=0, Z=0
And, Maxterm will be (X+Y+Z) (Complemented variable is 1 and uncomplemented
variable is 0)
Similarly, M1 Means 0 0 1 X+Y+ Z
AS F = M0 .M1 .M4. M5 .M7
and M0 = 000 X+Y+Z
M1 = 001 X+Y+Z
M4 =100 X+Y+Z
M5 = 101 X+Y+Z
M7 = 111 X+Y+Z
F = (X + Y + Z) (X + Y + Z) (X + Y + Z) (X + Y + Z) (X + Y + Z)
Example 1.28: Convert the following function into canonical product of sums
form: F(X, Y, Z) = M(0, 2, 4, 5)
Note: To convert an expression from shorthand SOP form to shorthand POS
form, just create truth table from given expression. From the created truth table,
derive other form of expression. For example, from truth table, you can convert
an expression F(X, Y,Z)= (0,1,3,5) to M(2,4,6,7)
Solution: F (X, Y, Z) = M(0, 2,4,5) = M0 . M2 .M4 .M5
M0 = 000 X+Y+Z
M2 = 010 X+Y+Z
M4 = 100 X+Y+Z
M5 = 101 X+Y+Z
F = (X + Y + Z) (X + Y + Z) (X + Y + Z) (X + Y + Z)
64 Boolean Algebra
= X + X + XY + Y
= X + XY + Y {X + X = X as X + X = X}
= (X + X) (X + Y) + Y (Putting X + X = 1
=X+Y+Y {Y + Y = 1}
=X+1 {Putting Y + Y = 1)
=1 {putting X + 1 = 1 as 1 + X =1}
Boolean Algebra 65
= Z(Y+Y)
=Z
Y Y
(0)Y (1)Y (0)Y (1)Y
X X
(0)X XY XY (0)X
0 1 0 1
(1)X XY XY (1)X
2 3 2 3
(a) (a)
Y Y
X (00)YZ (01)YZ (11)YZ (10)YZ X (00)YZ (01)YZ (11)YZ (10)YZ
Y Y
X (00)YZ (01)YZ (11)YZ (10)YZ X (00)YZ (01)YZ (11)YZ (10)YZ
Observe carefully above given K-map. See the binary numbers at the top
of K-map. These do not follow binary progression, instead they differ by only one
place when moving from left to right : 00, 01, 11, 10. It is done so that only one
variable changes from complemented to un complemented form or vice versa.
The terms are A B . A B, AB, A B
This binary code 00, 01, 11, 10 is called Gray code. Gray Code is the
binary code in which each successive number differs only in one place. That is
why box numbering scheme follows above order only.
How to Map in K-Map?
We’ll take an example of 2 variable map to be illustrated, with the following
truth table for mapping (Table 1.38)
Table 1.38
A B F
0 0 0
0 1 0
1 0 1
1 1 1
Canonical S-O-P expression for this table is F=AB + AB or F = (2,3).
To map this function first we’ll draw an empty 2-variable K-map as shown in Fig.
1.8(a)
68 Boolean Algebra
B B B
(0) (1) (0) (1) (0) (1)
A A A
YZ YZ
WX (00)YZ (01)YZ (11)YZ (10)YZ WX (00)YZ (01)YZ (11)YZ (10)YZ
(00)WX 1 0 0 0 (00)WX 1 0 0 0
0 1 3 2 0 1 3 2
(01)WX 1 0 0 0 (01)WX 1 0 1 1
4 5 7 6 4 5 7 6
(11)WX 0 0 1 1 (11)WX 1 0 1 1
12 13 15 14 12 13 15 14
(10)WX 0 0 0 0 (10)WX 1 0 0 0
8 9 11 10 8 9 11 10
(00)WX 0 0 0 0
0 1 3 2
(01)WX 0 0 0 0
4 5 7 6
(11)WX 1 1 1 1
12 13 15 14
(10)WX 1 1 1 1
8 9 11 10
While moving horizontally in the octet two variables Y and Z are removed
and moving vertically one variable x is removed. Thus eliminating X, Y and Z, the
reduced expression for the octet is W only.
Octet Reduction Rule
Remove the three variables which change their states. An octet removes 3-
variables. But after marking pairs, quads and octets, there are certain other things
to be taken care of before arriving at the final expression. These are map rolling,
overlapping groups and redundant groups.
Map Rolling
Map Rolling means roll the map i.e., consider the map as if its left edges are
touching the right edges and top edges are touching the bottom edges. This is a
special property of Karnaugh maps that its opposite edges squares and corner
squares are considered contiguous (Just as the world map is treated contiguous
at its opposite ends). As in opposite edges squares and in corner squares only one
variable changes its state from complemented to uncomplemented state or vice
versa. Therefore, while making the pairs, quads and octets, map must be rolled.
Following pairs, quads and octets are marking after rolling the map.
Boolean Algebra 71
CD CD
AB (00)CD (01)CD (11)CD (10)CD AB (00)CD (01)CD (11)CD (10)CD
1 1 1
(00)AB (00)AB
0 1 3 2 0 1 3 2
(01)AB 1 1 (01)AB 1 1
4 5 7 6 4 5 7 6
(11)AB (11)AB 1 1
12 13 15 14 12 13 15 14
(10)AB (10)AB
1 1
8 9 1 11 10 8 9 11 10
ABD+BCD BD+BD
Overlapping Groups
Overlapping means same 1 can be encircled more than once. For example,
if the following K-map is given:
CD
(00)CD (01)CD (11)CD (10)CD
AB
(00)AB
0 1 3 2
(01)AB 1 1 1
4 5 7 6
(11)AB
1 1
12 13 15 14
(10)AB 1
8 9 11 10
Observe that 1 for m7 has been encircled twice. Once for Pair-1(m5+m7)
and again for Quad (m7+m6+m15+m14). Also 1 for m14 has been encircled twice.
For the Quad and for Pair-2 (m14+m10).
Here, reduced expression for Pair-1 is ABD
Reduced expression for Quad is BC
Reduced expression for Pair-2 is ACD
Thus final reduced expression for this map is ABD + BC + ACD
72 Boolean Algebra
Thus reduced expression for entire K-map is sum of all reduced expressions
in the very K-map.
But before writing the final expression we must take care of redundant
Groups.
Redundant Groups
Reduntant group is a group whose all 1’s are overlapped by other groups
(i.e., pairs, quads, octets). Here is an example, given below.
CD CD
AB (00)CD (01)CD (11)CD (10)CD AB (00)CD (01)CD (11)CD (10)CD
(00)AB (00)AB
0 1 3 2 0 1 3 2
(01)AB 1 1 (01)AB 1 1
4 5 7 6 4 5 7 6
(11)AB 1 1 (11)AB 1 1
12 13 15 14 12 13 15 14
(10)AB (10)AB
8 9 11 10 8 9 11 10
Fig. 1.14(a) has a redundant group. There are three pairs : Pair-1 (m4+m5),
Pair-2 (m5+m13), Pair-3 (m13+m15). But Pair-2 is a redundant group as its all 1’s
are marked by other groups.
With this reduntant group, the reduced expression will be ABC+BD+ABD.
For a simpler expression, Redundant Groups must be removed. After removing
the redundant group, we get the K-map shown in fig. 1.14 (b).
The reduced expression, for K-map in fig. 1.14 (b), will be
ABC + ABD
Which is much simpler expression .
Thus removal of redundant group leads to much simpler expression.
3. Map the given function by entering 1’s for the outputs as 1 in the
corresponding squares.
4. Enter 0’s in all left out empty squares.
5. Encircle adjacent 1’s in form of octets, quads and pairs. Do not forget to
roll the map and overlap.
6. Remove redundant groups, if any.
7. Write the reduced expressions for all the groups and OR (+) them.
Example 1.33 Reduce F (a, b, c, d) = m (0,2,7,8,10,15) using Karnaugh map.
Solution: Given F (a, b, c, d) = m (0,2,7,8,10,15)
= m0 + m2 + m7 + m8 + m10 + m15
m0 = 0000 = A B C D m2 = 0010 = A B C D
m7 = 0111 = A B C D m8 = 1000 = A B C D
m10 = 1010 = A B C D m15 = 1111 = A B C D
Truth table for the given Mapping the given function in a K-map
function is as follows : we get
A B C D F
0 0 0 0 1 CD
(00)CD (01)CD (11)CD (10)CD
0 0 0 1 AB
0 0 1 0 1
(00)AB 1 0 0 1
0 0 1 1
0 1 3 2
0 1 0 0
0 1 0 1 (01)AB 0 0 1 0
0 1 1 0 4 5 7 6
0 1 1 1 1
1 0 0 0 1 (11)AB 0 0 1 0
1 0 0 1 12 13 15 14
1 0 1 0 1
(10)AB
1 0 1 1 1 0 1 1
8 9 11 10
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1 1
In the above K-map two groups have been marked, one Pair and One Quad.
Pair is m7 + m15
74 Boolean Algebra
(11)AB 1 1 1 1
12 13 15 14
(10)AB
0 1 1 1
8 9 11 10
Reduced expression for pair-1 (m7 + m15) is BCD, as ABCD (m7) changes to
ABCD (m15) eliminating A.
Reduced expression for Quad-1 (m12 + m13 + m14) is AB, as while moving
across the Quad, C and D both are removed because both are changing their
states from complemented to uncomplemented or vice-versa.
Reduced expression for Quad 2 (m13 + m15+m9 +m11) is AD, as moving
horizontally, C is removed and moving vertically, B is removed.
Reduced expression of Quad-3 (m15 + m11 +m14 + m10) is AC as horizontal
movement removes D and vertical movement removes B.
Thus, Pair-1 = BCD, Quad-1 = AB, Quad-2 = AD, Quad-3 = AC
Hence final reduced expression will be BCD+AB+AD+AC.
Example 1.35: Obtain a simplified expression for a Boolean function F (X, Y,
A) the Karnaugh map for which is given below:
Boolean Algebra 75
YZ
[00] [01] [11] [10]
X
12 13 15
(10)WX 14
1 0 0 0
76 Boolean Algebra
YZ
X (00) (01) (11) (10)
(0) 1 1
(1) 1 1
(d)
Solution: Completing the given K- map, we get one group which is a Quad has
been marked.
Quad reduces two variables. Y
Moving horizontally, Z is removed as it X (00)YZ (01)YZ (11)YZ (10)YZ
changes from Z to Z and moving
(0)X 0 0 1 1
vertically, X is removed as it changes 0 1 3 2
from X to X. Thus only one variable Y
(1)X 0 0 1 1
is left. Hence Reduced S-O-P 4 5 7 6
expression is Y. Thus F=Y assuming F
is the given function.
Y Y
(0) Y (1) Y (0) Y (1) Y
X X
(0) X (0) X (X + Y) (X + Y)
0 1 0 1
(1) X (1) X (X + Y) (X + Y)
2 3 2 3
(a) (b)
YZ YZ
(00)Y+Z (01)Y+Z (11)Y+Z (10)Y+Z (00)Y+Z (01)Y+Z (11)Y+Z (10) Y+Z
X X
4 5 7 6 4 5 7 6
(c) (d)
YZ
(00) (01) (11) (10) [00] W+X W + X + Y + Z W + X + Y + Z W + X + Y + Z W + X + Y + Z
X
[00] 0 1 3 2 0 1 3 2
[00] 4 5 7 6
W+X+Y+Z W+X+Y+Z W+X+Y+Z W+X+Y+Z
[01] W+X
[00] 12 13 15 14
4 5 7 6
[00]
8 9 11 10
[11] W+X W + X + Y + Z W + X + Y + Z W + X + Y + Z W + X + Y + Z
(e)
12 13 15 14
[10] W+X W + X + Y + Z W + X + Y + Z W + X + Y + Z W + X + Y + Z
8 9 11 10
(f)
4 varible K–Map representing Minterms
Figure 1.115: 2,3,4 variable K-Maps of POS expression.
78 Boolean Algebra
BC
A (00) (01) (11) (10)
(0) 0 0 0 1
(1) 0 1 1 1
Solution: To reach at POS expression, we’ll have to encircle all possible groups
of adjacent 0’s encircling we get the following K-map.
Boolean Algebra 79
[0]A 0 0 0
0 1 3 2
[1]A
0 0 0
4 5 7 6
Pair1: M0 . M1;
Pair 2: M0 .M4;
Pair 3: M1 . M3;
But there is one redundant group also i.e., Pair-1 (it’s all 0’s are encircled
by other groups). Thus removing this redundant pair-1, we have only two groups
now.
Reduced POS expression for Pair-2 is (B+C), as while moving across pair-2,
A changes its state from A to A, thus A is removed.
Reduced POS expression for Pair 3 is (A+C), as while moving across Pair 3
B changes to B, hence eliminated.
Final POS expression will be (B+C).(A+C)
Example1.39: Find the minimum POS expression of
Y(A, B, C, D) = (0, 1, 3, 5, 6, 7 10, 14, 15).
Solution: As the given function is 4 variable function, we’ll draw 4 variable K-
Map and then put 0’s for the given Maxterms. i.e., in the squares whose numbers
are 0, 1, 3, 5, 6, 7, 10, 14, 15 as each square number represents its Maxterm.
So, K-map will be
CD
AB (00)C+D (01)C+D (11)C+D (10)C+D
(00)A+B 0 0 0 1
0 1 3 2
(01)A+B 1 0 0 0
4 5 7 6
(11)A+B 1 1 0 0
12 13 15 14
(10)A+B 1 1 1 0
8 9 11 10
80 Boolean Algebra
Review questions:
One mark questions:
1. What is another name of Boolean algebra?
2. What do you understand by logic function?
3. Give examples for logic function.
4. What is meant by tautology and fallacy?
5. Prove the 1+Y is a tautology and 0.Y is a fallacy.
6. State indempotence law.
7. Prove indempotence law using truth table.
8. Draw logic diagram to represent indempotence law.
9. State Involution law.
10. Prove Involution law using truth table.
11. Draw logic diagram to represent Involution law.
12. State Complementarity law.
13. Prove Complementarity law using truth table.
14. Draw logic diagram to represent Complementarity law.
15. State Commutative law.
16. Prove Commutative law using truth table.
17. Draw logic diagram to represent Commutative law.
18. State Associative law.
19. Prove Associative law using truth table.
20. Draw logic diagram to represent Associative law.
21. State Distributive law.
22. Prove Distributive law using truth table.
23. Draw logic diagram to represent Distributive law.
24. Prove that X+XY = X (Absorption law )
25. Prove that X(X+Y) = X (Absorption law )
26. Draw logic diagram to represent Absorption law.
27. Prove that XY + XY = X
28. Prove that (X+Y)(X+Y) = X
29. Prove that X + X Y = X + Y
82 Boolean Algebra
14. Draw the K – map for the function in three variables given below.
(a) m0 + m2 + m4 + m6 + m7
(b) m1 + m2 + m3 + m5 + m7
15. Write S-O-P expression corresponding to the function F in the following truth
table and draw the logic gate diagram ( use OR and AND gates)
A B C F
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 1
A B C F F’
0 0 0 0 1
0 0 1 0 1
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 0
1 1 1 1 0
Chapter 3
Logic Gates
Objectives:
LOGIC GATES
3.1 Introduction
After Shannon applied Boolean algebra in telephone switching circuits,
engineers realized that Boolean algebra could be applied to computer electronics
as well.
In the computers, these Boolean operations are performed by logic gates.
An Inverter is a gate with only one input signal and one output
signal; the output state is always the opposite of the input state.
X
X
X X
X X
0 1
Low High
1 0
High Low
A low input i.e., 0 produces high output i.e., 1 and vice versa. NOT operation
is symbolized as or i.e., NOT X is written as X 1 or X.
3.1.2 OR Gate
The OR gate has two or more input signals but only one output signal. If
one or more input signals are 1 (high), the output signal is 1 (high).
An OR gate can have as many inputs as desired. No matter how many
inputs are there, the action of OR gate is the same.
The OR gate has two or more input signals but only one output
signal. If any of the input signals is 1 (high), the output signal is 1
(high).
A A A
F B F B F
B C C
D
The AND Gate can have two or more input signals and produce one
output signal. When all the inputs are high then the output is
high. Otherwise, the output is low.
X Y F=X.Y
0 0 0
0 1 0
1 0 0
1 1 1
Table 3.5: 2- input AND gate
X Y Z F=X.Y.Z
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
A A
A B
F B F F
B C
C D
The NOR gate has two or more input signals but only one output
signal. If all the inputs are 0 (low), then the output signal is 1
(high).
X Y F = X+Y
0 0 1
0 1 0
1 0 0
1 1 0
Table 3.7: 2-input NOR gate
X Y Z F=X+Y+Z
0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 0
(a)
The NAND Gate has two or more input signals but only one output
signal. If all of the inputs are 1 (high), then the output produced is
0 (low).
NAND action is illustrated in following Truth Tables (2.9 and 2.10)
X Y F = XY X Y Z F = XYZ
0 0 1 0 0 0 1
0 1 1 0 0 1 1
1 0 1 0 1 0 1
1 1 0 0 1 1 1
1 0 0 1
Table 3.9: Truth table of 2-input NAND gate 1 0 1 1
1 1 0 1
1 1 1 0
Table 3.10 Truth table of 3-input NAND gate
In Boolean algebra, sign stands for XOR operation. Thus, A XOR B can
be written as AB.
Following Truth Tables (2.11 and 2.12) illustrates XOR operation.
No. of 1’s X Y Z F
Even 0 0 0 0
Odd 0 0 1 1
Odd 0 1 0 1
Even 0 1 1 0
Odd 1 0 0 1
Even 1 0 1 0
Even 1 1 0 0
Odd 1 1 1 1
No. of 1’s X Y Z F
Even 0 0 0 1
Odd 0 0 1 0
Odd 0 1 0 0
Even 0 1 1 1
Odd 1 0 0 0
Even 1 0 1 1
Even 1 1 0 1
Odd 1 1 1 0
A AC F
C
AB+AC+BAC
B
BAC
A
C
Example 2.2 Draw the diagram of digital circuit for the function:
F(X, Y, Z) = (X + Y) . (X + Z) . (Y + Z)
Solution: Above expression can also be written as
F(X, Y, Z) = (X OR Y) AND ((NOT X) OR (NOT Z)) AND (Y OR Z)
Thus circuit diagram will be
Logic gates 95
X X+Y
Y
X X+Z F
Z
(X+Y).(X+Z).(Y+Z)
Y Y+Z
Z
Universal gate is a gate using which all the basic gates can be
designed. NAND and NOR gates are called as the universal gates.
NAND–to-NOT logic
Not Operation
X X
NOT X = X NAND X
= X.X {De Morgan's Second Theorem}
=X+X {De Morgan's Second Theorem}
=X
NAND–to-AND logic
AND and OR operations from NAND gates are shown below:
96 Logic gates
AND operation
X X.Y X.Y
Y
X+Y
Y
Y.Y
X X
Y Y
Z Z
=
Z XYZ+ZX XYZ+ZX
Z
X X
Example 2.3: Draw the diagram of a digital circuit for the function
F(X, Y, Z) = YZ + XZ using NAND gates only.
Solution : F(X, Y, Z) = YZ + XZ can be written as
= (Y NAND Z) NAND (X NAND Z)
Thus logic circuit diagram is
Y
Z Y.Z
X.Z YZ+ZX
X
Z
Example 2.4: Draw the diagram of digital circuit:
F (A, B, C) = AB + BC + CD using NAND-to-NAND logic.
Solution: F(A, B, C) = AB + BC + CD
= (A NAND B) NAND (B NAND C) NAND (C NAND D)
98 Logic gates
A A.B
B
B B.C
C AB+BC+CD
C C.D
D
A
B
C
ABC+CB
C
B
NOR-to-NOT logic
NOT, AND and OR operations can be implemented in NOR-to-NOR form
as shown on below.
A A
NOT X = X NOR X
= X+X
= X.X
=X
Logic gates 99
NOR to OR Operation
A + B = (A NOR B) NOR (A NOR B)
A A+B
A+B
B
A
A
A.B
B
B
A A.B
B
B B.C
C AB+BC+CD
C C.D
D
100 Logic gates
A B C X
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0
24.For the truth table given below, what type of logic gate does the output
X represent?
Logic gates 101
A B X
0 0 0
0 1 1
1 0 1
1 1 0
6.Draw the logic gate diagram to implement AND and OR gates using NAND
gates only. ( any two gates )
7.Draw the logic gate diagram to implement AND and OR gates using NOR
gates only. ( any two gates )
********
Data structure 103
Chatpter 4
Data Structures
Objectives:
4.1 Introduction
Data is a collection of raw facts that are processed by computers. Data
may contain single value or set of values. For example students name may contain
three sub items like first name, middle name, and last name, whereas students
regno can be treated as a single item. These data have to be processed to form
information.
In order to process the data the data should be organized in a particular
way. This leads to structuring of data. Utilization of space by data in memory
and optimization of time taken for processing, plays an important role in data
structures. Data structures are efficient way to organize the data.
Data structures provide a means to manage large amount of data efficiently.
Data structures are also key factor in designing efficient algorithms. In this
chapter we will discuss about various types of data structures and operations
performed on them.
Data structure means organization or structuring a collection of data items
in appropriate form so as to improve the efficiency of storage and processing.
We consider only the first and second cases are considered in this chapter.
Data structure 105
Integer
Floating
Primitive Data point
structure
Character
Pointers
Single
Dimension
Data
Structure Arrays Two
Dimension
Multi
Dimension
Stack
Non-Primitive
Data Structure Queues
Linear Data
Structures
Linked
Lists List
Files Trees
Non-Linear
Data structure
Graph
Traversal: The process of accessing each data item exactly once to perform
some operation is called traversing.
Insertion: The process of adding a new data item into the given collection
of data items is called insertion.
Deletion: The process of removing an existing data item from the given
collection of data items is called deletion.
Searching: The process of finding the location of a data item in the given
collection of data items is called as searching.
The data items in an array are called as elements. These elements are
accessed by numbers called as subscripts or indices. Since the elements are
accessed using subscripts, arrays are also called as subscripted variables.
4.5.1 Types of Arrays:
There are three types of array:
One-dimensional Array
Two-dimensional Array
Multi-dimensional array
4.5.2 One-dimension Array
An array with only one row or column is called one-dimensional array. It
is finite collection of n number of elements of same type such that
Elements are stored in contiguous locations
Elements can be referred by indexing
Array can be denoted as: data type Arrayname[size];
Here, size specifies the number of elements in the array and the index
(subscript) values ranges from 0 to n-1.
Data structure 109
Features:
Array size should be positive number only.
String array always terminates with null character (‘\0’).
Array elements are counted from 0 to n-1.
Useful for multiple reading of elements.
Calculating the length of Array
Arrays can store a list of finite number (n) of data items of same data type
in consecutive locations. The number n is called size or length of the array.
The length of an array can be calculated by L = UB – LB + 1
Here, UB is the largest index and LB is the smallest index of an array.
Example: If an array A has values 10, 20, 30, 40, 50, 60 stored in locations 0, 1,
2, 3, 4, 5 then UB = 5 and LB= 0
Size of the array L = 5 – 0 + 1 = 6
4.5.3 Representation of one-dimensional arrays in memory
Elements of linear array are stored in consecutive memory locations. Let P
be the location of the element. Address of first element of linear array A is given
by Base(A) called the Base address of A. Using this we can calculate the address
of any element of A by the formula
LOC(A[P]) = Base(A) + W(P – LB)
Here W is the number of words per memory cell.
Example: Suppose if a string S is used to store a string ABCDE in it with starting
address at 1000, one can find the address of fourth
element as follows:
cin>>n;
cout<<“Enter the elements: “;
for(i=0; i<n; i++)
cin>>a[i];
cout<<“Enter the search element: “;
cin>>ele;
pos=-1;
for(i=0; i<n ;i++)
if(ele == a[i])
{
pos = i;
break;
}
if(pos>= 0)
cout<<ele<<“ is present at position “<<pos<<endl;
else
cout<<ele<<“ is not present”<<endl;
getch( );
}
3. If ELE not found then we get a condition B>E. This results in unsuccessful
search.
Algorithm: A is the sorted array with LB as lower bound and UB as the upper
bound respectively. Let B, E, M denote beginning, end and middle locations of
the segments of A.
Step 1: set B = 0, E = n-1 LOC=-1
Step 2: while (B <= E)
M= int(B+E)/2
if(ELE = A[M]
loc = M
GOTO 4
else
if(ELE <A[M])
E = M-1
else
B = M+1
End of while
Step 3: if(LOC >= 0)
PRINT LOC
else
PRINT “Search is unsuccessful”
Step 4: Exit
m=(b+e)/2;
if(ele==a[m])
{
pos=m;
break;
}
else
if(ele<a[m])
e=m-1;
else
b=m+1;
}
if(pos>=0)
cout<<“Position= “<<pos;
else
cout<<“Search is unsuccessful”;
}
cout<<"Invalid position";
else
{
for(i=n-1; i>=p; i--)
a[i+1] = a[i];
a[p] = ele;
n = n+1;
cout<<"The elements after the insertion are ";
for(i=0; i<n; i++)
cout<<setw(4)<<a[i];
}
}
Column-major order
Let A be the array of order m x n. In column-major order, all the first-
column elements are stored in sequential memory locations and then all the
second-column elements are stored and so on.
Base(A) is the address of the first element. The memory address of any
element A[I][J] can be obtained by the formula
LOC(A[I][J]) = Base(A) + W[(I-LB) + m(J-LB)]
where W is the number of words per memory location.
Example: Consider the array of order 3 x 3.
Data structure 121
Example: Consider the array A of order 25 x 4 with base value 2000 and one
word per memory location. Find the address of A[12][3] in row-major order and
column-major order.
Solution:
Given Base(A) = 2000, m = 25, n=4 LB = 0
W = 1, I = 12, J=3
Row-major order: LOC(A[I][J]) = Base(A) + W[n(I-LB) + (J-LB)]
LOC(A[12][3]) = 2000 + 1[4(12-0)+(3-0)]
= 2000 + 4(12) + 3
= 2000 + 48 + 3
= 2051
Column-major order: LOC(A[I][J]) = Base(A) + W[ (I-LB) + m(J-LB)]
LOC(A[12][3]) = 2000 + 1[(12-0)+25(3-0)]
= 2000 + 1(12 + 75)
= 2000 + 87
= 2087
Applications of arrays
1. Arrays are used to implement other data structures such as heaps, hash
tables, queues, stacks and strings etc.
2. Arrays are used to implement mathematical vectors and matrices.
3. Many databases include one-dimensional arrays whose elements are
records.
Advantages of arrays
1. It is used to represent multiple data items of same type by using only
single name.
Data structure 123
2. It can be used to implement other data structures like linked lists, stacks,
queues, trees, graphs etc.
3. Two-dimensional arrays are used to represent matrices.
Disadvantages of arrays
1. We must know in advance that how many elements are to be stored in
array.
2. Array is static structure. It means that array is of fixed size. The memory
which is allocated to array cannot be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than requirement
then the memory space will be wasted. If we allocate less memory than
requirement, then it will create problem.
4. The elements of array are stored in consecutive memory locations. So
insertions and deletions are very difficult and time consuming.
STACKS
4.6.1 Introduction
A stack is an ordered collection of items where the addition of new items
and the removal of existing items always take place at the same end. This end is
commonly referred to as the “top”. The end opposite to top is known as the base.
The base of the stack is significant since items stored in the stack that are
closer to the base represent those that have been in the stack the longest. The
most recently added item is the one that is in position to be removed first. This
ordering principle is sometimes called LIFO, last-in first-out. Newer items are
near the top, while older items are near the base.
One of the most useful ideas related to stacks comes from the simple
observation of items as they are added and then removed. Assume you start out
with a clean desktop. Now, place books one at a time on top of each other. You
are constructing a stack. Consider what happens when you begin removing
books. The order that they are removed is exactly the reverse of the order that
they were placed. Stacks are fundamentally important, as they can be used to
reverse the order of items. The order of insertion is the reverse of the order of
removal.
Linked list data structure. The linked list structure will be studied in detail
later.
START
50 25 20 NULL
Insertion
Deletion
The stack abstract data type is defined by the following structure and
operations. A stack is structured, as described above, as an ordered collection of
items where items are added to and removed from the end called the “top.”
Stacks are ordered LIFO. The stack operations are given below.
push(item) adds a new item to the top of the stack. It needs the item
and returns nothing.
pop() removes the top item from the stack. It needs no parameters and
returns the item. The stack is modified.
peek() returns the top item from the stack but does not remove it. It
needs no parameters. The stack is not modified.
Language processing:
Space for parameters and local variables is created internally
using a stack.
Compiler’s syntax check for matching braces is implemented by
using stack.
support for recursion
Conversion of decimal number into binary
To solve tower of Hanoi
Expression evaluation and syntax parsing
Conversion of infix expression into prefix and postfix.
Rearranging railroad cars
Data structure 129
Quick sort
Stock span problem
Runtime memory management
Arithmetic expression: An expression is a combination of operands and operators
that after evaluation results in a single value. Operands consist of constants and
variables. Operators consists of {, +, - , * , / …….. etc., ) [ Expressions can be
Infix expression
Post fix expression
Prefix expression
Infix expression: If an operator is in between two operands it is called infix
expression.
Example: a + b, where a and b are the operands and + is an operator.
Postfix expression: If an operator follows the two operands it is called post fix
expression.
Example: ab +
Prefix expression: If an operator precedes two operands, it is called prefix
expression.
Example: +ab
Algorithm for Infix to Postfix
Queues
4.7.1 Introduction
We now turn our attention to another linear data structure. This one is
called queue. Like stacks, queues are relatively simple and yet can be used to
solve a wide range of important problems.
A queue is an ordered collection of items where the addition of new items
and the removal of existing items always take place at different ends.
FRONT REAR
1. Simple Queue: In Simple queue insertion occurs at the rear end of the list,
and deletion occurs at the front end of the list.
FRONT REAR
FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
2. Circular Queue: A circular queue is a queue in which all nodes are treated as
circular such that the last node follows the first node.
REAR
FRONT = 1
F D E A B C
REAR = 5
0 1 2 3 4 5 6 7 8
3. Priority Queue: A priority queue is a queue that contains items that have
some preset priority. An element can be inserted or removed
from any position depending on some priority.
Insertion Insertion
Deletion Deletion
Data structure
136
enqueue(item) adds a new item to the rear of the queue. It needs the
item and returns nothing. This operation is generally called as push.
dequeue() removes the front item from the queue. It needs no parameters
and returns the item. The queue is modified. This operation is generally
called as pop.
FRONT REAR
FRONT = 1
A B C D
REAR = 4
0 1 2 3 4 5 6 7 8
FRONT REAR
FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
Data structure 137
FRONT = 2
B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
Queues are also represented using linked lists. In queues an item should
be inserted from rear end and an item should be removed from the front end.
The pointer front contains location of the first node of the linked list and another
pointer rear contains location of the last node.
50 25 20 NULL
50 25 20 75 NULL
25 20 75 NULL
LINKED LISTS
4.8.1 Introduction
One disadvantage of using arrays is that arrays are static structures and
therefore cannot be easily extended or reduced to fit the data set. During
insertion, the array elements are shifted into higher order memory locations
and during deletion, elements ate shifted into lower order memory locations. In
this chapter we study another data structure called Linked Lists that addresses
these limitations of arrays. The linked list uses dynamic memory allocations.
Linked list
A linked list is a linear collection of data elements called nodes and the
linear order is given by means of pointers.
Each node contains two parts fields: the data and a reference to the next
node. The first part contains the information and the second part contains the
address of the next node in the list. This is also called the link field.
Data Link
START
30 20 50 10 X
The above picture is linked list with 4 with nodes, each node is contains
two parts. The left part of the node represents the information part of the node
and the right part represents the next pointer field that contains the address of
the next node. A pointer START gives the location of the first node. This pointer
is also represented as HEAD. Note that the link field of the last node contains
NULL.
4.8.2 Types of linked lists
There are three types of linked lists.
1. Singly linked list (SLL)
2. Doubly linked list (DLL)
3. Circular linked list (CLL)
Single linked list
A singly linked list contains two fields in each node – the data field and
link field. The data field contains the data of that node while the link field
contains address of the next node. Since there is only one link field in each
node, the linked list is called as singly linked list.
Data structure
140
START
50 40 70 20 NULL
In any linked list the nodes need not necessarily represent a set of
consecutive memory locations (or contiguous memory locations).
Circular linked lists:
In a singly linked list, a pointer is available to access all the succeeding
nodes, but not preceding nodes. In the singly linked lists, the link field of the
last node contains NULL.
In circular lists, if the link field of the last node contains the address of the
first node first node, such a linked list is called as circular linked list.
In a circular linked list, it is possible to reach any node from any other
STAR
node.
T
Header node
INFO
In the first node, if BACK contains NULL, it indicates that it is the first
node in the list. The node in which FORW contains NULL indicates that the
node is the last node in the linked list.
STAR T
T
x x
Dynamically the memory space is allocated for the linked list using new
operator as follows:
Node *newNode;
p = new Node;
data(p) = num;
link(p) = NULL;
Program: Creating a linked list and appending nodes into the linked list.
#include <iostream.h>
class LinkList
{
private:
struct Node
{
int data;
Node* link;
}*START;
public:
LinkList();
void Print(); //Prints the contents of linked list
void Append(int num); //Adds a new node at the end
of the linked list
void Count(); //Counts number of nodes in the linked
list
};
LinkList::LinkList() //Constructor
{
START = NULL;
}
return;
}
//Traverse
cout<<“Linked list contains”;
Node* p = START;
while(p != NULL)
{
cout<<“ “<<p->data;;
p = p->link ;
}
}
// Adds a new node at the end of the linked list
void LinkList::Append(int num)
{
Node *newNode;
newNode = new Node;
newNode->data = num;
newNode->link = NULL;
if(START == NULL)
{
//create first node
START = newNode;
cout<<endl<<num<<“ is inserted as the first node”<<endl;
}
else
{
//Traverse
Node *p = START;
while(p->link != NULL)
p = p->link;
//add newnode to the end of the linked list
p->link = newNode;
cout<<endl<<num<<“ is inserted”<<endl;
}
}
// Counts number of nodes present in the linked list
void LinkList::Count()
{
Node *p;
int c = 0;
//Traverse the entire Linked List
for (p = START; p != NULL; p = p->link)
c++;
Data structure 145
Algorithm: This algorithm traverses the linked list. Here START contains the
address of the first node. Another pointer p is temporarily used to visit all the
nodes from the beginning to the end of the linked list.
S T A R T
4 0 7 0 2 0 x
5 0
N e w n o d e
#include <iostream.h>
#include<ctype.h>
class LinkList
{
private:
struct Node
{
int data;
Node* link;
}*START;
public:
LinkList();
void Print();
void Count();
void insert(int num);
};
LinkList::LinkList()
{
START = NULL;
}
void LinkList::Print()
{
if (START == NULL)
{
cout<< “Linked list is empty”<<endl;
return;
}
cout<<endl<<“Linked list contains”;
Data structure
148
Node* p = START;
while(p != NULL)
{
cout<<“ “<<p->data;;
p = p->link ;
}
}
void LinkList::insert(int num)
{
Node *newNode;
newNode = new Node;
newNode->data = num;
newNode->link = START;
START = newNode;
cout<<num<<“ is inserted at the beginning”<<endl;
}
void LinkList::Count()
{
Node *p;
int c = 0;
//Traverse the entire Linked List
for (p = START; p != NULL; p = p->link)
c++;
cout<<endl<<“No. of elements in the linked list= “<<c<<endl;
}
void main()
{
LinkList* obj = new LinkList;
int item;
char ch;
START
50 40 70
20 NULL
New node
1. START
2. [Identify the last node in the list]
a. p START
b. while p != NULL
p next(p)
while end
3. N new Node [Create new node copy the address to the pointer N]
4. data(N) item
5. link(N) NULL
6. link(p) N
7. RETURN
Inserting a node at a given position
We can insert a node at a given position. The following procedure inserts
a node at the given position.
Data structure
150
START
50 40 20 NUL
70
New node
Algorithm (INST-POS): This algorithm inserts item into the linked list at the
given position pos.
1. START
2. [[Initialize] count 0
p1 START
3. while (p1 != NULL)
count count +1
p1 link(p1)
while end
4. a. if (pos = 1)
call function INST-BEG()
else
b. if (pos = count +1)
call function INST-END()
else
c. if( pos <= count)
p1 START
for (i = 1; i <= pos; i++ )
p1 next(p1)
for end
d. [create] p2 new node
data(p2) item
link(p2) link(p1)
link(p1) p2
else
e. PRINT “ Invalid position “
5. RETURN
Garbage collection:
If a node is deleted from the linked list or if a linked list is deleted, we
require the space to be available for future use. The memory space of the deleted
nodes is immediately reinserted into the free-storage list. The operating system
of the computer periodically collects all the deleted space into the free-storage
Data structure 151
list. This technique of collecting deleted space into free-storage list is called as
garbage collection.
20 30 50 40 X
Algorithm (DELE-BEG): This algorithm first copy data in the first node to a variable
and deletes the first node of the linked list.
Step1. START
Step2. p START
Step3. PRINT data(p)
Step4. START link(p)
Step5. free(p)
Step6. RETURN
Deleting a node at the end (DELE-END)
To delete the last node, we should find location of the second last node.
By assigning NULL to link field of the second last node, last node of the linked
list can be deleted.
START
p1 P2
20 30 50 x 40 X
Data structure
152
Algorithm (DELE-END): This used two pointers p1 and p2. Pointer p1 is used to
traverse the linked list and pointer p2 keeps the location of the previous node of
p1.
1. START
2. p2 START
3. while ( link(p2) != NULL)
p1 p2
p2 link(p2)
while end
4. PRINT data(p2)
5. link(p1) NULL
free(p1)
6. STOP
1. START
2. Count 0
p1 START
3. while(p1 != NULL)
count = count +1
p1 link(p1)
while end
4. if( pos = 1)
CALL DELE-BEG()
else
if(pos = count)
CALL DELE-END()
else
if(pos < count)
p2 START
for i = 1 to pos
p1 p2
p2 link(p2)
for end
Data structure 153
PRINT data(p2)
link(p1) link(p2)
free(p2)
else PRINT "Invalid position"
5. RETURN
The length of the data items is not necessary to be known prior to allocation.
Cons
Overhead of the link to the next data item.
4.9.2 TREES
Terminology
A node is a structure which may contain a value, a condition, or represent
a separate data structure (which could be a tree of its own). Each node in a tree
has zero or more child nodes, which are below it in the tree (by convention,
trees are drawn growing downwards). A node that has a child is called the parent
node (or ancestor node, or superior). A node has at most one parent.
Nodes that do not have any children are called leaf nodes. They are also
referred to as terminal nodes.
The height of a node is the length of the longest downward path to a leaf
from that node. The height of the root is the height of the tree. The depth of a
node is the length of the path to its root (i.e., its root path).
The topmost node in a tree is called the root node. Being the topmost
node, the root node will not have parents. It is the node at which operations on
the tree commonly begin. All other nodes can be reached from it by following
edges or links.
An internal node or inner node is any node of a tree that has child nodes
and is thus not a leaf node.
A subtree of a tree T is a tree consisting of a node in T and all of its
descendants in T.
Binary trees
The simplest form of tree is a binary tree. A binary tree is a tree in which
each node has at most two descendants - a node can have just one but it can’t
have more than two.
A binary tree consists of
a. a node (called the root node) and
b. left and right sub-trees.
Both the sub-trees are themselves binary trees.
The importance of a binary tree is that it can create a data structure that
mimics a “yes/no” decision making process.
Data structure 155
Root Node: Node at the “top” of a tree - the one from which all operations on the
tree commence. The root node may not exist (a NULL tree with no nodes in it) or
have 0, 1 or 2 children in a binary tree.
Leaf Node: Node at the “bottom” of a tree - farthest from the root. Leaf nodes
have no children.
Complete Tree: Tree in which each leaf is at the same distance from the root.
i.e. all the nodes have maximum two subtrees.
Height: Number of nodes which must be traversed from the root to reach a leaf
of a tree.
4.9.3 GRAPHS
A graph is a set of vertices and edges which connect them. A graph is a
collection of nodes called vertices, and the connections between them, called
edges.
Undirected and directed graphs
When the edges in a graph have a direction, the graph is called a directed
graph or digraph, and the edges are called directed edges or arcs.
Neighbors and adjacency
A vertex that is the end point of an edge is called a neighbor of the vertex,
that is its starting-point. The first vertex is said to be adjacent to the second.
The following diagram shows a graph with 5 vertices and 7 edges. The
edges between A and D and B and C are pairs that make a bidirectional
connection, represented here by a double-headed arrow.
Data structure
156
Chatpter 5
Review of C++
Objectives:
The simple or fundamental data types are the primary data types which
are not composed of any other data types.
The simple data types/fundamental data types include int, char, float
double and void.
Modifiers
The data type modifiers are listed here: signed, unsigned, long and short.
Example: unsigned int b;
Derived data types
These data types are constructed using simple or fundamental data types.
They include arrays, functions, pointers and references.
User defined data types
These data types are also constructed using simple or fundamental data
types. Some user defined data types include structure, union, class and enumerated.
5.6 Input and output Operators
Input and output operators are used to perform input and output operations.
Input Operator “>>”
The standard input device is usually the keyboard. Input in C++ is done by
using stream extraction operator (>>) on the cin stream. The operator must be
followed by the variable that will store the data that is going to be extracted from
the stream.
Example : int age;
cin>>age;
Output Operator “<<“
The standard output device is the screen (monitor) and outputting in C++
is done by using the object followed by the stream insertion operator which is
written as << . cout stands for Console output.
Review of C++167 167
Example: cout<< “ Let us learn C++”; // prints Let us learn C++
//on the screen.
Cascading of I/O operators:
C++ supports the use of stream extraction (<<) and stream insertion (>>)
operators many times in a single input (cin) and output (cout) statements. If a
program requires more than one input variable then it is possible to input these
variables in a single cin statement using multiple stream extraction operators.
Similarly, when we want to output more than one result then this can be done
using a single cout statement with multiple stream insertion operators. This is
called as cascading of input output operators.
Example : cout<<“ enter the value for x”;
cin>> x;
cout<<“ enter the value for y”;
cin>>y;
Instead of using cin statement twice, we can use a single cin statement
and input the values for the two variables x and y using multiple stream extraction
operator as shown below.
cout<<“ enter the value for x and y”;
cin>>x>>y;
Similarly, we can even output multiple results in a single cout statement
using cascading of stream insertion operator as shown below.
cout<<“ the sum of” <<x<<“ and “<<y<<“=”<<x+y;
5.7 Control Statements
C++ provides us control structures, statements that can alter the flow of a
sequence of instructions. .
Compound statements
Compound statements or block is a group of statements which are
separated by semicolons (;) and grouped together in a block enclosed in braces {
and } is called a compound statement.
Example: {
temp = a;
a = b;
b = temp;
}
Types of control statements
C++ supports two basic control statements.
· Selection statements
· Iteration statements
168 Review of C++
These numbers are called as indices or subscripts. These numbers are used to
locate the positions of elements within the array.
The method of numbering the ith element with index i-1 is called as zero-
based indexing. That is, the element is always same as the number of “steps”
from the initial element a[0] to that element. For example, the element a[3] is 3
steps from the element a[0].
Types of arrays
There are three types of arrays.
i. One-dimensional array
ii. Two-dimensional array
iii. Multi-dimensional array
One-dimensional array
It is an array in which each element is accessed by using only one subscript.
The only one subscript represents the position of the element in the array.
Two-dimensional array
It is an array in which each element is accessed using 2-subsripts. The
subscripts represent the position of element in the array.
Multi-dimensional array
A multidimensional array is an array of n-dimensions. In other words, an
array of arrays is called a multidimensional array. A one-dimensional array of one-
dimensional arrays is called a two-dimensional array; a one-dimensional array to
two-dimensional arrays is called a three-dimensional array and so on.
One-dimensional array:
It is an array in which each element is accessed by using only one subscript.
The only one subscript represents the position of the element in the array.
Declaration of one-dimensional array
Syntax datatype array-name[size];
Example : int marks[50];
Initialization of one-dimensional arrays
You can give values to each array element when the array is first defined.
Example : int a[5] = {9, -5, 6, 2, 8};
In the above example, value 9 is stored in a[0], value -5 is stored in a[1],
value 6 is store in a[2], value 2 is stored in a[3] and value 8 is store in a[4].
Memory representation of one-dimensional arrays:
The elements of one-dimensional arrays are stored in contiguous memory
locations.