Anda di halaman 1dari 119

KENDRIYA VIDYALAYA SANGATHAN

JAMMU REGION
.
STUDY/SUPPORT MATERIAL
2011-12
CLASS: XII

Computer Sciece
1
AC!"O#LED$EME"TS
C%ie& P'tro S%ri A(i')% Di*)%it
Commissioner
KVS, New Delhi
P'tro S%ri P+ !+ !ou,
Assistant Commissioner
KVS, Jammu Region
$ui-'ce S%ri Y+ P+Si.%
Education Officer
KVS, Jammu Region
S%ri !+ S+ Y'-'(
Education Officer
KVS, Jammu Region
STUDY/SUPPORT MATERIAL PREPARATIO" COMMITTEE
Co-or-i'tor Dr+ C%itr' Mi)%r'
rinci!al, KV No." Jammu
Mem/er):
1+ S%+ Aru !um'r
#$%Com!.Sc.&, KV No." Jammu
2+ S%+ R'me)% C%'- T%'*ur
#$%Com!.Sc.&, K.V. 'a(han!ur
0+ S%+ 1'ri-er Si.%
#$%Com!.Sc.&, K.V.) *dham!ur
2+ S%+ S%i( Pr't'p P',
#$%Com!.Sc.&, K.V.No.", A(hnoor
STUDY/SUPPORT MATERIAL RE3IE# COMMITTEE
Co-or-i'tor Smt+ 3iit' P'r)%eer'
+,c rinci!al KV No." Jalandhar
Mem/er):
1+ Mr)+ S4'ti A.'r4',
#$ %Com!uter Science&, KV No." Jalandhar
2+ S%+ Ume- A,i
#$ %Com!uter Science&, KV No.- Jalandhar
2
SUPPORT MATERIAL
COMPUTER SCIE"CE
2011-12
CBSE Mark Distribution for different Topics (Important Lessons)
S,"o Uit "'me M'r*)
1 U"IT 1 Pro.r'mmi. i C55 ./
2 U"IT 2 D't' )tructure) "-
0 U"IT 0 D't'/')e '- S6L /0
2 U"IT 2 1oo,e' Lo.ic /0
7 U"IT 7 Commuic'tio '- ope )ource cocept "/
Tot', M'r*) 80
Weightage to different toi!"#!ontent $nit"
Sr% No% Toi! Mar&"
' Re(ie) of *++ !o(ered in *,a"" -I '.
. O/0e!t oriented rogra11ing in *+
+
'.
2 Data Str$!t$re 3 4ointer" '5
5 Data 6i,e Hand,ing in *++ 78
9 Data/a"e" and S:L 7;
8 <oo,ean A,ge/ra 7;
= *o11$ni!ation and Oen So$r!e
*on!et"
'7
Tota, =7
Weightage to different for1" of >$e"tion"
S%
No%
6or1" of :$e"tion Mar&" for ea!h
>$e"tion
No% of
:$e"tion"
Tota,
Mar&"
' Ver? Short An")er :$e"tion" @VSAA 7' 7B 7B
. Short An")er :$e"tion"C T?e ' @SA'A 7. '2 .8
2 Short An")er :$e"tion"C T?e II @SAIIA 72 79 '9
5 Long An")er :$e"tion"C @LAA 75 79 .7
Tota, 2. =7
Diffi!$,t? Le(e, of :$e"tion"
S% N% E"ti1ated Diffi!$,t? Le(e, 4er!entage of >$e"tion"
' Ea"? '9D
. A(erage =7D
2 Diffi!$,t '9D
3
I"DEX
S+"o+
Topic) PA$E
"O+
1 O(er(ie4 o& C55 7
2 1')ic Cocept) o& OOP 9 C,'))e) '- O/:ect) 11
0 D't' ;i,e <'-,i. 27
2 Poiter) 02
7
D't' Structure)
1+ Arr'=)
2+ St'c*)
0+ 6ueue
20
> D't'/')e A- S6L 80
8 1oo,e' A,.e/r' ?0
? Commuic'tio A- Ope Source Cocept) @2
@ 6ue)tio P'per 4it% So,utio AM'rc%-2011B 111

O(er(ie4 o& C55
4
!EY POI"TS:
Itro-uctio to C55
C11 is the successor of C language 2 de3elo!ed 45 67arne Stroustru! at 6ell 'a4oratories,
New Jerse5 in "898.
To*e): smallest indi3idual unit. ;ollowing are the to(ens
!e=4or-:Reser3e word that can<t 4e used as identifier
I-eti&ier)-Names gi3en to an5 3aria4le, function, class, union etc.
Liter',):Value of s!ecific data t5!e
3'ri'/,e: memor5 4loc( of certain si=e where 3alue can 4e stored and changed.
Co)t't: memor5 4loc( where 3alue can 4e stored once 4ut can<t changed later on
Oper'tor > !erforms some action on data
o Arithmetic%1,:,?,,,@&
o Relational,com!arison %A,B,AC,BC,CC,DC&.
o 'ogical%AND%22&,OR%EE&,NO$%D&.
o Conditional %F G&
o +ncrement,Decrement O!erators% 11,::&
Prece-ece o& oper'tor):
11%!ost increment&,::%!ost decrement& Highest
'ow
11%!re increment&,::%!re decrement&,si=eof D%not&,:%unar5&,1unar5 !lus&
?%multi!l5&, , %di3ide&, @%modulus&
1%add&,:%su4tract&
A%less than&,AC%less than or eIual&,B%greater than&, BC%greater than or eIual to&
CC%eIual&,DC%not eIual&
22 %logical AND&
EE%logical OR&
FG%conditional eJ!ression&
C%sim!le assignment& and other assignment o!erators%arithmetic assignment
o!erator&
, Comma o!erator
D't' t=pe- A s!ecifier to create memor5 4loc( of some s!ecific si=e and t5!e. ;or eJam!le >
int,float,dou4le,char etc.
cout > +t is an o47ect of o)tre'mC4it%'))i. class defined in iostream.h header file and used to
dis!la5 3alue on monitor.
ci > +t is an o47ect of i)tre'mC4it%'))i. class defined in iostream.h header file and used to read
3alue from (e54oard for s!ecific 3aria4le.
commet: *sed for 4etter understanding of !rogram statements and esca!ed 45 the com!iler to
com!ile . e.g. > single line %,,& and multi line%,?K.?,&
Cotro, )tructure :
SeDuece
cotro,
)t'temetAi&B
co-itio',
)t'temet
Ai& e,)eB
c')e cotro, )t'temet
A)4itc% c')eB
,oop cotro, )t'temet
A4%i,e E-oF 4%i,eE &orB
S5ntaJ S5ntaJ S5ntaJ S5ntaJ
if%eJ!ression&
L
statementsM
N
+f%eJ!ression&
L
statementsM
N
else
L
statementsM
N
switch%integral eJ!ression&
Lcase %int const eJ!r"&G
Ostatements
4rea(MP
case %int const eJ!r)&G
Ostatements,
4rea(MP
defaultGStatementsMN
4%i,eAeGpre))ioB
LstatementsMN
-o F+4%i,e ,oop
do
LstatementMN while%eJ!ression&M
&or ,oop
for%eJ!ression"MeJ!ression)MeJ!ression.&
LstatementMN
"ote: '= o-Hero (',ue o& ' eGpre))io i) tre'te- ') true '- eG'ct,= 0 Ai+e+ ',, /it) cot'i
0B i) tre'te- ') &',)e+
"e)te- ,oop :loo! within loo!.
eGitAB- defined in !rocess.h and used to lea3e from the !rogram.
/re'*: eJit from the current loo!.
5
cotiue: to s(i! the remaining statements of the current loo! and !asses control to the neJt loo!
control statement.
.oto- control is unconditionall5 transferred to the location of local la4el s!ecified 45 AidentifierB.
;or eJam!le
A"G
coutAAQtestQM
goto A"M
Some St'-'r- C55 ,i/r'rie)
<e'-er "ome Purpo)e
iostream.h Defines stream classes for in!ut,out!ut streams
stdio.h Standard in!ut and out!ut
ct5!e.h Character tests
string.h String o!erations
math.h Rathematical functions such as sin%& and cos%&
stdli4.h *tilit5 functions such as malloc%& and rand%&
Some &uctio)
i)',p%'AcB-chec( whether the argument is al!ha4etic or not.
i),o4erAcB- chec( whether the argument is lowecase or not.
i)upperAcB : chec( whether the argument is u!ercase or not.
i)-i.itAcB- chec( whether the argument is digit or not.
i)',umAcB- chec( whether the argument is al!hanumeric or not.
to,o4erAB-con3erts argument in lowercase if its argument is a letter.
toupperAcB- con3erts argument in u!!ercase if its argument is a letter.
)trc'tAB- concatenates two string.
)trcmp-com!are two string.
po4AGE=B-return J raised to !ower 5.
)DrtAGB-return sIuare root of J.
r'-omAumB-return a random num4er 4etween / and %num:"&
r'-omiHe- initiali=es the random num4er generator with a random 3alue.
Arr'=- Collection of element of same t5!e that are referred 45 a common name.
Oe Dime)io 'rr'=
An arra5 is a continuous memor5 location holding similar t5!e of data in single row or single
column
T4o -ime)io', 'rr'=
A two diamensional arra5 is a continuous memor5 location holding similar t5!e of data in of
4oth row sand columns %li(e a matriJ structure&.
;uctio :Self:contained 4loc( of code that does some s!ecific tas( and ma5 return a 3alue.
;uctio protot=pe):;unction declaration that s!ecifies the function name, return t5!e and
!arameter list of the function.
)=t'G: returCt=pe &uctioC'meAt=pe ('r1Et=pe ('r2EF+Et=pe ('r BI
P'))i. (',ue to &uctio-
assing 45 3alue
assing 45 address,reference
;uctio o(er,o'-i.
rocessing of two or more functions ha3ing same name 4ut different list of !arameters
;uctio recur)io
;unction that call itself either directl5 or indirectl5.
Loc', ('ri'/,e)
Declared inside the function.
$,o/', ('ri'/,e)
Declared outside all 4races L N in a !rogram
Actu', P'r'meter)
Varia4les associated with function name during function call statement.
;orm', P'r'meter)
Varia4les which contains co!5 of actual !arameters inside the function definition.
6
Structure:Collection of logicall5 related different data t5!es %rimiti3e and Deri3ed& referenced under
one name.
"e)te- )tructure
A Structure definition within another structure.
A structure containing o47ect of another structure.
t=pe-e&
*sed to define new data t5!e name
S-e&ie Directi(e)
*se to define a constant num4er or macro or to re!lace an instruction.
I,ie ;uctio
+nline functions are functions where the call is made to inline functions, the actual code then
gets !laced in the calling !rogram.
#%'t %'ppe) 4%e ' i,ie &uctio i) 4ritteJ
$he inline function ta(es the format as a normal function 4ut when it is com!iled it is com!iled
as inline code. $he function is !laced se!aratel5 as inline function, thus adding reada4ilit5 to
the source !rogram. Then the !rogram is com!iled, the code !resent in function 4od5 is
re!laced in the !lace of function call.
$eer', ;orm't o& i,ie ;uctio:
$he general format of inline function is as followsG
i,ie -'t't=pe &uctioC'meA'r.umet)B
i,ie it M=C,'))A B
EG'mp,e:
Sinclude Aiostream.hB
int R5Class%int&M
3oid main% &
Lint JM
cout AA UVn Enter the +n!ut ValueG QM
cinBBJM
coutAAQVn $he Out!ut isG U AA R5Class%J&M
N
inline int R5Class%int J"&
Lreturn W?J"MN
$he out!ut of the a4o3e !rogram isG
Enter the +n!ut ValueG "/
$he Out!ut isG W/
$he out!ut would 4e the same e3en when the inline function is written solel5 as a function.
$he conce!t, howe3er, is different. Then the !rogram is com!iled, the code !resent in the
inline function R5Class % & is re!laced in the !lace of function call in the calling !rogram. $he
conce!t of inline function is used in this eJam!le 4ecause the function is a small line of code.
$he a4o3e eJam!le, when com!iled, would ha3e the structure as followsG
Sinclude Aiostream.hB
int R5Class%int&M
3oid main% &
Lint JM
cout AA UVn Enter the +n!ut ValueG QM cinBBJM
,,$he R5Class%J& gets re!laced with code return W?J"M
coutAAQVn $he Out!ut isG U AA R5Class%J&M
N
Sinclude Aiostream.hB
int R5Class%int&M
3oid main% &
Lint JM
cout AA UVn Enter the +n!ut ValueG QM
cinBBJM
,,Call is made to the function R5Class
7
coutAAQVn $he Out!ut isG U AA R5Class%J&M
N
int R5Class%int J"&
Lreturn W?J"MN
1 M'r*) Due)tio)
1B "'me t%e %e'-er &i,e) t%'t )%',, /e ee-e- &or t%e &o,,o4i. co-e:
3oid main% &
L char wordOPCQ6oard EJamQM
coutAAsetw%)/&AAwordM
N
2B Name the Header file to which the following 4uilt in functions 4elongs toG
%i& gets%& %ii& a4s%& %iii& sIrt%& %i3& o!en%&
2 M'r*) Due)tio):
"& Re4rite t%e &o,,o4i. pro.r'm '&ter remo(i. t%e )=t'ctic', errorA)B i& '=+ U-er,ie
e'c% correctio.
SincludeAiostream.hB
3oid main% &
L ; C "/, S C )/M
test%;MS&M
test%S&M
N
3oid test%int J, int 5 C )/&
L JCJ15M
countAAJBB5M
N
2B ;i- t%e output o& t%e &o,,o4i. pro.r'm:
SincludeAiostream.hB
3oid main% &
L int *C"/,VC)/M
for%int +C"M+AC)M+11&
L coutAAQO"PQAA*11AAQ2QAAV W AAendlM
coutAAQO)PQAA11VAAQ2QAA* 1 ) AAendlM N N
.& Re4rite t%e &o,,o4i. C55 pro.r'm '&ter remo(i. t%e )=t'G errorA)B i& '=+ U-er,ie
e'c% correctio+ KC1SE 2010L
includeAiostream.hB
class ;'+#H$
L 'ong ;lightCodeM
Char Descri!tionO)WPM
!u4lic
3oid add+nfo%&
L cinBB;lightCodeM gets%Descri!tion&MN
3oid show+nfo%&
L coutAA;lightCodeAAQGQAADescri!tionAAendlMN
NM
3oid main% &
L ;'+#H$ ;M
add+nfo.;%&M
show+nfo.;M
N
2B I t%e &o,,o4i. pro.r'mE &i- t%e correct po))i/,e outputA)B&rom t%e optio):
SincludeAstdli4.hB
SincludeAiostream.hB
3oid main% &
L randomi=e% &M
char Cit5O PO"/PCLUDE'Q, UCHNQ, UKO'Q, U6ORQ, U6N#QNM
8
int ;l5M
for%int +C/M +A.M+11&
L;l5Crandom%)& 1 "M
coutAACit5O;l5PAA UGQM
NN
Output):
%i& DE' G CHN G KO'G %ii& CHNG KO' G CHNG
%iii& KO' G 6OR G 6N#G %i3& KO' G CHN G KO'G
7B I t%e &o,,o4i. C55 pro.r'm 4%'t i) t%e eGpecte- (',ue o& M=)core &rom optio) AiB to
Ai(B .i(e /e,o4+ Mu)ti&= =our ')4er+
SincludeAstdli4.hB
SincludeAiostream.hB
3oid main% &
L randomi=e% &M
int ScoreO P C L)W,)/,.-,WX,9),X.N,R5scoreM
coutAAR5scoreAAendlM
N
+i& )W %ii& .- %iii& )/ %i3& #ar4age Value.
A)4er to 6ue)tio)
1 M'r*) A)4er
1B A): iostream.h
iomani!.h
2B AnsG iostream.h %ii& math.h,stdli4.h %iii& math.h %i3&fstream.h
2 m'r*) A)4er)
1 A):
SincludeAiostream.hB
3oid test%int J,int 5C)/&M ,,rotot5!e missing
3oid main% &
L int ; C "/, S C )/M ,,Data t5!e missing
$eJt%;ES&M ,,Comma to come instead of M
$eJt%S&MN
3oid $eJt%int J, int 5&
L JCJ15M
coutAAJAA5M ,,Out!ut o!erator AA reIuired N
2 A):Out!utG
O"P"/2"W
O)P)"2".
O"P""2"X
O)P))2"-
0 A): SincludeAiostream.hB
class ;'+#H$
L long ;lightCodeM
char Descri!tionO)WPM
!u4licG
3oid add+nfo%&
L cinBB;lightCodeM gets%Descri!tion&MN
3oid show+nfo%&
L coutAA;lightCodeAAQGQAADescri!tionAAendlMN
NM
3oid main% &
L ;'+#H$ ;M
;.add+nfo%&M
;.show+nfoM
N
2 A)B
Since random%)& gi3es either / or ", ;l5 3alue will 4e either " or ).
%random%n& gi3es 5ou an5 num4er 4etween / to n:"& Cit5O"P is .CHN.
Cit5O)P is .KO'.
9
Since + 3alue from / to ) %ieA.&, . iterations will ta(es !lace.
So the !ossi4le out!ut consists . strings se!arated 45 G, each of
them ma5 4e either .CHN. or .KO'..
So t%e po))i/,e output 4i,, /e
AiiB C<" : !OL : C<":
Ai(B !OL :C<" : !OL:
7 A): EJ!ected Out!utG
%i3& #ar4age 3alue since R5score is an uninitiali=ed local 3aria4le.
10
1ASIC CO"CEPTS O; OOPS 9 CLASSES A"D O1MECTS
O47ect:Oriented rogramming grou!s related data and functions together in a class, generall5 ma(ing
data !ri3ate and onl5 some functions !u4lic. Restricting access decreases cou!ling and increases
cohesion. +t has !ro3en to 4e 3er5 effecti3e in reducing the com!leJit5 increase with large !rograms.
;or small !rograms ma5 4e difficult to see the ad3antage of OO o3er, eg, structured !rogramming
4ecause there is little com!leJit5 regardless of how itYs written.
+t hel!s in !rogramming a!!roach in order to 4uilt ro4ust user friendl5 and efficient software and
!ro3ides the efficient wa5 to maintain real world software. OO is an O47ect Oriented rogramming
language which is the eJtension of rocedure Oriented rogramming language. OOs reduce the
code of the !rogram 4ecause of the eJtensi3e feature of ol5mor!hism. OOs ha3e man5 !ro!erties
such as DataHiding, +nheritance Data A4straction, Data Enca!sulation and man5 more. $he main aim
is to creating an O47ect to the Entire !rogram and that to we can control entire !rogram using the
O47ect. $he main features of OOS is ol5mor!hism, Rulti!le +nheritance, A4straction and
Enca!sulation.
O/:ect):
O47ect is the 4asic unit of o47ect:oriented !rogramming. O47ects are identified 45 its uniIue name. An
o47ect re!resents a !articular instance of a class.
An O47ect is a collection of data mem4ers and associated mem4er functions also (nown as methods.
C,'))e):
Classes are data t5!es 4ased on which o47ects are created.
$hus a Class re!resents a set of indi3idual o47ects. Characteristics of an o47ect are
re!resented in a class as ro!erties. $he actions that can 4e !erformed 45 o47ects 4ecome
functions of the class and are referred to as Rethods.
Classes are the 4lue!rints u!on which o47ects are created. E.g when we design a ma! of the
house, the architect first designs it. Once it is designed, the raw material is used to 4uild the
house. +n this eJam!le, Design of the house is C'ASS %4lue!rint& and the house 4uilt on the
4asis of design is an o47ect.
No memor5 is allocated when a class is created. Remor5 is allocated onl5 when an o47ect is created,
i.e., when an instance of a class is created.
I%erit'ce:
+nheritance is the !rocess of forming a new class from an eJisting class or 4ase class. $he
4ase class is also (nown as !arent class or su!er class. $he new class that is formed is called
deri3ed class.
Deri3ed class is also (nown as a child class or su4 class. +nheritance hel!s in reducing the
o3erall code si=e of the !rogram, which is an im!ortant conce!t in o47ect:oriented
!rogramming. +t is the !rocess 45 which one class inherits the !ro!erties of another Class.
D't' A/)tr'ctio:
Data A4straction increases the !ower of !rogramming language 45 creating user defined data
t5!es.
Data A4straction also re!resents the needed information in the !rogram without !resenting the
details.
11
$he conce!t of a4straction relates to the idea of hiding data that are not needed for !resentation. $he
main idea 4ehind data a4straction is to gi3e a clear se!aration 4etween !ro!erties of data t5!e and
the associated im!lementation details.
An A/)tr'ct D't' T=pe is defined as a data t5!e that is defined in terms of the o!erations that it
su!!orts and not in terms of its structure or im!lementation.
D't' Ec'p)u,'tio:
Data Enca!sulation com4ines data and functions into a single unit called class.
Data Enca!sulation ena4les the im!ortant conce!t of data hiding !ossi4le.
Enca!sulation is the !rocess of com4ining data and functions into a single unit called class. *sing the
method of enca!sulation, the !rogrammer cannot directl5 access the data. Data is onl5 accessi4le
through the functions !resent inside the class.
D't' %i-i. is the im!lementation details of a class that are hidden from the user.
class R5Class
L!u4licG
int sam!le%&M
int eJam!le%char ?se&
int endfunc%&M
......... ,,Other mem4er functions
!ri3ateG
int JM
float sIM
......... ,,Other data mem4ers
NM
+n the a4o3e eJam!le, the data mem4ers integer J, float sI and other data mem4ers and mem4er
functions sam!le%&,eJam!le%char? se&,endfunc%& and other mem4er functions are 4undled and !ut
inside a single autonomous entit5 called class R5Class. $his eJem!lifies the conce!t of
Enca!sulation.
Enca!sulation alone is a !owerful feature that leads to information hiding, a4stract data t5!e and
friend functions.
Po,=morp%i)m:
Po,= means man5 and morp%) mean form, so !ol5mor!hism means one name multi!le form. $here
are two t5!es of !ol5mor!hismG com!ile time and run time !ol5mor!hism. ol5mor!hism allows
routines to use 3aria4les of different t5!es at different times. An o!erator or function can 4e gi3en
different meanings or functions.
ol5mor!hism refers to a single function or multi:functioning o!erator !erforming in different wa5s.
O(er,o'-i.:
O3erloading is one t5!e of ol5mor!hism.
+t allows an o47ect to ha3e different meanings, de!ending on its conteJt.
Then an eJiting o!erator or function 4egins to o!erate on new data t5!e, or class, it is
understood to 4e o3erloaded.
Reu)'/i,it=:
$his term refers to the a4ilit5 for multi!le !rogrammers to use the same written and de4ugged
eJisting class of data.
$he !rogrammer can incor!orate new features to the eJisting class, further de3elo!ing the
a!!lication and allowing users to achie3e increased !erformance. .
OO4 Ter1 Definition
Rethod Same as function, 4ut the t5!ical OO notation is used for the call, i.e.
f%J,5& is written J.f%5& where J is an o47ect of class that contains this f
12
method.
Send a message Call a function %method&
+nstantiate Allocate a class,struct o47ect %ie, instance& with new
Class A struct with 4oth data and functions
O47ect Remor5 allocated to a class,struct. Often allocated with new.
Rem4er A field or function is a mem4er of a class if itYs defined in that class
Constructor A mem4er function ha3ing same name as that of the class that
initiali=es data mem4ers of an o47ect at the time of creation of the
o47ect.
Destructor ;unction:li(e code that is called when an o47ect is deleted to free an5
resources %e.g. memor5& that is has !ointers to. +t is automaticall5
in3o(ed when o47ect goes out of sco!e.
+nheritance Deri3ing !ro!erties of !arent class to the child class.
ol5mor!hism Defining functions with the same name, 4ut different !arameters.
O3erload A function is o3erloaded if there is more than one definition. See
!ol5mor!hism.
Su4 class Same as child, deri3ed, or inherited class.
Su!er class Same as !arent or 4ase class.
Attri4ute Same as data mem4er or mem4er field
IMPLEME"TATIO" O; OOPS I" C55
*,a""e"
4$/,i! and 4ri(ate "e!tion"
All mem4er fields in a c,')) are !ri3ate 45 default %no code outside the class can reference them&,
whereas fields of a )truct are !u4lic, meaning that an5one can use them. ;or eJam!le,
struct roduct Lchar mfgZidO-PM ,, - char code for the manufacturer.
char !rodZidO0PM ,, 0:char code for the !roduct
int !riceM ,, !rice of the !roduct in dollars.
int It5ZonZhandM ,, Iuantit5 on hand in in3entor5
NM
Could 4e rewritten as a class li(e this
class roduct L
!u4licG
char mfgZidO-PM ,, - char code for the manufacturer.
char !rodZidO0PM ,, 0:char code for the !roduct
int !riceM ,, !rice of the !roduct in dollars.
int It5ZonZhandM ,, Iuantit5 on hand in in3entor5
NM
A class is an eJ!anded conce!t of a data structureG instead of holding onl5 data, it can hold 4oth data
and functions.
An object is an instantiation of a class. +n terms of 3aria4les, a class would 4e the t5!e, and an o47ect
would 4e the 3aria4le.
Classes are generall5 declared using the (e5word class, with the following formatG
class classZname L
accessZs!ecifierZ"G
mem4er"M
accessZs!ecifierZ)G
mem4er)M
...
N o47ectZnamesM
There classZname is a 3alid identifier for the class, o47ectZnames is an o!tional list of names for
o47ects of this class. $he 4od5 of the declaration can contain mem4ers that can either 4e data or
function declarations, and o!tionall5 access s!ecifiers.
13
All is 3er5 similar to the declaration on data structures, eJce!t that we can now include also functions
and mem4ers, 4ut also this new thing called access specifier. An access s!ecifier is one of the
following three (e5wordsG !ri3ate, !u4lic or !rotected. $hese s!ecifiers modif5 the access rights that
the mem4ers following them acIuireG
pri('te mem4ers of a class are accessi4le onl5 from within other mem4ers of the same class
or from their friends.
protecte- mem4ers are accessi4le from mem4ers of their same class and from their friends,
4ut also from mem4ers of their deri3ed classes.
;inall5, pu/,ic mem4ers are accessi4le from an5where where the o47ect is 3isi4le.
65 default, all mem4ers of a class declared with the class (e5word ha3e pri('te access for all its
mem4ers. $herefore, an5 mem4er that is declared 4efore one other class s!ecifier automaticall5 has
!ri3ate access. ;or eJam!leG
class CRectangle L int J, 5M
!u4licG
3oid setZ3alues %int,int&M
int area %3oid&M
N rectM
Declares a class %i.e., a t5!e& called CRectangle and an o47ect %i.e., a 3aria4le& of this class
called rect. $his class contains four mem4ersG two data mem4ers of t5!e int %mem4er J and
mem4er 5& with !ri3ate access %4ecause !ri3ate is the default access le3el& and two mem4er
functions with !u4lic accessG setZ3alues%& and area%&, of which for now we ha3e onl5 included their
declaration, not their definition.
Notice the difference 4etween the class name and the o47ect nameG +n the !re3ious
eJam!le, CRectangle was the class name %i.e., the t5!e&, whereas rect was an o47ect of t5!e
CRectangle. +t is the same relationshi! it and ' ha3e in the following declarationG
int aM where int is the t5!e name %the class& and a is the 3aria4le name %the o47ect&.
After the !re3ious declarations of CRectangle and rect, we can refer within the 4od5 of the !rogram to
an5 of the !u4lic mem4ers of the o47ect rect as if the5 were normal functions or normal 3aria4les, 7ust
45 !utting the o47ectYs name followed 45 a dot %.& and then the name of the mem4er. All 3er5 similar to
what we did with !lain data structures 4efore. ;or eJam!leG
rect.setZ3alues %.,-&M
m5area C rect.area%&M
$he onl5 mem4ers of rect that we cannot access from the 4od5 of our !rogram outside the class
are J and 5, since the5 ha3e !ri3ate access and the5 can onl5 4e referred from within other mem4ers
of that same class.
Here is the com!lete eJam!le of class CRectangleG
class CRectangle Lint J, 5M
!u4licG
3oid setZ3alues %int,int&M
int area %& Lreturn %J?5&MN
NM
3oid CRectangleGGsetZ3alues %int a, int 4& LJ C aM 5 C 4MN
int main %& LCRectangle rectM
rect.setZ3alues %.,-&M
cout AA [areaG [ AA rect.area%&M
return /MN
$he most im!ortant new thing in this code is the o!erator of sco!e %::, two colons& included in the
definition of setZ3alues%&. +t is used to define a mem4er of a class from outside the class definition
itself.
\ou ma5 notice that the definition of the mem4er function area%& has 4een included directl5 within the
definition of the CRectangle class gi3en its eJtreme sim!licit5, where assetZ3alues%& has onl5 its
!rotot5!e declared within the class, 4ut its definition is outside it. +n this outside declaration, we must
use the o!erator of sco!e %GG& to s!ecif5 that we are defining a function that is a mem4er of the
14
class CRectangle and not a regular glo4al function.
$he sco!e resolution o!erator %::& s!ecifies the class to which the mem4er 4eing declared 4elongs,
granting eJactl5 the same sco!e !ro!erties as if this function definition was directl5 included within the
class definition. ;or eJam!le, in the function setZ3alues%& of the !re3ious code, we ha3e 4een a4le to
use the 3aria4les J and 5, which are !ri3ate mem4ers of classCRectangle, which means the5 are onl5
accessi4le from other mem4ers of their class.
$he onl5 difference 4etween defining a class mem4er function com!letel5 within its class or to include
onl5 the !rotot5!e and later its definition, is that in the first case the function will automaticall5 4e
considered an inline mem4er function 45 the com!iler, while in the second it will 4e a normal %not:
inline& class mem4er function, which in fact su!!oses no difference in 4eha3ior.
Rem4ers J and 5 ha3e !ri3ate access %remem4er that if nothing else is said, all mem4ers of a class
defined with (e5word class ha3e !ri3ate access&. 65 declaring them !ri3ate we den5 access to them
from an5where outside the class. $his ma(es sense, since we ha3e alread5 defined a mem4er
function to set 3alues for those mem4ers within the o47ectG the mem4er function setZ3alues%&.
$herefore, the rest of the !rogram does not need to ha3e direct access to them. erha!s in a so
sim!le eJam!le as this, it is difficult to see an5 utilit5 in !rotecting those two 3aria4les, 4ut in greater
!ro7ects it ma5 4e 3er5 im!ortant that 3alues cannot 4e modified in an uneJ!ected wa5 %uneJ!ected
from the !oint of 3iew of the o47ect&.
One of the greater ad3antages of a class is that, as an5 other t5!e, we can declare se3eral o47ects of
it. ;or eJam!le, following with the !re3ious eJam!le of class CRectangle, we could ha3e declared the
o47ect rect4 in addition to the o47ect rectG
class CRectangle L int J, 5M
!u4licG
3oid setZ3alues %int,int&M
int area %& Lreturn %J?5&MN
NM
3oid CRectangleGGsetZ3alues %int a, int 4& L J C aM 5 C 4MN
int main %& L CRectangle rect, rect4M
rect.setZ3alues %.,-&M
rect4.setZ3alues %W,X&M
cout AA [rect areaG [ AA rect.area%& AA endlM
cout AA [rect4 areaG [ AA rect4.area%& AA endlM
return /MN
+n this concrete case, the class %t5!e of the o47ects& to which we are tal(ing a4out is CRectangle, of
which there are two instances or o47ectsG rect and rect4. Each one of them has its own mem4er
3aria4les and mem4er functions.
Notice that the call to rect.area%& does not gi3e the same result as the call to rect4.area%&. $his is
4ecause each o47ect of class CRectangle has its own 3aria4les J and 5, as the5, in some wa5, ha3e
also their own function mem4ers setZ3alue%& and area%& that each uses its o47ectYs own 3aria4les to
o!erate.
$hat is the 4asic conce!t of object-oriented programmingG Data and functions are 4oth mem4ers of
the o47ect. Te no longer use sets of glo4al 3aria4les that we !ass from one function to another as
!arameters, 4ut instead we handle o47ects that ha3e their own data and functions em4edded as
mem4ers. Notice that we ha3e not had to gi3e an5 !arameters in an5 of the calls
to rect.area or rect4.area. $hose mem4er functions directl5 used the data mem4ers of their
res!ecti3eo47ects rect and rect4.
Co)tructor) '- De)tructor)
Constructors and destructors are s!ecial mem4er functions of classes that are used to
construct and destro5 class o47ects. Construction ma5 in3ol3e memor5 allocation and
initiali=ation for o47ects.
Destruction ma5 in3ol3e cleanu! and deallocation of memor5 for o47ects.
T%e &o,,o4i. re)trictio) 'pp,= to co)tructor) '- -e)tructor)G
15
Constructors and destructors do not ha3e return t5!e, not e3en 3oid nor can the5 return
3alues.
References and !ointers cannot 4e used on constructors and destructors 4ecause their
addresses cannot 4e ta(en.
Constructors cannot 4e declared with the (e5word 3irtual.
Constructors and destructors cannot 4e declared static, const, or 3olatile.
*nions cannot contain class o47ects that ha3e constructors or destructors.
$he com!iler automaticall5 calls constructors when defining class o47ects and calls destructors
when class o47ects go out of sco!e.
A constructor does not allocate memor5 for the class o47ect its t%i) !ointer refers to, 4ut ma5
allocate storage for more o47ects than its class o47ect refers to. +f memor5 allocation is
reIuired for o47ects, constructors can eJ!licitl5 call the new o!erator. During cleanu!, a
destructor ma5 release o47ects allocated 45 the corres!onding constructor. $o release o47ects,
use the delete o!erator.
Deri3ed classes do not inherit constructors or destructors from their 4ase classes, 4ut the5 do
call the constructor and destructor of 4ase classes.
Constructors are also called when local or tem!orar5 class o47ects are created, and
destructors are called when local or tem!orar5 o47ects go out of sco!e.
Te can call mem4er functions from constructors or destructors.
Constructor is a mem4er function with the same name as its class.
;or eJam!leG
class ] L
!u4licG
]%&M ,, constructor for class ]
NM
Destructors are usuall5 used to deallocate memor5 and do other cleanu! for a class o47ect
and its class mem4ers when the o47ect is destro5ed.
A destructor is called for a class o47ect when that o47ect !asses out of sco!e or is eJ!licitl5
deleted.
A destructor is a mem4er function with the same name as its class !refiJed 45 a ^ %tilde&.
;or eJam!leG
class ] L !u4licG
]%&M ,, Constructor for class ]
^]%&M ,, Destructor for class ]
NM
De&'u,t Co)tructor) '- De)tructor)
+f 5ou donYt declare a constructor or a destructor, the com!iler ma(es one for 5ou. $he default
constructor and destructor ta(e no arguments and do nothing.
That good is a constructor that does nothingF +n !art, it is a matter of form. All o47ects must 4e
constructed and destructed, and these do:nothing functions are called at the right time.
U)i. co)tructor) '- -e)tructor)+
,, Demonstrates declaration of a constructors and
,, destructor for the Cat class
Sinclude Aiostream.hB ,, for cout
class Cat ,, 4egin declaration of the class
L !u4licG ,, 4egin !u4lic section
C'tAit iiti',A.e&M ,, constructor
NC'tABI ,, destructor
int #etAge%&M ,, accessor function
3oid SetAge%int age&M ,, accessor function
3oid Reow%&M
!ri3ateG ,, 4egin !ri3ate section
int itsAgeM ,, mem4er 3aria4le
NM
CatGGCat%int initialAge& ,, constructor definition of Cat,
L itsAge C initialAgeM N
16
CatGG^Cat%& L N ,, destro5 the o47ect of cat when it is no longer referred.
int CatGG#etAge%&
L return itsAgeMN
3oid CatGGSetAge%int age&
L itsAge C ageMN ,, set mem4er 3aria4le its age to 3alue !assed in 45 !arameter ageN
3oid CatGGReow%&
L cout AA [Reow.Vn[MN
int main%&
L Cat ;ris(5%W&M
;ris(5.Reow%&M
cout AA [;ris(5 is a cat who is [ M
cout AA ;ris(5.#etAge%& AA [ 5ears old.Vn[M
;ris(5.Reow%&M
;ris(5.SetAge%9&M
cout AA [Now ;ris(5 is [ M
cout AA ;ris(5.#etAge%& AA [ 5ears old.Vn[M
return /M
N
Out!utG Reow.
;ris(5 is a cat who is W 5ears old.
Reow.
Now ;ris(5 is 9 5ears old.
Cop= Co)tructor
A co!5 constructor is a s!ecial constructor in the C11 !rogramming language used to create a
new o47ect as a co!5 of an eJisting o47ect.
Normall5 the com!iler automaticall5 creates a co!5 constructor for each class %(nown as a
default co!5 constructor& 4ut for s!ecial cases the !rogrammer creates the co!5 constructor,
(nown as a user:defined co!5 constructor. +n such cases, the com!iler does not create one.
Co!5ing of o47ects is achie3ed 45 the use of a co!5 constructor and a assignment o!erator. A
co!5 constructor has as its first !arameter a reference to its own class t5!e. +t can ha3e more
arguments, 4ut the rest must ha3e default 3alues associated with them. $he following would
4e 3alid co!5 constructors for class ]G
]%const ]2 co!5;romRe&M
]%]2 co!5;romRe&M
]%const ]2 co!5;romRe, int C "/&M
]%const ]2 co!5;romRe, dou4le C "./, int C -/&M
T%e &o,,o4i. c')e) m'= re)u,t i ' c',, to ' cop= co)tructor:
Then an o47ect is returned 45 3alue
Then an o47ect is !assed %to a function& 45 3alue as an argument
Then an o47ect is thrown
Then an o47ect is caught
Then an o47ect is !laced in a 4race:enclosed initiali=er list
An o47ect can 4e assigned 3alue using one of the two techniIuesG
EJ!licit assignment in an eJ!ression
+nitiali=ation
EGp,icit '))i.met i ' eGpre))io
O47ect AM
O47ect 6M
A C 6M ,, translates as O47ectGGo!eratorC%const O47ect2&, thus A.o!eratorC%6& is called
,, %in3o(e sim!le co!5, not co!5 constructorD&
Iiti',iH'tio
An o47ect can 4e initiali=ed 45 an5 one of the following wa5s.
a. T%rou.% -ec,'r'tio
O47ect 6 C AM ,, translates as O47ectGGO47ect%const O47ect2& %in3o(e co!5 constructor&
/+ T%rou.% &uctio 'r.umet)
t5!e function %O47ect a&M
17
c+ T%rou.% &uctio retur (',ue
O47ect a C function%&M
$he co!5 constructor is used onl5 for initiali=ations, and does not a!!l5 to assignments where the
assignment o!erator is used instead.
$he im!licit co!5 constructor of a class calls 4ase co!5 constructors and co!ies its mem4ers 45
means a!!ro!riate to their t5!e. +f it is a class t5!e, the co!5 constructor is called. 65 using a user:
defined co!5 constructor the !rogrammer can define the 4eha3ior to 4e !erformed when an o47ect is
co!ied.
Examples
$hese eJam!les illustrate how co!5 constructors wor( and wh5 the5 are reIuired sometimes.
Imp,icit cop= co)tructor
'et us consider the following eJam!le.
,,co!5 constructor
Sinclude AiostreamB
class erson
L !u4licG
int ageM
erson%int a& LageCaMN
NM
int main%&
Lerson timm5%"/&M
erson sall5%"W&M
erson timm5Zclone C timm5M
cout AA timm5.age AA [ [ AA sall5.age AA [ [ AA timm5Zclone.age AA endlM
timm5.age C ).M
cout AA timm5.age AA [ [ AA sall5.age AA [ [ AA timm5Zclone.age AA endlM
return /M N
Out!ut
"/ "W "/
). "W "/
As eJ!ected, timmy has 4een co!ied to the new o47ect, timmy_clone. Thile timmy's age was
changed, timmy_clone's age remained the same. $his is 4ecause the5 are totall5 different o47ects.
$he com!iler has generated a co!5 constructor for us, and it could 4e written li(e thisG
erson% erson2 co!5&
L ageCco!5.ageM N
I"<ERITA"CE
+nheritance is the !rocess 45 which new classes called derived classes are created from
eJisting classes called base classes.
$he deri3ed classes ha3e all the features of the 4ase class and the !rogrammer can choose to
add new features s!ecific to the newl5 created deri3ed class.
;e'ture) or A-('t'.e) o& I%erit'ce:
Reusability:
+nheritance hel!s the code to 4e reused in man5 situations.
$he 4ase class is defined and once it is com!iled, it need not 4e rewor(ed.
*sing the conce!t of inheritance, the !rogrammer can create as man5 deri3ed classes from
the 4ase class as needed while adding s!ecific features to each deri3ed class as needed+
Saves Time and Effort:
$he a4o3e conce!t of reusa4ilit5 achie3ed 45 inheritance sa3es the !rogrammer time and effort. $he
main code written can 4e reused in 3arious situations as needed.
In!rea"e" 4rogra1 Str$!t$re )hi!h re"$,t" in greater re,ia/i,it?.
#eneral ;ormat for im!lementing the conce!t of +nheritanceG
class derived_classname: access specifier baseclassname
;or eJam!le, if the base class is MyClass and the deri3ed class is sam!le it is s!ecified asG
class sample: public MyClass
18
$he a4o3e ma(es sam!le ha3e access to 4oth public and protected 3aria4les of 4ase class MyClass.
Remi-er '/out pu/,icE pri('te '- protecte- 'cce)) )peci&ier):
". +f a mem4er or 3aria4les defined in a class is !ri3ate, then the5 are accessi4le 45 mem4ers of
the same class onl5 and cannot 4e accessed from outside the class.
). u4lic mem4ers and 3aria4les are accessi4le from outside the class.
.. rotected access s!ecifier is a stage 4etween !ri3ate and !u4lic. +f a mem4er functions or
3aria4les defined in a class are !rotected, then the5 cannot 4e accessed from outside the class
4ut can 4e accessed from the deri3ed class.
+nheritance EJam!leG
c,')) M=C,'))
L pu/,ic:
R5Class%3oid& L JC/M N
3oid f%int n"&
L JC n"?WMN
3oid out!ut%3oid& L coutAAJM N
pri('teG
int JM
NM
class sam!leG !u4lic R5Class
L pu/,ic:
sam!le%3oid& L s"C/M N
3oid f"%int n"&
L s"Cn"?"/MN
3oid out!ut%3oid&
L R5ClassGGout!ut%&M cout AA s"M N
!ri3ateG
int s"M
NM
int main%3oid&
Lsam!le sM
s.f%"/&M
s.out!ut%&M
s.f"%)/&M
s.out!ut%&M
N
$he out!ut of the a4o3e !rogram is
W/
)//
T=pe) o& I%erit'ce
1+ Si.,e c,')) I%erit'ce:
Single inheritance is the one where 5ou ha3e a single 4ase class and a single deri3ed class.
it is a 6ase class %su!er&

it is a su4 class %deri3ed&
2+ Mu,ti,e(e, I%erit'ce:
+n Rulti le3el inheritance, a class inherits its !ro!erties from another deri3ed class.
it is a 6ase class %su!er& of 6

it is a su4 class %deri3ed& of A
and 4ase class of class C
19
Class Employee
Class Manager
Class
Class !

deri3ed class%su4& of class 6
0+ Mu,tip,e I%erit'ce):
+n Rulti!le inheritances, a deri3ed class inherits from multi!le 4ase classes. +t has !ro!erties
of 4oth the 4ase classes.
ase class
"eri#e$ class
2+ <ier'rc%ic', I%erit'ce:
+n hierarchial +nheritance, itYs li(e an in3erted tree. So multi!le classes inherit from a single
4ase class. +tYs Iuite analogous to the ;ile s5stem in a uniJ 4ased s5stem.
<a"e !,a"" @"$erA
%ub Class& $eri#e$'
7+ <=/ri- I%erit'ce:
+n this t5!e of inheritance, we can ha3e miJture of num4er of inheritances 4ut this can
generate an error of using same name function from no of classes, which will 4other the
com!iler to how to use the functions.
$herefore, it will generate errors in the !rogram. $his has (nown as am4iguit5 or du!licit5.
Am4iguit5 !ro4lem can 4e sol3ed 45 using (irtu', /')e c,'))e)
2 M'r*) 6ue)tio
Pr'ctice 1 G: Answer the Iuestions after going through the following class.
class EJam
Lchar Su47ectO)/P M
int Rar(s M
!u4lic G
EJam%& ,, ;unction "
Lstrc!5%Su47ect, UCom!uterQ & M Rar(s C / MN
EJam%char O P& ,, ;unction )
Lstrc!5%Su47ect, & M
Rar(sC/ M
N
EJam%int R& ,, ;unction .
Lstrc!5%Su47ect, UCom!uterQ& M Rar(s C R MN
EJam%char O P, int R& ,, ;unction -
Lstrc!5%Su47ect, & M Rar(s C R MN
NM
a& Thich feature of the O47ect Oriented rogramming is demonstrated using ;unction ",
;unction), ;unction . and ;unction - in the a4o3e class EJamF
AnsG: ;unction O3erloading %Constructor o3erloading&
4& Trite statements in C11 that would eJecute ;unction . and ;unction - of class EJam.
20
Class C
*,a"" A
*,a"" <
*,a"" *
Class ! Class
Class C
Class !
Class
Class "
Class C
Class "
A)G: EJam a%"/&M and EJam 4%UCom!Q, "/&M
Pr'ctice 2G Consider the following declaration G
class welcome
L!u4licG
welcome %int J, char ch&M ,, constructor with !arameter
welcome%&M ,, constructor without !arameter
3oid com!ute%&M
!ri3ateG
int JM char chM
NM
which of the following are 3alid statements
welcome o47 %.., _a8<&M
welcome o47"%W/, _8<&M
welcome o47.%&M
o47"C welcome %-W, _$<&M
o47.C welcomeM
A). Valid and in3alid statements are
welcome o47 %.., _a8<&M (',i-
welcome o47"%W/, _8<&M (',i-
welcome o47.%&M i(',i-
o47"C welcome %-W, _$<&M (',i-
o47.C welcomeM i(',i-
!ccess public pro(ec(e$ )ri#a(e
members of the same class yes Yes Yes
members of derived classesyes Yes No
not members yes No No
Pr'ctice 0: That do 5ou mean 45 3isi4ilit5 modesF That is their effect on inheritanceF
Pr'ctice 2: EJ!lain different t5!es of inheritance.
2 M'r*) 6ue)tio)
Pr'ctice1 :- *on"ider the fo,,o)ing de!,aration" and an")er the >$e"tion" gi(en /e,o)E
class 3ehicle
Lint wheelsM
!rotectedG
int !assengerM
!u4licG
3oid in!utdata% int, int&M
3oid out!utdata%&MNM
class hea353ehicleG !rotected 3ehicle
Lint diesel!etrolM
!rotectedG
int loadM
!u4licG
3oid readdata% int, int&M
21
3oid writedata%&MNM
class 4usG!ri3ate hea353ehicle
Lchar mar(sO)/PM
!u4licG
3oid fetchdata%char&M
3oid dis!la5data%&MNM
%i& Name the class and deri3ed class of the class %e'(=(e%ic,e+
%ii& Name the data mem4ers that can 4e accessed from function -i)p,'=-'t'AB
%iii& Name the data mem4ers that can 4e accessed 45 an o47ect of /u) c,'))
%i3& +s the mem4er function out!utdata%& accessi4le to the o47ects of %e'(=(e%ic,e c,'))+
A)
%i& 6ase class C 3ehicle and deri3ed class C 4us
%ii& $he data mem4ers !assenger, load, ma(e are a3aila4le to function dis!la5 data
%iii& No data mem4ers can 4e accessed 45 the o47ect of 4us calss.
%i3& No mem4er functions out!utdata %& is not accessi4le to the o47ects of hea35 3ehicle
class.
Pr'ctice2 :- Co)i-er t%e &o,,o4i. -ec,'r'tio) '- ')4er t%e Due)tio) .i(e
/e,o4:
Sinclude Aiostream.hB
class 4oo(
Lchar titleO)/PM
char authorO)/PM
int noof !agesM
!u4licG
3oid read%&M
3oid show%&MNM
class teJt4oo(G !ri3ate teJt4oo(
Lint noofcha!ters, noofassignmentsM
!rotectedG
int standardM
3oid readteJt4oo(%&M
3oid showteJt4oo(%&MNM
class !h5sics4oo(G !u4lic teJt4oo(
Lchar to!icO)/PM
!u4licG
3oid read!h5sics4oo(%&M
3oid show!h5sics4oo(%&MNM
%i& Name the mem4ers, which can 4e accessed from the mem4er functions of class
!h5sics4oo(.
%ii& Name the mem4ers, which can 4e accessed 45 an o47ect of Class teJt4oo(.
%iii& Name the mem4ers, which can 4e accessed 45 an o47ect of Class !h5sics4oo(.
%i3& That will 4e the si=e of an o47ect %in 45tes& of class !h5sics4oo(.
A)
AiB standard , readteJt4oo(%&,showteJt4oo(%& and to!icM
AiiB readteJt4oo(%& and showteJt4oo(%&
AiiiB read!h5sics4oo(%&, show!h5sics4oo(%&, readteJt4oo(%& and showteJt4oo(%&
Ai(B $he si=e of o47ect of !h5sics4oo(C si=e of 4oo( 1 si=e of $eJt4oo( 1 si=e of
!h5sics4oo(.
C -)1X1)/ C X0 45tes
Pr'ctice0 : An")er the >$e"tion" @iA to @i(A /a"ed on the fo,,o)ingE
Class C*S$ORER
L int CustZnoM
char CustZNameO)/PM
!rotectedG
3oid Register%&M
22
!u4licG
C*S$ORER% &M
3oid Status% &MNM
class SA'ESRAN
L int SalesmanZnoM
char SalesmanZNameO)/PM
!rotectedG
float Salar5M
!u4licG
SA'ESRAN% &M
3oid Enter% &M
3oid Show% &MNM
class SHO G !ri3ate C*S$ORER, !u4lic SA'ESRAN
L char VoucherZNoO"/PM
char SalesZDateO0M
!u4lic G
SHO% &M
3oid SalesZEntr5% &M
3oid SalesZDetail% &MNM
%i& Trite the names of data mem4ers, which are accessi4le from o47ect 4elonging
to class C*S$ORER.
%ii& Trite the names of all the mem4er functions which are accessi4le from o47ect
4elonging to class SA'ESRAN.
%iii& Trite the names of all the mem4ers which are accessi4le from mem4er
functions of class SHO.
%i3& How man5 45tes will 4e reIuired 45 an o47ect 4elonging to class SHOF
Pr'ctice 2: Define a class <OTEL in C11 with the following descri!tionG
ri3ate Rem4ers
Rno ,,Data Rem4er to store Room No
Name ,,Data Rem4er to store customer Name
$ariff ,,Data Rem4er to store !er da5 charge
NOD ,,Data Rem4er to store Num4er of da5s
CA'C ,,A function to calculate and return amount as NOD?$ariff and if the
3alue of NOD?$ariff is more than "//// then as "./W?NOD?$ariff
u4lic Rem4ersG
Chec(in% & ,,A function to enter the content RNo,Name, $ariff and NOD
Chec(out%& ,,A function to dis!la5 Rno, Name, $ariff, NOD
and Amount %Amount to 4e dis!la5ed 45 calling function CA'C% &
So,utio
SincludeAiostream.hB
class HO$E'
L unsigned int RnoM
char NameO)WPM
unsigned int $ariffM
unsigned int NODM
int CA'C%&
L int JM
JCNOD?$ariffM
if% JB"////&
return%"./W?NOD?$ariff&M
else
return%NOD?$ariff&M
N
!u4licG
3oid Chec(in%&
23
LcinBBRnoBBNameBB$ariffBBNODMN
3oid Chec(out%&
LcoutAARnoAANameAA$ariffAANODAACA'C%&MN
NM
3oid main%&
LHO$E' s"M
s".Chec(in%&M
s".Chec(out%&M
N
24
DATA ;ILE <A"DLI"$ I" C55
!e= Poit):
* $eJt fileG A teJt file stores information in reada4le and !rinta4le form. Each line of teJt is
terminated with an EOL %End of 'ine& character.
* 6inar5 fileG A 4inar5 file contains information in the non:reada4le form i.e. in the same format in
which it is held in memor5.
* StreamG A stream is a general term used to name flow of data. Different streams are used to
re!resent different (inds of data flow.
* $here are three file +,O classes used for file read , write o!erations.
o i&)tre'm - can 4e used for read o!erations.
o o&)tre'm - can 4e used for write o!erations.
o &)tre'm - can 4e used for 4oth read 2 write o!erations.
* &)tre'm+%G
* $his header file includes the definitions for the stream classes ifstream, ofstream and fstream.
+n C11 &i,e iput output facilities im!lemented through fstream.h header file.
* +t contain !redefines set of o!eration for handling file related in!ut and out!ut, fstream class
ties a file to the !rogram for in!ut and out!ut o!eration.
* A file can 4e o!ened usingG
o 65 the constructor of the stream. $his method is !referred when single file is used with
the stream.
o 65 the o!en%& function of the stream.
* ;i,e mo-e):
* io)::out +t creates file in out!ut mode and allows writing into the file.
* io)::i +t creates file in in!ut mode and !ermit reading from the file.
* io)::'pp $o retain the !re3ious contents of the file and to a!!end to the
end of eJisting file.
* io)::'te $o !lace the file !ointer at the end of the file, 4ut 5ou can write
data an5 where in the file.
* io)::truc +t truncates the eJisting file %em!ties the file&.
* io)::ocre'te +f file does not eJist this file mode ensures that no file is
created and o!en%& fails.
* io)::orep,'ce +f file does not eJist, a new file gets created 4ut if the file
alread5 eJists, the o!en%& fails.
* io)::/i'r= O O!ens a file in 4inar5 mode.
eo&AB: $his function determines the end:of:file 45 returning true%non:=ero& for end of file otherwise
returning false%=ero&.
opeAB:If 5ou want to manage multi!le file with same stream use o!en%&.
Stre'mCo/:ect+opeAP;i,e'meQEA;i,emo-eBBI
e.g., fstream fioM
&io+opeAP/oo*+-'tQE io)::i R io)::out R io)::/i'r=BI
$he o!erator R is (nown as 4itwise:OR o!erator.
c,o)eAB: $his function terminates the connection 4etween the file and stream associated with it.
Stre'mCo/:ect+c,o)eABI
re'-AB: $he read%& function reads a fiJed num4er of 45tes from the s!ecified stream and !uts them in
the 4uffer.
Stre'mCo/:ect+re'-AAc%'r SB9 O/:ectE )iHeo&AO/:ectBBI
4riteAB: $he write%& function writes fiJed num4er of 45tes from a s!ecific memor5 location to the
s!ecified stream.
Stre'mCo/:ect+4riteAAc%'r SB9 O/:ectE )iHeo&AO/:ectBBI
NoteG
6oth functions ta(e two arguments.
* $he first is the address of 3aria4le, and the second is the length of that 3aria4le in 45tes. $he
address of 3aria4le must 4e t5!e cast to t5!e char?%!ointer to character t5!e&
* $he data written to a file using write% & can onl5 4e read accuratel5 using read% &.
25
&i,e poiter: the file !ointer indicates the !osition in the file at which the neJt in!ut,out!ut is to occur.
)ee*.AB: +t !laces the file !ointer to the s!ecified !osition in a stream 4efore in!ut o!eration.
)ee*pAB: +t !laces the file !ointer to the s!ecified !osition in a stream 4efore out!ut o!eration.
te,,.AB: $his function returns the current !osition of the file !ointer in a stream.
te,,pAB: $his function returns the current !osition of the file !ointer in a stream.
Step) To Proce)) A ;i,e
Declare an o!47ect of the desired file stream class%ifstream, ofstream, or fstream&
O!en the reIuired file to 4e !rocessed using constructor or o!en function.
rocess the file.
Close the file stream using the o47ect of file stream.
EGerci)e: 1 M'r* 6ue)tio)
1+ O/)er(e t%e pro.r'm )e.met c're&u,,= '- ')4er t%e Due)tio t%'t &o,,o4):
class item
Lint itemZnoM
char itemZnameO)/PM
!u4licG
3oid enterDetail% &M
3oid showDetail% &M
int get+temZno% &L return itemZnoMN
NM
3oid modif5%item J, int 5 &
Lfstream ;ileM
;ile.o!en% Uitem.datQ, iosGG4inar5 E iosGGin E iosGGout& M
item iM
int recordsRead C /, found C /M
while%Dfound 22 ;ile.read%%char?& 2i , si=eof %i&&&
LrecordsRead11M
if%i. get+temZno% & CC 5 &
LZZZZZZZZZZZZZZZZZZZZZZZZZ,,Rissing statement
;ile.write%%char?& 2J , si=eof %J&&M found C "M
N
N
if%D found&
coutAAQRecord for modification does not eJistQ M
;ile.close%& M
N
+f the function modif5% & is su!!osed to modif5 a record in the file U item.dat U, which itemZno is
5, with the 3alues of item J !assed as argument, write the a!!ro!riate statement for the
missing statement using see(!% & or see(g% &, whiche3er is needed, in the a4o3e code that
would write the modified record at its !ro!er !lace.
+f the function modif5% & modifies a record in the file U item.dat U with the 3alues of item J
!assed as argument, write the a!!ro!riate !arameter for the missing !arameter in the a4o3e
code, so as to modif5 record at its !ro!er !lace.
A)+1+ ;i,e+)ee*pAArecor-)Re'--1BS)iHeo&AGBEio)::/e.BI or ;i,e+)ee*pA-1S)iHeo&AGBEio)::curBI
26
e.g
ifstream fin(book.txt);
char ch;
fin>>ch; //fin.get(ch);
cout<<ch;
Oen +ile /oo&%tFt,
read +ile an$
di",a? (-e c-arac(er
+rom (-e +ile.
e.g,:
ofstream fout(book.txt);
char ch;
cin>>ch;
fout<<ch; // fout.put(ch);
*reate and oen +ile
/oo&%tFt, .ri(e $a(a
in(o +ile +rom (-e
#ariable.
$eer', pro.r'm )tructure u)e- &or oper'ti. ' TeGt ;i,e
2 M'r*) 6ue)tio)
TeGt &i,e) i iput mo-e:
#rite ' &uctio i ' C55 to cout t%e um/er o& ,o4erc')e ',p%'/et) pre)et i ' teGt &i,e
P1OO!+tGtQ+
int countal!ha%&
L ifstream ;in%U6OOK.tJtQ&M
char chM
int countC/M
while%D;in.eof%&&
L;in.get%ch&M
if %islower%ch&&
count11M
N
;in.close%&M
return countM
N
;uctio to c',cu,'te t%e '(er'.e 4or- )iHe o& ' teGt &i,e+
3oid calculate%&
L fstream ;ileM
;ile.o!en%U4oo(.tJtQ,iosGGin&M
char aO)/PM
char chM
int iC/,sumC/,nC/M
while%;ile&
L ;i,e+.etAc%BI
aOiPCchM
i11M
if%%chCC< _& EE ch%CC _.<&EE%charCC<,<&%chCC<Vt<&EE%chCC<Vn<&
Li ::M sumCsum 1iM
iC/M N11M
N
N
coutAAQa3erage word si=e is UAA%sum,n&M
N
A))ume ' teGt &i,e Pcoor-i'te+tGtQ i) ',re'-= cre'te-+ U)i. t%i) &i,e cre'te ' C55 &uctio to
cout t%e um/er o& 4or-) %'(i. &ir)t c%'r'cter c'pit',+
int countword%&
L ifstream ;in%U6OOK.tJtQ&M
char chO)WPM
int countC/M
while%D;in.eof%&&
L;iTTc%I
if %isu!!er%chO/P&&
count11M
N
;in.close%&M
return countM
N
;uctio to cout um/er o& ,ie) &rom ' teGt &i,e) A' ,ie c' %'(e m'Gimum 80 c%'r'cter) or
e-) 't U+VB
27
int countword%&
L ifstream ;in%U6OOK.tJtQ&M
char chO9/PM
int countC/M
if %D;in&
L coutAAQError o!ening fileDQ M
eJit%/&M
N
while%"&
L;i+.et,ieAc%E80EU+VBI
if %;in.eof%&&
4rea(M
count11M
N
;in.close%&M
return countM
N
A pro.r'm to -i)p,'= t%e )iHe o& ' &i,e i /=te)+
SincludeAiostream.hB
SincludeAfstream.hB
SincludeA!rocess.hB
SincludeAconio.hB
int main%&
L
char filenameO".PM
clrscr%&M
coutAQEnter ;ilenameGVnQM
cin.getline%filename,".&M
ifstream infile%filename&M
if%Dinfile&
LcoutBBQsorr5 D Can not o!en UAAfilename AAQfileVnQM
eJit%:"&M
N
long noZ45tesC/M
char chM
infile.see(g%/,iosGGend&M
noZ45tesCinfile.tellg%&M
coutAAQ;ile Si=e isQAAnoZ45tesAAQ45tesVnQM
return /M
N
TeGt &i,e) i output mo-e:
C11 !rogram, which initiali=es a string 3aria4le to the content U$here is an island of o!!ortunit5 in the
middle of e3er5 difficult5.Q and out!ut the string one character at a time to the dis( file UO*$.$]$Q.
SincludeAfstream.hB
int main%&
L ofstream fout%UO*$.$]$Q&M
char ?str C Q$here is an island of o!!ortunit5 in the middle of e3er5 difficult5.Q M
int iC/M
if%Dfout&
L
coutAAQ;ile cannot 4e o!ened UM
return /M
N
while %strOiPDC<V/<&
LfoutAAstrOiPM
i11M
28
N
fout.close%&M
N
EGerci)e: A2 M'r*) 6ue)tio)B
". te a function in a C11 to count the num4er of u!!ercase al!ha4ets !resent in a teJt file
U6OOK.tJtQ
). Trite a function in a C11 to count the num4er of al!ha4ets !resent in a teJt file U6OOK.tJtQ
.. Trite a function in a C11 to count the num4er of digits !resent in a teJt file U6OOK.tJtQ
-. Trite a function in a C11 to count the num4er of white s!aces !resent in a teJt file U6OOK.tJtQ
W. Trite a function in a C11 to count the num4er of 3owels !resent in a teJt file U6OOK.tJtQ
X. Trite a function in a C11 to count the a3erage word si=e in a teJt file U6OOK.tJtQ
9. Trite a function in C11 to !rint the count of the word UtheQ as an inde!endent word in a teJt file
S$OR\.$]$.
;or eJam!le, if the content of the file S$OR\.$]$ is
$here was a mon(e5 in the =oo.
$he mon(e5 was 3er5 naught5.
$hen the out!ut of the !rogram should 4e ).
0. Assume a teJt file U$est.tJtQ is alread5 created. *sing this file, write a function to create three
files U'OTER.$]$Q which contains all the lowercase 3owels and U*ER.$]$Q which contains
all the u!!ercase 3owels and UD+#+$.$]$Q which contains all digits.
8. Create a function ;ile'owerShow%& in c11 which ta(e file name%teJt files&as a argument and
dis!la5 its all data into lower case
"/. Trite a function in C11 to count the num4er of lines !resent in a teJt file UStor5.tJtQ.
<OTS ;ILE <A"DLI"$
". Trite a function in a C11 to count the num4er of consonants !resent in a teJt file U$r5.tJtQ
). Trite a function in a C11 to count the num4er of u!!ercase 3owels !resent in a teJt file
UNo3el.tJtQ
.. Trite a function in a C11 to dis!la5 the sum of digits !resent in a teJt file U;ees.tJtQ.
-. Trite a function in a C11 to dis!la5 the !roduct of digits !resent in a teJt file UNum4er.tJtQ.
W. Trite a function in a C11 to find the largest digit !resent in a teJt file URar(s.tJtQ
0 M'r*) 6ue)tio)
$eer', pro.r'm )tructure u)e- &or oper'ti. ' 1i'r= ;i,e
Pro.r'm to re'- '- 4rite ' )tructure u)i. re'-AB '- 4riteAB u)i. /i'r= &i,e+
struct student
L
char nameO"WPM
float !ercentM
NM
3oid main%&
L
clrscr%&M
student sM
strc!5%s.name,QrashaQ&M
s.!ercentC08.W/M
ofstream foutM
fout.o!en%Usa3ingQ, iosGGout E io):: /i'r=&M
if%Dfout&
L
coutAAU;ile can<t 4e o!enedQM
4rea(M
N
29
fout.write%%char ?& 2s, si=eof%student&&M
fout.close%&M
ifstream finM
fin.o!en%Usa3ingQ,iosGGin E iosGG 4inar5&M
fin.read%%char ?& 2 s,si=eof%student&&M
coutAAs.nameM
coutAAUVn has the !ercentG QAAs.!ercentM
fin.close%&M
N
;uctio to '-- more o/:ect) /e,o.i. to c,')) MO!E 't t%e e- o& MO!ES+DAT &i,e+
class JOKELint 7o(eidM char t5!eOWP, 7o(edescO)//PM
!u4licG
3oid New7o(eentr5%&LcinBB7o(eidBBt5!eM cin.getline%7o(edesc,)//&MN
3oid show7o(e%&LcoutAA7o(eidAAQVtQAAt5!eAAendlAA7o(edescAAendlMN
NM
3oid a!!end%&
L
fstream afileM
afile.o!en%UJOKES.DA$Q, iosGG4inar5 E iosGGa!!&M
JOKE 7M
int n,iM
coutAAQHow man5 o47ects 5ou want to add GQM
cinBBnM
for %iC/MiAnMi11&
L
7.New7o(eentr5%&M
afile.write%%char ?&2 7, si=eof %JOKE&&M
N
afile.close%&M
N
$i(e ' /i'r= &i,e TELEP<O"E+DATE cot'ii. recor-) o& t%e &o,,o4i. c,')) Director=
class Director5
L char nameO)/P,addressO./P, areacodeOWP, !honeZnoO"WPM
!u4licG
3oid register%&M
3oid show%&M
int chec(code%char ACO P& L return strcm!%areacode, AC&MN
NM
#rite ' &uctio COPYA1CAB i C55E t%'t 4ou,- cop= ',, t%o)e recor-) %'(i. 're'co-e ')
P120Q &rom TELEP<O"E+DAT to TELE1AC!+DAT
CO\A6C%&
Lifstream ifile%U$E'EHONE.DA$Q,iosGGinEiosGG4inar5&M
+f%Difle& L coutAAQcould not o!en $E'EHONE.DA$QM eJit%:"&MN
else
Lofstream ofile%U$E'E6ACKQ,iosGGoutEiosGG4ainar5&M
if%Dofile& LcoutAAQcould not o!en $E'E6ACK.DA$QM eJit%:"&MN
else
LDirector5 dM
while%ifile.read%%char ?&2d, si=eof%d&&&
Lif%d.chec(code%U").Q&CC/&
Ofile.write%%char ?&2d,si=eof%d&&M
N
ifile.close%&M
ofile.close%&M
N
N
30
N
EGerci)e :0 M'r*) 6ue)tio
2+ #rite ' &uctio i C55 to )e'rc% &or ' 1oo*"o &rom ' /i'r= &i,e P1OO!+DATQE
'))umi. t%e /i'r= &i,e i) cot'ii. t%e o/:ect) o& t%e &o,,o4i. c,'))+
class 6OOK
L
int 6noM
char $itleO)/PM
!u4licG
int R6no%&Lreturn 6noMN
3oid Enter%&LcinBB6noMgets%$itle&MN
3oid Dis!la5%&LcoutAA6noAA$itleAAendlMN
NM
0+ #rite ' &uctio i C55 to '-- e4 o/:ect) 't t%e /ottom o& ' /i'r= &i,e
PSTUDE"T+DATQE '))umi. t%e /i'r= &i,e i) cot'ii. t%e o/:ect) o& t%e &o,,o4i.
c,'))+
class S$*D
Lint RnoM
char NameO)/PM
!u4licG
3oid Enter%&
LcinBBRnoMgets%Name&MN
3oid Dis!la5%&LcoutAARnoAANameAAendlMN
NM
POI"TERS
31
ointer is a memor5 3aria4le which can store address of an o47ect of s!ecified data t5!e. ;or
eJam!leG
SincludeAiostream.hB
3oid main%&
Lint JCWM
int ?aM,,here _a< is a !ointer to int which can store address of an o47ect of t5!e int
aC2JM,,address of J is assigned to !ointer _a<
coutAAQValue of J is G UAAJAAendlM
coutAAQAddress of J is G UAA2JAAendlM
coutAAQAddress stored in a is G UAAaAAendlM
coutAAQValues stored at memor5 location !ointed 45 a is G UAA?aAAendlM
coutAAQAddress of a is GUAA2aAAendlM
N
out!utG
Value of J is G W
Address of J is G XWW)-
Address stored in a is G XWW)-
Value stored at memor5 location !ointed 45 a is G W
Address of a is G XWW))
+n the a4o3e eJam!le the 9G gi3es address of J which in this case ha!!en to 4e XWW)- and
-ere&erece oper'tor S gi3es the 3alue stored in the memor5 location stored in !ointer 3aria4le _a< as
shown in the figure 4elow.
XWW)/ XWW)) XWW)- XWW)X XWW)0
XWW)- W
a J
Some propertie) o& poiter ('ri'/,e)
Since a !ointer 3aria4le stores address of another 3aria4le in memor5, we must understand
the wa5 C11 organi=es memor5 for its !rogram. Te can 3iew memor5 as linear seIuence of 45tes
where e3er5 45te has a uniIue address. ;or eJam!le, if 5ou ha3e X- K45tes memor5 i.e.
X-J"/)-CXWW.X 65tes, then the address num4er can 4e an5 3alue 4etween / and XWW.W as
illustrated in the figure gi3en 4elow.
Address Content
/
"
)
.
-
. .
. .
. .
XWW..
XWW.-
XWW.W
An eJecuta4le !rogram generated after 45 C11 com!iler is di3ided in three different segments. $hese
segments are Code Segment %holds instruction of the !rogram&, Data Segment %Hold glo4al and static
3aria4les of the !rogram& and Stac( Segment %holds local 3aria4les and return addresses used in the
!rogram&. Code Segment ma5 ha3e one 4loc( of X-K6 or multi!le 4loc(s of X-K6 each de!ending on
memor5 model used in the !rogram com!ilation. Data Segment and stac( segment shares common
4loc( of one 4loc( of X-K6 or multi!le 4loc(s of X-K6 each de!ending on memor5 model used in
!rogram com!ilation. $he free s!ace 4etween Data Segment and Code Segment is (nown as
Memor= <e'p or ;ree Store which is used in D5namic%Run $ime& memor5 allocation.
32
Poiter) '- Arr'=) i C55
$he conce!t of arra5 is 3er5 much 4ound to the one of
!ointer. +n fact, the identifier of an arra5 is eIui3alent to
the address of its first element, as a !ointer is eIui3alent to the
address of the first element that it !oints to, so in fact the5
are the same conce!t. ;or eJam!le, su!!osing these two
declarationsG
int 4O)/PM
int ? !M
$he following assignment o!eration would 4e 3alidG
! C 4M
After that, ! and 4 would 4e eIui3alent and would ha3e the
same !ro!erties. $he onl5 difference is that we could change
the 3alue of !ointer ! 45 another one, whereas 4 will alwa5s !oint
to the first of the )/ elements of t5!e int with which it was
defined. $herefore, unli(e !, which is an ordinar5 !ointer, 4
is an arra5, and an arra5 can 4e considered a constant !ointer.
$herefore, the following assignment statement would not 4e
3alidG
4C !M
6ecause 4 is an arra5, so it o!erates as a constant !ointer, and
we cannot assign 3alues to constants. Due to the
characteristics of 3aria4les, all eJ!ressions that include
!ointers in the following eJam!le are !erfectl5 3alidG

Sinclude Aiostream.hB
int main %&
Lint 4OWPM
int ? !M
! C 4M
!O/P C "/M ,, !O/P and ?! refers to the same 3alue i.e. 4O/P
!11M
?! C )/M
! C 24O)PM
?! C ./M
! C 4 1 .M
?! C -/M
! C 4M
?%!1-& C W/M ,,?%!1-& and !O-P refers to the same 3alue i.e. 4O-P
!C4M
for %int nC/M nAWM n11&
cout AA !OnP AA [, [M
coutAAendlM
for %int nC/M nAWM n11&
cout AA ?%41n& AA [, [M
return /M
N
Output i)
"/, )/, ./, -/, W/,
"/, )/, ./, -/, W/,
NoteG A !ointer 3aria4le can 4e used as an arra5 as in a4o3e eJam!le 4oth ?! and !O/P refers to the
same 3aria4le similarl5 4O/P and ?4 refers to the same 3aria4le. Again !O"P and ?%!1"& are same.
Address calculation method is same for 4oth !ointer and arra5. ;or eJam!le
int aOWPCL),-,X,9,8NM
Offset
Address Content

/
Code
Segment
S
e
g
m
e
n
t

n
o

0
"
"
)
.
-
.
.
.
XWW..
XWW.-
XWW.W
/
Data
Segment
S
e
g
m
e
n
t

n
o

0
)
"
)
.
. Hea! or
;ree
Store
.
.
.
Stac(
Segment
.
XWW.-
XWW.W

33
int ?ICaM
T%e '--re)) o& 'Ki-eGL i) c',cu,'te- ')
6ase address of a %i.e. address of aO/P& 1 indeJ ? si=eof %data t5!e of a&
for eJam!le if the 4ase address of a is "/// then address of aO.P is calculated as
2aO.P C"/// 1 . ? si=eof %int& C "/// 1 . ? )C"/// 1 X C "//X
Note that aO.P and ?2aO.P 4oth will gi3e the same 3alue i.e. 9
Similarl5 address of %I1.& is calculated as
Address stored in I 1 . ? si=eof %data t5!e of !ointer 4elongs to in this case int&
%I1.& C "/// 1 . ? si=eof %int& C "/// 1 . ? )C"//X
Arit%metic oper'tio o poiter)
Te can increment or decrement a !ointer 3aria4le for eJam!le
float aOWPCL../,W.X,X./,).W,W..NM
float ?!CaM
11!M
::!M
if the 4ase address of a is "/// then statement 11! will increment the address stored in !ointer
3aria4le 45 - 4ecause ! is a !ointer to float and si=e of a float o47ect is - 45te, since 11! is eIui3alent
to !C!1" and address of !1" is calculated as
Address stored in ! 1 " ? si=eof %float&C "/// 1 " ? - C "//-.
Te can add or su4tract an5 integer num4er to a !ointer 3aria4le 4ecause adding an integer 3alue to
an address ma(es another address e.g.
3oid main%&
Lint !O"/PCL0,X,-,),"NM
+nt ?IM
IC!M,,address of !O/P is assigned to I assuming !O/P is allocated at memor5 location "///
ICI1-M,,now I contains address of !O-P i.e. "///1-?si=eof%int&C"///1-?)C "//0
coutAA?IAAendlM
ICI:)M,,now I contains address of !O)P i.e. "//0:)?si=eof %int&C"//0:)?)C"//-
coutAA?IAAendlM
N
Out!ut is
"
-
A--itio o& t4o poiter ('ri'/,e) i) me'i.,e))+
Su/tr'ctio o& t4o poiter) i) me'i.&u, o,= i& /ot% poiter) cot'i t%e '--re)) o& -i&&eret
e,emet) o& t%e )'me 'rr'=
;or eJam!le
3oid main%&
Lint !O"/PCL",.,W,X,0,8NM
int ?aC!1",?4C!1-M
!C!1IM ,,error 4ecause sum of two addresses 5ields an illegal address
int JC4:aM ,,3alid
coutAAJM
N
Out!ut is
.
+n the a4o3e eJam!le if 4ase address of a is "/// then address of a would 4e "//) and address of 4
would 4e "//0 then 3alue of 4:a is calculated as
%Address stored in 4:address stored in a&,si=eof%data t5!e in this case int&
%"//0:"//)&,)C.

Mu,tip,ic'tio '- -i(i)io oper'tio o ' poiter ('ri'/,e i) ot ',,o4e-
Te cannot assign an5 integer 3alue other than / to a !ointer 3aria4le.
;or eJam!le
34
int ?!M
!CWM ,,not allowed
!C/M ,,allowed 4ecause / is a 3alue which can 4e assigned to a 3aria4le of an5 data t5!e
"u,, Poiter
A null !ointer is a regular !ointer of an5 !ointer t5!e which has a s!ecial 3alue that indicates that it is
not !ointing to an5 3alid reference or memor5 address. $his 3alue is the result of t5!e:casting the
integer 3alue =ero to an5 !ointer t5!e.
int ?IC/M ,, I has a N*'' !ointer 3alue
float ? !M
!CN*''M ,, ! has a N*'' %N*'' is defined as a constant whose 3alue is /& !ointer 3alue
"ote: 0 memor= '--re)) i) ' memor= ,oc'tio 4%ic% i) ot ',,oc'te- to '= ('ri'/,e o& t%e
pro.r'm+
(oi- poiter)
3oid !ointer is a s!ecial !ointer which can store address of an o47ect of an5 data t5!e. Since 3oid
!ointer do not contain the data t5!e information of the o47ect it is !ointing to we can not a!!l5
dereference o!erator ? to a 3oid !ointer without using eJ!licit t5!e casting.
3oid main%&
L
int JCWM
float 5C..WM
int ?!M
float ?IM
3oid ?rM
!C25M ,,error cannot con3ert float ? to int ?
IC2JM ,,error cannot con3ert int ? to float ?
!C2JM ,,3alid
coutAA?! AAendlM ,,3alid
IC25M ,,3alid
coutAA?IAAendl M ,,3alid
rC2JM ,,3alid
coutAA?rAAendlM ,,error !ointer to cannot 4e dereference without eJ!licit t5!e cast
coutAA?%int ?&rAAendlM ,, 3alid and dis!la5 the 3alues !ointed 45 r as int
rC25M ,,3alid
coutAA?%float ?&rAAendlM ,,3alid and dis!la5 the 3alues !ointed 45 r as float
N
"ote: Poiter to oe -'t' t=pe c'ot /e co(erte- to poiter to 'ot%er -'t' t=pe eGcept
poiter to (oi-
Arr'= o& poiter) '- poiter to 'rr'=
An arra5 of !ointer is sim!l5 an arra5 whose all elements are !ointers to the same data t5!e.
;or eJam!le
Void main%&
L
float JC..W,5C9.),=C8.9M
float ?4OWPM ,, here 4 is an arra5 of W !ointers to float
4O/PC2JM
4O"PC25M
4O)PC2=M
coutAA?4O/PAAQVtQAA?4O"PAAQVtQAA?4O)PAAendlM
coutAAsi=eof%4&AAsi=eof%4O/P&AAsi=eof%?4O/P& M
N
Out!ut is
..W 9.) 8.9
"/ ) -
35
+n the a4o3e eJam!le the eJ!ression si=eof%4& returns the num4er of 45tes allocated to 4 i.e. "/
4ecause arra5 4 contain W !ointers to float and si=e of each !ointer is ) 45tes. $he eJ!ression
si=eof%4O/P& returns the si=e of first !ointer of the arra5 4 i.e. )45tes. $he eJ!ression si=eof%?4O/P&
returns the si=e of the data t5!e the !ointer 4O/P !oints to which is float, so the eJ!ression returns - as
out!ut.
ointer to arra5 is a !ointer which !oints to an arra5 of for eJam!le
3oid main%&
L
float aO"/P, 4OWP,cO"/PM
float %?I&O"/PM ,,here I is a !ointer to arra5 of "/ floats
IC2aM ,,3alid
IC2cM ,,3alid
IC24M ,,error cannot con3ert float OWP? to float O"/P?
coutAAsi=eof%I&AAQVtQsi=eof%?I&
N
After remo3ing the incorrect statement IC24 from the !rogram the out!ut of the !rogram is
) -/
6ecause I is a !ointer and !ointer to an5 thing is and address which is of )45tes. 6ut si=eof%?I&
returns the si=e of an arra5 of "/ floats i.e. -/ 4ecause ?I !oints to an arra5 of "/ floats.
Poiter) to poiter)
C11 allows the use of !ointers that !oint to !ointers, that these, in its turn, !oint to data %or e3en to
other !ointers&. +n order to do that, we onl5 need to add an asteris( %?& for each le3el of reference in
their declarationsG
Void main%&
L
int JCWM
int ?aM ,, a is !ointer to int
int ??4M ,,4 is !ointer to !ointer to int
int ???cM ,,c is !ointer to !ointer to !ointer to int
aC2JM
4C2aM
cC24M
coutAAJAAQVtQAA?aAAQVtQAA??4AAQVtQAA???cAAendlM
N
Out!ut is
W W W W
Assuming 3aria4le J, a, 4, c are allocated randoml5 at memor5 location 8///, 0///, 9///, and X///
res!ecti3el5. $he 3alue of each 3aria4le is written inside each cellM under the cells are their res!ecti3e
addresses in memor5.
X/// 9/// 0/// 8///
9/// 0/// 8/// W
c has t5!e int??? and a 3alue of 9///
?c has t5!e int ?? and a 3alue of 0///
??c has t5!e int ? and a 3alue of 8///
???c has t5!e int and a 3alue of W
Poiter to co)t't '- co)t't poiter
it GW7E HW8 I
co)t it =W?I
co)t it SpI // %ere p i) poiter to co)t it
it S co)t DW9GI // %ere D i) co)t'tt poiter to it
it S co)t rI //error co)t't poiter mu)t /e iiti',iHe-
pW9GI // (',i- )ice it S c' /e co(erte- to co)t it S
36
SpW0I // error c'ot mo-i&= co)t o/:ect
SDW0I //(',i-
DW9HI //error
it S co)t )W9=I// error c'ot co(ert co)t it S to it S
co)t it S co)t /W 9GI //,e.',
co)t it S co)t cW9=I //,e.',
S/W2I//error c'ot mo-i&= co)t o/:ect
/WcI // error c'ot mo-i&= co)t o/:ect
St'tic memor= ',,oc'tio '- D='mic memor= ',,oc'tio
+f memor5 allocation is done at the time of com!ilation of the !rogram then memor5 allocation is
(nown as static memor5 allocation. ;or eJam!le memor5 allocation for 3aria4les and arra5s are done
at com!ilation of the !rogram as si=e of 3aria4le and arra5s are alread5 (nown at the time of
com!ilation of the !rogram.
D5namic memor5 allocation is the memor5 allocation reIuired during eJecution of the !rogram. ;or
eJam!le if the amount of memor5 needed is determined 45 the 3alue in!ut 45 the user at run time.
D5namic memor5 allocation using new o!erator
+nt ?aCnew int%W&M ,? new o!erator will create an int o47ect somewhere in memor5 hea!
and initiali=e the o47ect with 3alue W and returns address of the
o47ect to !ointer 3aria4le a ?,
int nM
cinBBnM
+nt ?4Cnew intOnPM ,?here new o!erator will create an arra5 of W int and return the 4ase
address of the allocated arra5 to !ointer 3aria4le 4 ?,
+f the new o!erator fails to allocate memor5 then it returns N*'' 3alue which indicates that the
reIuired memor5 is not a3aila4le for the reIuested instruction.
NoteG Te cannot create arra5 of 3aria4le length for eJam!le
+nt nM
CinBBnM
+nt aOnPM ,, error 4ecause we can use onl5 constant integral 3alue in arra5 declaration
const int !C.M
int aO!PM ,,legal
Oper'tor) -e,ete '- -e,eteK L
Since the necessit5 of d5namic memor5 is usuall5 limited to s!ecific moments within a !rogram, once
it is no longer needed it should 4e freed so that the memor5 4ecomes a3aila4le again for other
reIuests of d5namic memor5. $his is the !ur!ose of the o!erator delete, whose format isG
delete !ointerM ,,used to delete the memor5 allocated for single o47ect
delete !ointerOPM,,used to delete the memor5 allocated for arra5 of o47ects
$he 3alue !assed as argument to delete must 4e either a !ointer to a memor5 4loc( !re3iousl5
allocated with new, or a null !ointer %in the case of a null !ointer, delete !roduces no effect&.
3oid main%&
Lint nM
+nt ?IM
coutAAQenter no of integers reIuired GQM
cinBBnM
ICnew int OnPM
if%ICCN*''&
coutAAQmemor5 could not 4e allocatedQM
else
Lfor%int iC/MiAnMi11&
IOiPCi1"M
for%iC/MiAnMi11&
coutAAIOiPAAQ UM
delete IO PM
37
N
N
;or nC. and successful memor5 allocation the out!ut of the !rogram would 4e
" ) .
Otherwise on failure of new o!erator the message Umemor5 could not 4e allocatedQ would 4e the
out!ut.
+t is alwa5s a good ha4it to chec( the !ointer with N*'' 3alue to ma(e sure that memor5 is allocated
or not.
Memor= ,e'*)
+f d5namicall5 allocated memor5 has 4ecome unreacha4le %no !ointer 3aria4le is !ointing the
allocated memor5& then such situation is (nown as memor5 lea( 4ecause neither it can 4e accessed
nor it can 4e deleted from the memor5 hea! 45 gar4age collection. ;or eJam!le
3oid function"%&
L
int JCWM
int ?!M
!Cnew int O"//PM ,, d5namic memor5 allocation for an arra5 of "// integers
!C2JM
N
+n the a4o3e eJam!le the statement pWe4 it K100L assign the address of the d5namicall5 allocated
memor5 to ! and the neJt statement pW9G assign the address of J to ! therefore, after that the
d5namicall5 allocated memor5 4ecome unreacha4le which cause memor5 lea(.
Poiter to )tructure
struct em!
L
int em!noM
char nameO)/PM
float salM
NM
3oid main%&
L
em! e"CLW,QAn(it SinghQ, .////./NM
em! ?!C2e"M
coutAA!:Bem!noAAQVtQAA!:BnameAAQVtQAA!:BsalAAendlM
!:BsalC!:Bsal 1 !:Bsal?../,"//M
coutAA%?!&.em!noAAQVtQAA!:BnameAAQVtQAA%?!&.salM
N
Out!ut is
W An(it Singh .////./
W An(it Singh ./8//./
this !ointer
Then an o47ect of a class in3o(es a mem4er function then a s!ecial !ointer is im!licitl5 !assed to the
mem4er function which contains the address of the o47ect 45 which the function is 4eing called, this
s!ecial !ointer is (nown as this !ointer. ;or eJam!le
SincludeAiostream.hB
class A
Lint JM
!u4licG
3oid in!ut%&
L coutAAQenter a num4er GUM
cinBBJM
N
3oid out!ut%&
38
LcoutAAQaddress of the o47ect is GUAAthisAAendlM
coutAAthis:BJAAQVtQAAJAAendl M ,, 4oth this:BJ and J will gi3e the same 3alue i.e. 3alue of
N
3oid main%&
L
A a", a)M
a".in!ut%&M
a).in!ut%&M
a".out!ut%&M
a).out!ut%&M
N
Out!ut is
enter a num4er G W
enter a num4er G 0
address of the o47ect is G XWW)- ,,assuming that a" is allocated at memor5 location "///
W W
address of the o47ect is G XWW)) ,,assuming that a) is allocated at memor5 location 880
0 0
XWW)/ XWW)) XWW)- XWW)X XWW)0
0 W
a) a"
NoteG the this !ointer does not eJist outside the mem4er function, i.e. we cannot define or access this
!ointer in main function 4ecause this !ointer comes into eJistence onl5 when a mem4er function of a
class is in3o(ed 45 an o47ect of the class.
2 M'r*) 6ue)tio)
EGerci)e
1. #i3e the out!ut of the following !rogramG
3oid main%&
Lchar ?! C USchoolQM
char cM
c C 11 ?! 11M
coutAAcAAU, QAA!AAendlM
coutAA!AAU, QAA11?!: :AAU, QAA11?!11M
N
2. #i3e the out!ut of the following !rogramG
3oid main%&
Lint J O P C LW/, -/, ./, )/, "/NG
int ?!, ??I, ?tM
! C JM
t C J 1 "M
I C 2tM
cout AA ?! AAU, Q AA ??I AA U,Q AA ?t11M
N
0+ #i3e the out!ut of the following !rogram %Assume all necessar5 header files are included&G
3oid main% &
Lchar ? J C U$a7RahalQM
char cM
JCJ1. M
c C 11 ?J 11M
coutAAcAAQ, UM
coutAA ?JAAU, QAA: :?J11AA: :J M
N
39
2+ #i3e the out!ut of the following !rogram %Assume all necessar5 header files are included&G
3oid main% &
Lchar ?J C URa7asthanQM
char cM
c C %?%J1.&&11 M
coutAAcAAU, QAAJAAU, QAAsi=eof%J&AA U, QAAsi=eof%J1.&AA U, QAAstrlen%J1.&M
N
7+ That will 4e the out!ut of the !rogram %Assume all necessar5 header files are included&G
3oid !rint %char ? ! &
Lint iC/M
while%?!&
?!C?!11 1 i11M
coutAA[3alue is [AA!:i1)AAendlM
N
3oid main% &
Lchar ? J C [Rum4ai[M
!rint%J&M
coutAA[new 3alue is [AAJAAendlM
N
>+ + +dentif5 the errors if an5. Also gi3e the reason for errors.
SincludeAiostream.hB
3oid main%&
Lint 4C-M
const int i C)/M
const int ? !trC2iM
%?!tr&11M
!tr C27M
coutAA?!trM
N
9. +dentif5 errors on the following code segment
3oid main%&
Lfloat cO P CL ".),).),..),WX.)NM
float ?(,?gM
(CcM
gC(1-M
(C(?)M
gCg,)M
(C(1gM
cC(M
coutAAQ?(CQAA?(AAQ?gCQAA?gM
N
?+ That will 4e the out!ut of the !rogram %Assume all necessar5 header files are included&G
3oid main% &
Lint aO PCL-,0,),W,9,8,XN
int JCa1W,5Ca1.M
coutAA11?J: :AAQ,QAA11?J: :AAQ,QAA11?511AAQ,QAA11?5: :M
N
@+ That will 4e the out!ut of the !rogram %Assume all necessar5 header files are included&G
3oid main%&
Lint arrO P C L"), )., .-, -WNM
int ?!tr C arrM
int 3al C ?!tr M cout AA 3al AA endlM
3al C ?!tr11M cout AA 3al AA endlM
3al C ?!trM cout AA 3al AA endlM
40
3al C ?11!trM cout AA 3al AA endlM
3al C 11?!trM cout AA 3al AA endlM
N
0 M'r*) 6ue)tio)
1+#i3e out!ut of following code fragment assuming all necessar5 header files are includedG
3oid main%&
Lchar ?msg C UCom!uter ScienceQM
for %int i C /M i A strlen %msg&M i11&
if %islower%msgOiP&&
msgOiP C tou!!er %msgOiP&M
else
if %isu!!er%msgOiP&&
if% i @ ) DC /&
msgOiP C tolower %msgOi:"P&M
else
msgOi::PM
cout AA msg AA endlM
N
2+ That will 4e the out!ut of the !rogram %Assume all necessar5 header files are included&G
3oid main% &
Lclrscr% &M
int a C.)M
int ?!tr C 2aM
char ch C _A<M
char ?choC2chM
cho1CaM ,, it is sim!l5 adding the addresses.
?!tr 1 C chM
coutAA a AA UQ AAchAAendlM
N
.. That will 4e the out!ut of the !rogram %Assume all necessar5 header files are included&G
3oid main% &
Lchar ?aO PCLUDE'H+Q, UR*R6A+Q,QVARANSA+QNM
char ??!M
!CaM
coutAAsi=eof%a&AAQ, UAAsi=eof%aO/P&AAQ, UAAsi=eof%!&AAendlM
coutAA ! AA U, QAA11?!AAU, QAA?11!AAendlM
coutAA ??a AA U, Q AA?%a1"&AAU, Q AAaO)PAAendlM
N
41
D't' Structure
+n Com!uter Science, a -'t' )tructure is a !articular wa5 of storing and organi=ing data in a
com!uter so that it can 4e used efficientl5. Different (inds of data structures are suited to different
(inds of a!!lications, and some are highl5 s!eciali=ed to s!ecific tas(s.
$he data structure can 4e classified into following two t5!esG
Simp,e D't' Structure: $hese data structures are normall5 4uilt from !rimiti3e data t5!es li(e
integers, floats, characters. ;or eJam!le arra5s and structure.
Compou- D't' Structure: sim!le data structures can 4e com4ined in 3arious wa5s to form more
com!leJ structure called com!ound structures. 'in(ed 'ists, Stac(, `ueues and $rees are eJam!les
of com!ound data structure.
D't' Structure Arr'=)
Data structure arra5 is defined as linear seIuence of finite num4er of o47ects of same t5!e
with following set of o!erationG
Creating G defining an arra5 of reIuired si=e
+nsertionG addition of a new data element in the in the arra5
DeletionG remo3al of a data element from the arra5
SearchingG searching for the s!ecified data from the arra5
$ra3ersingG !rocessing all the data elements of the arra5
Sorting G arranging data elements of the arra5 in increasing or decreasing order
Rerging G com4ining elements of two similar t5!es of arra5s to form a new arra5 of same t5!e
+n C11 an arra5 can 4e defined as
Datat5!e arra5nameOsi=ePM
42
There si=e defines the maJimum num4er of elements can 4e hold in the arra5. ;or eJam!le
float 4O"/PM,,4 is an arra5 which can store maJimum "/ float 3alues
int cOWPM
Arr'= iiti',iH'tio
3oi- m'iAB
X
it /K10LWX0E7E8E?E@YI//
coutZZ/K2LZZe-,I
coutZZ/K7LZZe-,I
Y
Out!ut is
8
/
+n the a4o3e eJam!le the statement int 4O"/PCL.,W,9,0,8N assigns first W elements with the gi3en
3alues and the rest elements are initiali=ed with /. Since in C11 indeJ of an arra5 starts from / to
si=e:" so the eJ!ression 4O-P denotes the W
th
element of the arra5 which is 8 and 4OWP denotes X
th
element which is initiali=ed with /.
. W 9 0 8 / / / / /
4O/P 4O"P 4O)P 4O.P 4O-P 4OWP 4OXP 4O9P 4O0P 4O8P
Se'rc%i.
Te can use two different search algorithms for searching a s!ecific data from an arra5
'inear search algorithm
6inar5 search algorithm
Lie'r )e'rc% ',.orit%m
+n 'inear search, each element of the arra5 is com!ared with the gi3en item to 4e searched for. $his
method continues until the searched item is found or the last item is com!ared.
SincludeAiostream.hB
int linearZsearch%int aOP, int si=e, int item&
L
int iC/M
Thile%iAsi=e22 aOiPDCitem&
i11M
if%iAsi=e&
return iM,,returns the indeJ num4er of the item in the arra5
else
return :"M,,gi3en item is not !resent in the arra5 so it returns :" since :" is not a legal indeJ num4er
N
3oid main%&
L
int 4O0PCL),-,W,9,0,8,"),"WN,si=eC0M
int itemM
coutAAQenter a num4er to 4e searched forQM
cinBBitemM
int !ClinearZsearch%4, si=e, item&M ,,search item in the arra5 4
if%!CC:"&
coutAAitemAAQ is not !resent in the arra5QAAendlM
else
coutAAitem AAQ is !resent in the arra5 at indeJ no UAA!M
N
+n linear search algorithm, if the searched item is the first elements of the arra5 then the loo!
terminates after the first com!arison %4est case&, if the searched item is the last element of the arra5
then the loo! terminates after si=e time com!arison %worst case& and if the searched item is middle
element of the arra5 then the loo! terminates after si=e,) time com!arisons %a3erage case&. ;or large
si=e arra5 linear search not an efficient algorithm 4ut it can 4e used for unsorted arra5 also.
43
1i'r= )e'rc% ',.orit%m
6inar5 search algorithm is a!!lica4le for alread5 sorted arra5 onl5. +n this algorithm, to search for the
gi3en item from the sorted arra5 %in ascending order&, the item is com!ared with the middle element of
the arra5. +f the middle element is eIual to the item then indeJ of the middle element is returned,
otherwise, if item is less than the middle item then the item is !resent in first half segment of the arra5
%i.e. 4etween / to middle:"&, so the neJt iteration will continue for first half onl5, if the item is larger
than the middle element then the item is !resent in second half of the arra5 %i.e. 4etween middle1" to
si=e:"&, so the neJt iteration will continue for second half segment of the arra5 onl5. $he same
!rocess continues until either the item is found %search successful& or the segment is reduced to the
single element and still the item is not found %search unsuccessful&.
SincludeAiostream.hB
int 4inar5Zsearch%int aO P, int si=e, int item&
L
int firstC/,lastCsi=e:",middleM
while%firstAClast&
L
middleC%first1last&,)M
if%itemCCaOmiddleP&
return middleM ,, item is found
else if%itemA aOmiddleP&
lastCmiddle:"M ,,item is !resent in left side of the middle element
else
firstCmiddle1"M ,, item is !resent in right side of the middle element
N
return :"M ,,gi3en item is not !resent in the arra5, here, :" indicates unsuccessful search
N
3oid main%&
L
int 4O0PCL),-,W,9,0,8,"),"WN,si=eC0M
int itemM
coutAAQenter a num4er to 4e searched forQM
cinBBitemM
int !C4inar5Zsearch%4, si=e, item&M ,,search item in the arra5 4
if%!CC:"&
coutAAitemAAQ is not !resent in the arra5QAAendlM
else
coutAAitem AAQ is !resent in the arra5 at indeJ no UAA!M
N
'et us see how this algorithm wor( for itemC")
+nitiali=ing first C/ M lastCsi=e:"M where si=eC0
Iter'tio 1
AO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P
) - W 9 0 8 ") "W
first
middl
e last
;irstC/, lastC9
middleC%first1last&,)C%/19&,)C. ,, note integer di3ision ..W 4ecomes .
3alue of aOmiddleP i.e. aO.P is 9
9A") then firstC middle1" i.e. . 1 " C-
iter'tio 2
AO-P aOWP aOXP aO9P
0 8 ") "W
44
first
middl
e last
firstC-, lastC9
middleC%first1last&,)C%-19&,)CW
3alue of aOmiddleP i.e. aOWP is 8
8A") then firstCmiddle1"MW1"CX
iter'tio 0
aOXP aO9P
") "W
first
middl
e last
firstCX,lastC9
middleC%first1last&,) C %X19&,)CX
3alue of aOmiddleP i.e. aOXP is ") which is eIual to the 3alue of item 4eing search i.e.")
As a successful search the function 4inar5Zsearch%& will return to the main function with 3alue
X as indeJ of ") in the gi3en arra5. +n main function ! hold the return indeJ num4er.
Note that each iteration of the algorithm di3ides the gi3en arra5 in to two eIual segments and the onl5
one segment is com!ared for the search of the gi3en item in the neJt iteration. ;or a gi3en arra5 of
si=e NC )
n
elements, maJimum n num4er of iterations are reIuired to ma(e sure whether the gi3en
item is !resent in the gi3en arra5 or not, where as the linear reIuires maJimum )
n
num4er of iteration.
;or eJam!le, the num4er of iteration reIuired to search an item in the gi3en arra5 of "/// elements,
4inar5 search reIuires maJimum "/ %as "///)
"/
& iterations where as linear search reIuires
maJimum "/// iterations.
I)erti. ' e4 e,emet i ' 'rr'=
Te can insert a new element in an arra5 in two wa5s
+f the arra5 is unordered, the new element is inserted at the end of the arra5
+f the arra5 is sorted then the new element is added at a!!ro!riate !osition without altering the
order. $o achie3e this, all elements greater than the new element are shifted. ;or eJam!le, to
add "/ in the gi3en arra5 4elowG
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P aO0P
) - W 9 0 "" ") "W
Original arra5
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P aO0P
) - W 9 0 "" ") "W
Elements greater than "/ shifted to create free !lace to insert "/
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P aO0P
) - W 9 0 "/ "" ") "W
Arra5 after insertion

;ollowing !rogram im!lement insertion o!eration for sorted arra5
SincludeAiostream.hB
3oid insert%int aO P, int 2n, int item& ,,n is the num4er of elements alread5 !resent in the arra5
L
int iCn:"M
45
while %iBC/ 22 aOiPBitem&
L
aOi1"PCaOiPM ,, shift the i
th
element one !osition towards right
i::M
N
aOi1"PCitemM ,,insertion of item at a!!ro!riate !lace
n11M ,,after insertion, num4er of elements !resent in the arra5 is increased 45 "
N
3oid main%&
Lint aO"/PCL),-,W,9,0,"","),"WN,nC0M
int iC/M
coutAAUOriginal arra5 isGVnQM
for%iC/MiAnMi11&
coutAAaOiPAAQ, UM
insert%a,n,"/&M
coutAAQVnArra5 after inserting "/ isGVnQM
for%iC/M iAnM i11&
coutAAaOiPAAQ, UM
N
Out!ut is
Original arra5 isG
), -, W, 9, 0, "", "), "W
Arra5 after inserting "/ isG
), -, W, 9, 0, "/, "", "), "W
De,etio o& ' item &rom ' )orte- 'rr'=
+n this algorithm the item to 4e deleted from the sorted arra5 is searched and if the item is found in the
arra5 then the element is remo3ed and the rest of the elements are shifted one !osition toward left in
the arra5 to (ee! the ordered arra5 undistur4ed. Deletion o!eration reduces the num4er of elements
!resent in the arra5 45". ;or eJam!le, to remo3e "" from the gi3en arra5 4elowG
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P
) - W 9 0 "" ") "W
Original arra5
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P
) - W 9 0 ") "W
Element remo3ed
aO/P aO"P O)P aO.P aO-P aOWP aOXP aO9P
) - W 9 0 ") "W
Arra5 after shifting the rest element
;ollowing !rogram im!lement deletion o!eration for sorted arra5
SincludeAiostream.hB
3oid deleteZitem%int aO P, int 2n, int item& ,,n is the num4er of elements alread5 !resent in the arra5
Lint iC/M
while%iAn 22 aOiPAitem&
i11M
if %aOiPCCitem& ,, gi3en item is found
Lwhile %iAn&
LaOiPCaOi1"PM ,, shift the %i1"&
th
element one !osition towards left
i11M
N
coutAAQVn #i3en item is successfull5 deletedQM
46
N
else
coutAAQVn #i3en item is not found in the arra5QM
n::M
N
3oid main%&
Lint aO"/PCL),-,W,9,0,"","),"WN,nC0M
int iC/M
coutAAUOriginal arra5 is GVnQM
for%iC/MiAnMi11&
coutAAaOiPAAQ, UM
deleteZitem%a,n,""&M
coutAAQVnArra5 after deleting "" isGVnQM
for%iC/M iAnM i11&
coutAAaOiPAAQ, UM
N
Out!ut is
Original arra5 isG
), -, W, 9, 0, "", "), "W
#i3en item is successfull5 deleted
Arra5 after deleting "" isG
), -, W, 9, 0, "), "W
Tr'(er)',
rocessing of all elements %i.e. from first element to the last element& !resent in one:dimensional
arra5 is called tra3ersal. ;or eJam!le, !rinting all elements of an arra5, finding sum of all elements
!resent in an arra5.
SincludeAiostream.hB
3oid !rintZarra5%int aO P, int n& ,,n is the num4er of elements !resent in the arra5
Lint iM
coutAAQVn #i3en arra5 is GVnQM
for%iC/M iAnM i11&
coutAAaOiPAAQ, UM
N
int sum%int aO P, int n&
Lint i,sC/M
for%iC/M iAnM i11&
sCs1aOiPM
return sM
N
3oid main%&
Lint 4O"/PCL.,W,X,),0,-,","),)W,".N,nC"/M
int i, sM
!rintZarra5%4,n&M
sCsum%4,n&M
coutAAQVn Sum of all elements of the gi3en arra5 is G QAAsM
N
Out!ut is
#i3en arra5 is
., W, X, ), 0, -, ", "), )W, ".
Sum of all elements of the gi3en arra5 is G 98
Sorti.
$he !rocess of arranging the arra5 elements in increasing %ascending& or decreasing %descending&
order is (nown as sorting. $here are se3eral sorting techniIues are a3aila4le e.g. selection sort,
insertion sort, 4u44le sort, Iuic( sort, hea! short etc. 6ut in C6SE s5lla4us onl5 selection sort,
insertion sort, 4u44le sort are s!ecified.
47
Se,ectio Sort
$he 4asic idea of a selection sort is to re!eatedl5 select the smallest element in the remaining
unsorted arra5 and eJchange the selected smallest element with the first element of the unsorted
arra5. ;or eJam!le, consider the following unsorted arra5 to 4e sorted using selection sort
Ori.i', 'rr'=
/ " ) . - W X
0 W 8 . "X - 9
iteration " G Select the smallest element from unsorted arra5 which is . and eJchange . with the
first element of the unsorted arra5 i.e. eJchange . with 0. After iteration " the element .
is at its final !osition in the arra5.
/ " ) . - W X
. W 8 0 "X - 9
+teration )G $he second !ass identif5 - as the smallest element and then eJchange - with W
/ " ) . - W X
. - 8 0 "X W 9
+teration .G $he third !ass identif5 W as the smallest element and then eJchange W with 8
/ " ) . - W X
. - W 0 "X 8 9
+teration -G $he third !ass identif5 9 as the smallest element and then eJchange 9 with 0
/ " ) . - W X
. - W 9 "X 8 0
+teration WG $he third !ass identif5 0 as the smallest element and then eJchange 0 with "X
/ " ) . - W X
. - W 9 0 8 "X
+teration XG $he third !ass identif5 8 as the smallest element and then eJchange 8 with 8 which
ma(es no effect.
/ " ) . - W X
. - W 9 0 8 "X
$he unsorted arra5 with onl5 one element i.e. "X is alread5 at its a!!ro!riate !osition so no more
iteration is reIuired. Hence to sort n num4ers, the num4er of iterations reIuired is n:", where in each
neJt iteration, the num4er of com!arison reIuired to find the smallest element is decreases 45 " as in
each !ass one element is selected from the unsorted !art of the arra5 and mo3ed at the end of sorted
!art of the arra5 . ;or nC9 the total num4er of com!arison reIuired is calculated as
ass"G X com!arisons i.e. %n:"&
ass)G W com!arisons i.e. %n:)&
ass.G - com!arisons i.e. %n:.&
ass-G . com!arisons i.e. %n:-&
assWG ) com!arisons i.e. %n:W&
assXG " com!arison i.e. %n:X&C%n:%n:"&&
$otal com!arison for nC%n:"&1%n:)&1%n:.&1 KKK.1%n:%n:"&&C n%n:"&,)
9CX1W1-1.1)1"C9?X,)C)"M
NoteG ;or gi3en arra5 of n elements, selection sort alwa5s eJecutes n%n:"&,) com!arison statements
irres!ecti3e of whether the in!ut arra5 is alread5 sorted%4est case&, !artiall5 sorted%a3erage case& or
totall5 unsorted%i.e. in re3erse order&%worst case&.
Pro.r'm:
SincludeAiostream.hB
3oid selectZsort%int aO P, int n& ,,n is the num4er of elements !resent in the arra5
Lint i, 7, !, smallM
for%iC/MiAn:"Mi11&
LsmallCaOiPM ,, initiali=e small with the first element of unsorted !art of the arra5
!CiM ,, (ee! indeJ of the smallest num4er of unsorted !art of the arra5 in !
for%7Ci1"M 7AnM 711& ,,loo! for selecting the smallest element form unsorted arra5
48
Lif%aO7PAsmall&
LsmallCaO7PM
!C7M
N
N,, end of inner loo!::::::::::
,,::::::::::eJchange the smallest element with i
th
element:::::::::::::
aO!PCaOiPM
aOiPCsmallM
,,:::::::::::end of eJchange:::::::::::::
N
N,,end of function
3oid main% &
Lint aO9PCL0,W,8,.,"X,-,9N,nC9,iM
coutAAQVn Original arra5 is GVnQM
for%iC/MiAnMi11&
coutAAaOiPAAQ, UM
selectZsort%a,n&M
coutAAQVn$he sorted arra5 isGVnQM
for%iC/M iAnM i11&
coutAAaOiPAAQ, UM
N
Out!ut is
Original arra5 is
0, W, 8, ., "X, -, 9
$he sorted arra5 is
., -, W, 9, 0, 8, "X
I)ertio Sort
+nsertion sort algorithm di3ides the arra5 of n elements in to two su4!arts, the first su4!art contain aO/P
to aO(P elements in sorted order and the second su4!art contain aO(1"P to aOnP which are to 4e sorted.
$he algorithm starts with onl5 first element in the sorted su4!art 4ecause arra5 of one element is itself
in sorted order. +n each !ass, the first element of the unsorted su4!art is remo3ed and is inserted at
the a!!ro!riate !osition in the sorted arra5 so that the sorted arra5 remain in sorted order and hence
in each !ass the si=e of the sorted su4!art is increased 45 " and si=e of unsorted su4!art is
decreased 45 ". $his !rocess continues until all n:" elements of the unsorted arra5s are inserted at
their a!!ro!riate !osition in the sorted arra5.
;or eJam!le, consider the following unsorted arra5 to 4e sorted using selection sort
Ori.i', 'rr'=
/ " ) . - W X
0 W 8 . "X - 9
Sorted unsorted
+nitiall5 the sorted su4!art contains onl5 one element i.e. 0 and the unsorted su4!art contains n:"
elements where n is the num4er of elements in the gi3en arra5.
+teration"G $o insert first element of the unsorted su4!art i.e. W into the sorted su4!art, W is
com!ared with all elements of the sorted su4!art starting from rightmost element to the
leftmost element whose 3alue is greater than W, shift all elements of the sorted su4!art
whose 3alue is greater than W one !osition towards right to create an em!t5 !lace at
the a!!ro!riate !osition in the sorted arra5, store W at the created em!t5 !lace, here 0
will mo3e from !osition aO/P to aO"P and aO/P is filled 45 W. After first !ass the status of
the arra5 isG
/ " ) . - W X
W 0 8 . "X - 9
Sorted unsorted
+teration)G +n second !ass 8 is the first element of the unsorted su4!art, 8 is com!ared with 0,
since 0 is less than 8 so no shifting ta(es !lace and the com!aring loo! terminates. So
49
the element 8 is added at the rightmost end of the sorted su4!art. After second !ass
the status of the arra5 isG
/ " ) . - W X
W 0 8 . "X - 9
Sorted unsorted
+teration.G in third !ass . is com!ared with 8, 0 and W and shift them one !osition towards right
and insert . at !osition aO/P. After third !ass the status of the arra5 isG
/ " ) . - W X
. W 0 8 "X - 9
Sorted unsorted
+teration-G in forth !ass "X is greater than the largest num4er of the sorted su4!art so it remains
at the same !osition in the arra5. After fourth !ass the status of the arra5 isG
Sorted unsorted
+terationWG in fifth !ass - is inserted after .. After third !ass the status of the arra5 isG
/ " ) . - W X
. - W 0 8 "X 9
Sorted unsorted
+terationXG in siJth !ass 9 is inserted after W. After fifth !ass the status of the arra5 isG
/ " ) . - W X
. - W 9 0 8 "X
Sorted
+nsertion sort ta(e ad3antage of sorted%4est case& or !artiall5 sorted%a3erage case& arra5 4ecause if
all elements are at their right !lace then in each !ass onl5 one com!arison is reIuired to ma(e sure
that the element is at its right !osition. So for nC9 onl5 X %i.e. n:"& iterations are reIuired and in each
iteration onl5 one com!arison is reIuired i.e. total num4er of com!arisons reIuiredC %n:"&CX which is
4etter than the selection sort %for sorted arra5 selection sort reIuired n%n:"&,) com!arisons&. $herefore
insertion sort is 4est suited for sorted or !artiall5 sorted arra5s.
Pro.r'm:
SincludeAiostream.hB
3oid insertZsort%int aO P,int n& ,,n is the no of elements !resent in the arra5
Lint i, 7,!M
for %iC"M iAnM i11&
L!CaOiPM
7Ci:"M
,,inner loo! to shift all elements of sorted su4!art one !osition towards right
while%7BC/22aO7PB!&
L
aO71"PCaO7PM
7::M
N
,,:::::::::end of inner loo!
aO71"PC!M ,,insert ! in the sorted su4!art
N
N
3oid main% &
L
int aO9PCL0,W,8,.,"X,-,9N,nC9,iM
coutAAQVn Original arra5 is GVnQM
for%iC/MiAnMi11&
/ " ) . - W X
. W 0 8 "X - 9
50
coutAAaOiPAAQ, UM
insertZsort%a,n&M
coutAAQVn$he sorted arra5 isGVnQM
for%iC/M iAnM i11&
coutAAaOiPAAQ, UM
N
Out!ut is
Original arra5 is
0, W, 8, ., "X, -, 9
$he sorted arra5 is
., -, W, 9, 0, 8, "X
1u//,e Sort
6u44le sort com!ares aOiP with aOi1"P for all iC/..n:), if aOiP and aOi1"P are not in ascending order then
eJchange aOiP with aOi1"P immediatel5. After each iteration all elements which are not at their !ro!er
!osition mo3e at least one !osition towards their right !lace in the arra5. $he !rocess continues until
all elements get their !ro!er !lace in the arra5 %i.e. algorithm terminates if no eJchange occurs in the
last iteration&
;or eJam!le, consider the following unsorted arra5 to 4e sorted using selection sort
Ori.i', 'rr'=
/ " ) . - W X
0 W 8 . "X - 9
+teration"G $he element aO/P i.e. 0 is com!ared with aO"P i.e. W, since 0BW therefore eJchange 0 with
W.
/ " ) . - W X
W 0 8 . "X - 9
$he element aO"P i.e. 0 and aO)P i.e. 8 are alread5 in ascending order so no eJchange
reIuired
/ " ) . - W X
W 0 8 . "X - 9
$he element aO)P i.e. 8 and aO.P i.e. . are not in ascending order so eJchange aO)P with aO.P
/ " ) . - W X
W 0 . 8 "X - 9
$he element aO.P i.e. 8 and aO-P i.e. "X are in ascending order so no eJchange reIuired
/ " ) . - W X
W 0 . 8 "X - 9
$he element aO-P i.e. "X and aOWP i.e. - are not in ascending order so eJchange aO-P with
aOWP
/ " ) . - W X
W 0 8 . - "X 9
$he element aOWP i.e. "X and aOXP i.e. 9 are not in ascending order so eJchange aOWP with
aOXP
/ " ) . - W X
W 0 8 . - 9 "X
Since in iteration" some elements were eJchanged with each other which shows that
arra5 was not sorted 5et, neJt iteration continues. $he algorithm will terminate onl5 if
the last iteration do not !rocess an5 eJchange o!eration which assure that all elements
of the arra5 are in !ro!er order.
51
+teration)G onl5 eJchange o!erations are shown in each !ass
/ " ) . - W X
W 0 8 . - 9 "X
/ " ) . - W X
W 0 . 8 - 9 "X
/ " ) . - W X
W 0 . - 8 9 "X
/ " ) . - W X
W 0 . - 9 8 "X
+n iteration ) some eJchange o!erations were !rocessed, so, at least one more
iteration is reIuired to assure that arra5 is in sorted order.
+teration.G
/ " ) . - W X
W 0 . - 9 8 "X
/ " ) . - W X
W . 0 - 9 8 "X
/ " ) . - W X
W . - 0 9 8 "X
/ " ) . - W X
W . - 9 0 8 "X
+teration-G
/ " ) . - W X
W . - 9 0 8 "X
/ " ) . - W X
. W - 9 0 8 "X
/ " ) . - W X
. - W 9 0 8 "X
+terationWG
/ " ) . - W X
. - W 9 0 8 "X
+n iteration W no eJchange o!eration eJecuted 4ecause all elements are alread5 in
!ro!er order therefore the algorithm will terminate after W
th
iteration.
Mer.i. o& t4o )orte- 'rr'=) ito t%ir- 'rr'= i )orte- or-er
Algorithm to merge arra5s aOmP%sorted in ascending order& and 4OnP%sorted in descending order& into
third arra5 COn1mP in ascending order.
SincludeAiostream.hB
Rerge%int aO P, int m, int 4OnP, int cO P&,, m is si=e of arra5 a and n is the si=e of arra5 4
Lint iC/M ,, i !oints to the smallest element of the arra5 a which is at indeJ /
int 7Cn:"M,, 7 !oints to the smallest element of the arra5 4 which is at the indeJ m:" since 4 is
,, sortet in descending order
int (C/M ,,( !oints to the first element of the arra5 c
while%iAm227BC/&
Lif%aOiPA4O7P&
52
cO(11PCaOi11PM ,, co!5 from arra5 a into arra5 c and then increment i and (
else
cO(11PC4O7::PM ,, co!5 from arra5 4 into arra5 c and then decrement 7 and increment (
N
while%iAm& ,,co!5 all remaining elements of arra5 a
cO(11PCaOi11PM
while%7BC/& ,,co!5 all remaining elements of arra5 4
cO(11PC4O7::PM
N
3oid main%&
Lint aOWPCL),-,W,X,9N,mCWM ,,a is in ascending order
int 4OXPCL"W,"),-,.,),"N,nCXM ,,4 is in descending order
int cO""PM
merge%a, m, 4, n, c&M
coutAAQ$he merged arra5 is GVnQM
for%int iC/M iAm1nM i11&
coutAAcOiPAQ, UM
N
Out!ut is
$he merged arra5 isG
", ), ), ., -, -, W, X, 9, "), "W
T4o -ime)io', 'rr'=)
+n com!uting, ro4-m':or or-er and co,um-m':or or-er descri4e methods for storing
multidimensional arra5s in linear memor5. ;ollowing standard matriJ notation, rows are identified 45
the first indeJ of a two:dimensional arra5 and columns 45 the second indeJ. Arra5 la5out is critical for
correctl5 !assing arra5s 4etween !rograms written in different languages. Row:ma7or order is used in
C, C11M column:ma7or order is used in ;ortran and RA$'A6.
Row-major order
+n row:ma7or storage, a multidimensional arra5 in linear memor5 is accessed such that rows are
stored one after the other. Then using row:ma7or order, the difference 4etween addresses of arra5
cells in increasing rows is larger than addresses of cells in increasing columns. ;or eJam!le, consider
this )a. arra5G
An arra5 declared in C as
int AO)PO.P C L L", ), .N, L-, W, XN NM
would 4e laid out contiguousl5 in linear memor5 asG
" ) . - W X
$o tra3erse this arra5 in the order in which it is laid out in memor5, one would use the following nested
loo!G
for %i C /M i A )M i11&
for %7 C /M 7 A .M 711&
coutAAAOiPO7PM
$he difference in offset from one column to the neJt is "?si=eof%t5!e& and from one row to the neJt is
.?si=eof%t5!e&. $he linear offset from the 4eginning of the arra5 to an5 gi3en element AOrowPOcolumnP
can then 4e com!uted asG
53
o&&)et W ro4S"UMCOLS 5 co,um
A--re)) o& e,emet AKro4LKco,umL c' /e compute- '):
Address of AOrowPOcolumnPC/')e '--re)) o& A 5 Aro4S"UMCOLS 5 co,umBS )iHeo& At=peB
There "UMCOLS is the num4er of columns in the arra5.
$he a4o3e formula onl5 wor(s when using the C, C11 con3ention of la4eling the first element /. +n
other words, row ", column ) in matriJ A, would 4e re!resented as AO/PO"P
Note that this techniIue generali=es, so a )a)a) arra5 loo(s li(eG
int AO)PO)PO)P C LLL",)N, L.,-NN, LLW,XN, L9,0NNNM
and the arra5 would 4e laid out in linear memor5 asG
" ) . - W X 9 0
EG'mp,e 1+
;or a gi3en arra5 AO"/PO)/P is stored in the memor5 along the row with each of its elements occu!5ing
- 45tes. Calculate address of AO.POWP if the 4ase address of arra5 A is W///.
So,utio:
;or gi3en arra5 AORPONP where RCNum4er of rows, N CNum4er of Columns !resent in the arra5
address of AO+POJPC 4ase address1%+ ? N 1 J&?si=eof%t5!e&
here RC"/, NC)/, +C., JCW, si=eof%t5!e&C- 45tes
address of AO.POWP C W/// 1 %. ? )/ 1 W& ? -
C W/// 1 XW?-CW///1)X/CW)X/
EG'mp,e 2+
An arra5 AOW/PO)/P is stored in the memor5 along the row with each of its elements occu!5ing 0 45tes.
;ind out the location of AOWPO"/P, if AO-POWP is stored at -///.
So,utio:
Calculate 4ase address of A i.e. address of AO/PO/P
;or gi3en arra5 AORPONP where RCNum4er of rows, N CNum4er of Columns !resent in the arra5
address of AO+POJPC 4ase address1%+ ? N 1 J&?si=eof%t5!e&
here RCW/, NC)/, si=eof%t5!e&C0, +C-, JCW
address of AO-POWP C 4ase address 1 %-?)/ 1W&?0
-/// C 4ase address 1 0W?0
6ase addressC -///:0W?0C -///:X0/C..)/
Now to find address of AOWPO"/P
here RCW/, NC)/, si=eof%t5!e&C0, +CW, JC"/
Address of AOWPO"/P C 4ase address 1%W?)/ 1 "/&?0
C..)/ 1 ""/?0 C ..)/100/ C -)//
Co,um-m':or or-er is a similar method of flattening arra5s onto linear memor5, 4ut the columns are
listed in seIuence. $he !rogramming languages ;ortran, RA$'A6, use column:ma7or ordering. $he
arra5
54
if stored contiguousl5 in linear memor5 with column:ma7or order would loo( li(e the followingG
" - ) W . X
$he memor5 offset could then 4e com!uted asG
o&&)et W ro4 5 co,umS"UMRO#S
A--re)) o& e,emet AKro4LKco,umL c' /e compute- '):
Address of AOrowPOcolumnPC/')e '--re)) o& A 5 Aco,umS"UMRO#S 5ro4)BS )iHeo& At=peB
There "UMRO#S re!resents the num4er of rows in the arra5 in this case, ).
$reating a row:ma7or arra5 as a column:ma7or arra5 is the same as trans!osing it. 6ecause
!erforming a trans!ose reIuires data mo3ement, and is Iuite difficult to do in:!lace for non:sIuare
matrices, such trans!ositions are rarel5 !erformed eJ!licitl5. ;or eJam!le, software li4raries for linear
alge4ra, such as the 6'AS, t5!icall5 !ro3ide o!tions to s!ecif5 that certain matrices are to 4e
inter!reted in trans!osed order to a3oid the necessit5 of data mo3ement
EG'mp,e1+
;or a gi3en arra5 AO"/PO)/P is stored in the memor5 along the column with each of its elements
occu!5ing - 45tes. Calculate address of AO.POWP if the 4ase address of arra5 A is W///.
So,utio:
;or gi3en arra5 AORPONP where RCNum4er of rows, N CNum4er of Columns !resent in the arra5
Address of AOIPOJPC 4ase address 1 %J ? R 1 I&?si=eof%t5!e&
here RC"/, NC)/, IC., JCW, si=eof%t5!e&C- 45tes
Address of AO.POWP C W/// 1 %W ? "/ 1 .& ? -
C W/// 1 W.?- C W///1)"W CW)"W
EG'mp,e2+
An arra5 AOW/PO)/P is stored in the memor5 along the column with each of its elements occu!5ing 0
45tes. ;ind out the location of AOWPO"/P, if AO-POWP is stored at -///.
So,utio:
Calculate 4ase address of A i.e. address of AO/PO/P
;or gi3en arra5 AORPONP where RCNum4er of rows, N CNum4er of Columns !resent in the arra5
address of AOIPOJPC 4ase address1%J ? R 1 I&?si=eof%t5!e&
here RCW/, NC)/, si=eof%t5!e&C0, IC-, JCW
address of AO-POWP C 4ase address 1 %W ? W/ 1-&?0
-/// C 4ase address 1 )W-?0
6ase addressC -///:WW?0C -///:)/.)C"8X0
Now to find address of AOWPO"/P
here RCW/, NC)/, si=eof%t5!e&C0, I CW, JC"/
Address of AOWPO"/P C 4ase address 1%"/?W/ 1 "/&?0
C"8X0 1 W"/?0 C "8X01-/0/ C X/-0
2 M'r*) 6ue)tio)
1+ Trite a function in C11 which acce!ts an integer arra5 and its si=e as arguments and re!laces
elements ha3ing e3en 3alues with its half and elements ha3ing odd 3alues with twice its 3alue
2+ Trite a function in C11 which acce!ts an integer arra5 and its si=e as argument and eJchanges the
3alue of first half side elements with the second half side elements of the arra5.
EJam!leG +f an arra5 of eight elements has initial content as ),-,",X,9,8,).,"/. $he function should
rearrange the arra5 as 9,8,).,"/,),-,",X.
0+ Trite a function in c11 to find and dis!la5 the sum of each row and each column of ) dimensional
arra5. *se the arra5 and its si=e as !arameters with int as the data t5!e of the arra5.
55
2+ Trite a function in C11, which acce!ts an integer arra5 and its si=e as !arameters and rearrange
the arra5 in re3erse. EJam!le if an arra5 of fi3e mem4ers initiall5 contains the elements as
X,9,0,".,8,"8
$hen the function should rearrange the arra5 as "8,8,".,0,9,X
7+ Trite a function in C11, which acce!t an integer arra5 and its si=e as arguments and swa! the
elements of e3er5 e3en location with its following odd location. EJam!le G if an arra5 of nine elements
initiall5 contains the elements as ),-,",X,W,9,8,).,"/ $hen the function should rearrange the arra5 as
-,),X,",9,W,).,8,"/
>+ Trite a function in C11 which acce!ts an integer arra5 and its si=e as arguments and re!laces
elements ha3ing odd 3alues with thrice and elements ha3ing e3en 3alues with twice its 3alue.
EJam!leG +f an arra5 of fi3e elements initiall5 contains the elements .,-,W,"X,8
$hen the function should rearrange the content of the arra5 as 8,0,"W,.),)9
8+ Trite function SOR$O+N$S%& in c11 to sort an arra5 of structure #ame in descending order of
!oints using 6u44le Sort NoteG Assume the following definition of structure #ame
struct #ame
Llong NoM ,, la5er Num4er
char NameO)/PM
long !ointsM
NM
?+ Trite a c11 function to shift all the negati3e num4ers to left and !ositi3e num4er in the right side.
@+ Define a function STCO'%& in C11 to swa! % interchange& the first column elements with the last
column elements, for a two dimensional arra5 !assed as the argument of the function.
EJam!le G if the two dimensional arra5 contains
) " - 8
" . 9 9
W 0 X .
9 ) " )
After swa!!ing of the content of "st and last column, it should 4e
8 " - )
9 . 9 "
. 0 X W
) ) " 9
10. Trite a function which acce!t )D arra5 of integers and its si=e as arguments and dis!la5s the sum
of elements which lie on diagonals.
OAssuming the )D arra5 to 4e a sIuare matriJ with odd dimension ie . J . , - J - etc P
EJam!le of the arra5 content is
W - .
X 9 0
" ) 8
Out!ut through the function should 4e
Diagonal One Sum G )"
Diagonal $woG ""
17. Trite a user defined function named u!!erhalf%& which ta(es a )D arra5 A, with si=e n rows and n
cols as arguments and !rint the u!!er half of the matriJ
1>. Trite a user defined function lowerhalf%& which ta(es a )D arra5, with si=e n rows and n cols as
argument and !rints the lower half of the matriJ
18+ Trite the function to find the largest and second largest num4er from a two dimensional arra5. $he
function should acce!t the arra5 and its si=e as argument.
1?+ Trite a function in C11 to merge the contents of two sorted arra5s A 2 6 into third arra5 C.
Assuming arra5 A is sorted in ascending order, 6 is sorted in descending order, the resultant arra5 is
reIuired to 4e in ascending order.
1@+ An arra5 arrO-/PO./P stored in the memor5 along the column with each of the element occu!5ing -
45tes. ;ind out the 4ase address and address of the element arrO)/PO"WP, if an element arrO"WPO"/P is
stored at the memor5 location 9)//.
20. An arra5 sOW/PO"/P stored in the memor5 along the row with each element occu!5ing ) 45tes. ;ind
out the address of the location sO)/POW/P if the location sO"/PO)WP stored at the address "////.
56
St'c*E 6ueue) u)i. Arr'=) '- Li*e- Li)t
St'c*
+n com!uter science, a stac( is a last in, first out %'+;O& data structure. A stac( can is characteri=ed 45
onl5 two fundamental o!erationsG push and pop. $he !ush o!eration adds an item to the to! of the
stac(. $he !o! o!eration remo3es an item from the to! of the stac(, and returns this 3alue to the
caller.
A stac( is a restricted data structure, 4ecause onl5 a small num4er of o!erations are !erformed on it.
$he nature of the !o! and !ush o!erations also mean that stac( elements ha3e a natural order.
Elements are remo3ed from the stac( in the re3erse order to the order of their additionG therefore, the
lower elements are those that ha3e 4een on the stac( the longest. One of the common uses of stac(
is in function call.
St'c* u)i. 'rr'=
SincludeAiostream.hB
const int si=eCW
class stac(
Lint aOsi=ePM ,,arra5 a can store maJimum W item of t5!e int of the stac(
int to!M ,,to! will !oint to the last item !ushed onto the stac(
!u4licG
stac(%&Lto!C:"MN ,,constructor to create an em!t5 stac(, to!C:" indicate that no item is ,,!resent
in the arra5
3oid !ush%int item&
L+f%to!CCsi=e:"&
coutAAQstac( is full, gi3en item cannot 4e addedQM
else
aO11to!PCitemM ,,increment to! 45 " then item at new !osition of the to! in the arra5 a
N
int !o!%&
L+f %to!CC:"&
LoutAAQStac( is em!t5 UM
return :"M ,,:" indicates em!t5 stac(
N
else
return aOto!::PM,,return the item !resent at the to! of the stac( then decrement to! 45 "
N
3oid main%&
L stac( s"M
s".!ush%.&M
s".!ush%W&M
coutAAs".!o!%&AAendlM
coutAAs".!o!%&AAendlM
coutAAs".!o!%&M
N
Out!ut is
W
.
Stac( is em!t5 :"
+n the a4o3e !rogram the statement )t'c* )1 creates s" as an em!t5
stac( and the constructor initiali=e to! 45 :".
-
.
)
to! " W
/ .
57
+nitiall5 stac( is em!t5 stac( after s".!ush%.& stac( after s".!ush%W&
-
.
)
"
/
to! :"

After first s".!o!%& statement, the item W is remo3ed from the stac( and to! mo3es from " to /
After second s".!o!%& statement, the item . is remo3ed from stac( and to! mo3es from / to :" which
indicates that now stac( is em!t5.
-
.
)
"
/
to! :"
After third s".!o!%& statement the !o! function dis!la5 error message Ustac( is em!t5Q and returns :"
to indicating that stac( is em!t5 and do not change the !osition of to! of the stac(.
St'c* u)i. Li*e- ,i)t
+n Com!uter Science, a ,i*e- ,i)t %or more clearl5, [singl5:lin(ed list[& is a data structure that
consists of a seIuence of nodes each of which contains data and a !ointer which !oints %i.e., a link& to
the neJt node in the seIuence.
A linked list whose nodes contain two fields an integer value and a link to the ne!t node
$he main 4enefit of a lin(ed list o3er a con3entional arra5 is that the list elements can easil5 4e added
or remo3ed without reallocation or reorgani=ation of the entire structure 4ecause the data items need
not 4e stored contiguousl5 in memor5 or on dis( .Stac( using lin(ed lists allow insertion and remo3al
of nodes onl5 at the !osition where the !ointer to! is !ointing to.
St'c* imp,emet'tio u)i. ,i*e- ,i)t
SincludeAiostream.hB
struct node
L
+nt itemM ,,data that will 4e stored in each node
node ? neJtM ,,!ointer which contains address of another node
NM ,,node is a self referential structure which contains reference of another o47ect t5!e node
class stac(
Lnode ?to!M
!u4licG
stac(%& ,,constructor to create an em!t5 stac( 45 initiali=ing to! with N*''
-
.
)
"
to! / .
-
.
)
"
to! / .
58
L to!CN*''M N
3oid !ush%int item&M
int !o!%&M
^stac(%&M
NM
3oid stac(GG!ush%int item& ,,to insert a new node at the to! of the stac(
Lnode ?tCnew nodeM ,,d5namic memor5 allocation for a new o47ect of node t5!e
if%tCCN*''&
coutAAQRemor5 not a3aila4le, stac( is fullQM
else
Lt:BitemCitemM
t:BneJtCto!M ,,newl5 created node will !oint to the last inserted node or N*'' if
,,stac( is em!t5
to!CtM ,,to! will !oint to the newl5 created node
N
N
int stac(GG!o!%&,,to delete the last inserted node%which is currentl5 !ointed 45 the to!&
Lif%to!CCN*''&
LcoutAAQStac( is em!t5 VnQM
return /M ,, / indicating that stac( is em!t5
N
else
Lnode ?tCto!M ,,sa3e the address of to! in t
int rCto!:BitemM ,,store item of the node currentl5 !ointed 45 to!
to!Cto!:BneJtM ,, mo3e to! from last node to the second last node
delete tM ,,remo3e last node of the stac( from memor5
return rM
N
N
stac(GG^stac(%&,,de:allocated all undeleted nodes of the stac( when stac( goes out of sco!e
Lnode ?tM
while%to!DCN*''&
LtCto!M
to!Cto!:BneJtM
delete tM
N
NM
3oid main%&
L stac( s"M
s".!ush%.&M
s".!ush%W&M
s".!ush%9&M
coutAAs".!o!%&AAendlM
coutAAs".!o!%&AAendlM
coutAAs".!o!%&AAendlM
coutAAs".!o!%&AAendlM
N
Out!ut is
9
W
.
Stac( is em!t5 /
+n the a4o3e !rogram the statement stack s"# in3o(es the constructor stac(%& which create an em!t5
stac( o47ect s" and initiali=e to! with N*''.
to! N*''
After statement s".!ush%.& the stac( 4ecome
59
to! N*''
After statement s".!ush%W& the stac( 4ecome
to! N*''
After statement s".!ush%9& the stac( 4ecome
to! N*''
After the first s".!o!%& statement the node currentl5 !ointed 45 to! %i.e. node containing 9& is deleted
from the stac(, after deletion the status of stac( is
to! N*''
After the second s".!o!%& statement the node currentl5 !ointed 45 to! %i.e. node containing W& is
deleted from the stac(, after deletion the status of stac( is
to! N*''
After the third s".!o!%& statement the node currentl5 !ointed 45 to! %i.e. node containing .& is deleted
from the stac(, after deletion the stac( 4ecome em!t5 i.e.
$o! N*''
After the fourth s".!o!%& statement, the error message Ustac( is em!t5U dis!la5ed and the !o!%&
function return / to indicate that stac( is em!t5.
App,ic'tio o& )t'c*) i i&iG eGpre))io to po)t&iG eGpre))io co(er)io
+nfiJ eJ!ression oper'-1 oper'tor oper'-2 for eJam!le '5/
ostfiJ eJ!ression oper'-1 oper'-2 oper'tor for eJam!le '/5
refiJ eJ!ression oper'tor oper'-1 oper'-2 for eJam!le 5'/
Some eJam!le of infiJ eJ!ression and their corres!onding !ostfiJ eJ!ression
+nfiJ eJ!ression !ostfiJ eJ!ression
a?%4:c&,e a4c:?e,
%a14&?%c:d&,e a41cd:?e,
%a14?c&,%d:e&1f a4c?1de:,f1
A,.orit%m to co(ert i&iG eGpre))io to po)t&iG eGpre))io u)i. )t'c*
'et the infiJ eJ!ression +NE] is to 4e con3erted in to eIui3alent !ostfiJ eJ!ression OS$E]. $he
!ostfiJ eJ!ression OS$E] will 4e constructed from left to right using the o!erands and o!erators
%eJce!t U%U, and U&Q& from +NE]. $he algorithm 4egins 45 !ushing a left !arenthesis onto the em!t5
stac(, adding a right !arenthesis at the end of +NE], and initiali=ing OS$E] with null. $he
algorithm terminates when stac( 4ecome em!t5.
$he algorithm contains following ste!s
". +nitiali=e OS$E] with null
). Add _&< at the end of +NE]
.. Create an em!t5 stac( and !ush _%_ on to the stac(
-. +nitiali=e iC/,7C/
W. Do while stac( is not em!t5
X. +f +NE]OiP is an o!erand then
.
.
W
W
.
.
W
.
60
OS$E]O7PC+NE]OiP
+Ci1"
7C71"
#oto ste! W
9. +f +NE]OiP is _%_ then
!ush %+NE]OiP&
iCi1"
#oto ste! W
0. +f +NE]OiP is an o!erator then
Thile !recedence of the o!erator at the to! of the stac( B !recedence of o!erator
OS$E]O7PC!o!%&
JC71"
End of while
ush %+NE]OiP&
+Ci1"
#oto ste! W
8. +f +NE]OiP is _&< then
Thile the o!erator at the to! of the stac( is not _%_
OS$E]O7PC!o!%&
JC71"
End while
o!%&
"/. End of ste! W
"". End algorithm
;or eJam!le con3ert the infiJ eJ!ression %A16&?%C:D&,E into !ostfiJ eJ!ression showing stac( status
after e3er5 ste!.
S5m4ol scanned from infiJ Stac( status % 4old letter
shows the to! of the stac(&
ostfiJ eJ!ression
A
% %A
A %A A
1 %%5 A
6 %%5 A6
& A A61
? %S A61
% %?A A61
C %?A A61C
: %?%- A61C
D %?%- A61CD
& %S A61CD:
, %/ A61CD:?
E %/ A61CD:?E
& A61CD:?E,
AnswerG ostfiJ eJ!ression of AA51BSAC-DB/E is A15CD-SE/
E(',u'tio o& Po)t&iG eGpre))io u)i. St'c*
Algorithm to e3aluate a !ostfiJ eJ!ression .
". Create an em!t5 stac(
). iC/
.. while OiP DC N*''
if OiP is o!erand then
ush%OiP&
+Ci1"
Else if OiP is a o!erator then
O!erand)C!o!%&
O!erand"C!o!%&
ush %O!erand" o!erator O!erator)&
61
End if
-. End of while
W. return !o!%& ,, return the calculated 3alue which a3aila4le in the stac(.
End of algorithm
EG'mp,e: E3aluate the following !ostfiJ eJ!ression showing stac( status after e3er5 ste!
?E 2E 5E 7E 0E -E SE 2 /
to(en scanned
from !ostfiJ
eJ!ression
Stac( status % 4old letter
shows the to! of the
stac(& after !rocessing
the scanned to(en
O!eration !erformed
0 ? ush 0
) 0, 2 ush )
1 10 O!)C!o!%& i.e )
O!"C!o!%& i.e 0
ush%o!"1o!)& i.e. 01)
W "/, 7 ush%W&
. "/, W, 0 ush%.&
: "/, 2 O!)C!o!%& i.e. .
O!"C!o!%& i.e. W
ush%o!":o!)& i.e. W:.
? 20 O!)C!o!%& i.e. )
O!"C!o!%& i.e. "/
ush%o!":o!)& i.e. "/?)
- )/, 2 ush -
, W O!)C!o!%& i.e. -
O!"C!o!%& i.e. )/
ush%o!",o!)& i.e. )/,-
N*'' ;inal result W o! W and return W
EG'mp,e:
E3aluate the following 6oolean !ostfiJ eJ!ression showing stac( status after e3er5 ste!
TrueE ;',)eE TrueE A"DE ORE ;',)eE "OTE A"D
to(en scanned
from !ostfiJ
eJ!ression
Stac( status % 4old letter
shows the to! of the
stac(& after !rocessing
the scanned to(en
O!eration !erformed
$rue True ush $rue
;alse $rue, ;',)e ush ;alse
$rue $rue, ;alse, True ush $rue
AND $rue, ;',)e O!)C!o!%& i.e. $rue
O!"C!o!%& i.e. ;alse
ush%O!) AND O!"& i.e. ;alse AND
$rueC;alse
OR $rue O!)C!o!%& i.e. ;alse
O!"C!o!%& i.e. $rue
ush%O!) OR O!"& i.e. $rue OR ;alseC$rue
;alse $rue, ;',)e ush ;alse
NO$ $rue, True O!"C!o!%& i.e. ;alse
ush%NO$ ;alse& i.e. NO$ ;alseC$rue
AND True O!)C!o!%& i.e. $rue
O!"C!o!%& i.e. $rue
ush%O!) AND O!"& i.e. $rue AND $rueC$rue
N*'' ;inal result True o! $rue and Return $rue
62
6ueue
`ueue is a linear data structure which follows ;irst +n ;irst Out %;+;O& rule in which a new item is
added at the rear end and deletion of item is from the front end of the Iueue. +n a ;+;O data structure,
the first element added to the Iueue will 4e the first one to 4e remo3ed.
Lie'r 6ueue imp,emet'tio u)i. Arr'=
SincludeAiostream.hB
const int si=eCWM
class Iueue
Lint front , rearM
int aOsi=ePM
!u4licG
Iueue%&LfrotC/MrearC/MN ,,Constructor to create an em!t5 Iueue
3oid add`%&
L if%rearCCsi=e&
coutAAQIueue is fullAAendlM
else
aOrear11PCitemM
N
int del`%&
Lif%frontCCrear&
LcoutAAQIueue is em!t5QAAendlM return /MN
else
return aOfront11PM
N
N
3oid main%&
LIueue I"M
I".add`%.&M
I".add`%W& M
I".add`%9& M
coutAAI".del`%&AAendl M
coutAAI".del`%&AAendl M
coutAAI".del`%&AAendlM
coutAAI".del`%&AAendlM
N
Out!ut is
.
W
9
`ueue is em!t5
/
+n the a4o3e !rogram the statement Dueue D1 creates an em!t5 Iueue I".
;ront
aO/P aO"P aO)P aO.P aO-P

Rear
After eJecution of the statement D1+'--6A0BE status of Iueue is
;ront
aO/P aO"P aO)P aO.P aO-P
.
63
Rear
After eJecution of the statement D1+'--6A7BE status of Iueue is
;ront
aO/P aO"P aO)P aO.P aO-P
. W
Rear
After eJecution of the statement D1+'--6A8BE status of Iueue is
;ront
aO/P aO"P aO)P aO.P aO-P
. W 9
Rear
After eJecution of the first coutAAD1+-e,6AB statementE . is deleted from Iueue status of Iueue is
;ront
aO/P aO"P aO)P aO.P aO-P
. W 9
Rear
After eJecution of the second coutAAD1+-e,6AB statementE 7 is deleted from the Iueue status of Iueue
is
;ront
aO/P aO"P aO)P aO.P aO-P
. W 9
Rear
After eJecution of the third coutAAD1+-e,6AB statementE 8 is deleted from the Iueue. $he status of
Iueue is em!t5
;ront
aO/P aO"P aO)P aO.P AO-P
. W 9
Rear
After eJecution of the fourth coutAAD1+-e,6AB statementE the message UIueue is em!t5U dis!la5ed
and status of Iueue is ;ront
aO/P aO"P aO)P aO.P aO-P
. W 9
Rear
Note that since rear and front mo3es onl5 in one direction therefore once the rear cross the last
element of the arra5%i.e. rearCCsi=e& then e3en after deleting some element of Iueue the free s!aces
a3aila4le in Iueue cannot 4e allocated again and the function del`%& dis!la5 error message UIueue is
fullQ.
6ueue u)i. ,i*e- ,i)t
SincludeAiostream.hB
struct node
L
int itemM
node ?neJtM
NM
class Iueue
64
L
node ?front, ?rearM
!u4licG
Iueue%& LfrontCN*''M rearCN*''MN,,constructor to create em!t5 Iueue
3oid add`%int item&M
int del`%&M
NM
3oid IueueGGadd`%int item&
Lnode ? tCnew nodeM
if%tCCN*''&
coutAAQmemor5 not a3aila4le, Iueue is fullQAAendlM
else
Lt:BitemCitemM
t:BneJtCN*''M
if %rearCCN*''& ,,if the Iueue is em!t5
LrearCtM frontCtM ,,rear and front 4oth will !oint to the first node
N
else
L
rear:BneJtCtM
rearCtM
N
N
N
int IueueGGdel`%&
L
if%frontCCN*''&
coutAAQIueue is em!t5QAAreturn /M
else
Lnode ?tCfrontM
int rCt:BitemM
frontCfront:BneJtM ,,mo3e front to the neJt node of the Iueue
if%frontCCN*''&
rearCCN*''M
delete tM
return rM
N
N
3oid main%&
L
Iueue I"M
I".add`%.&M
I".add`%W& M
I".add`%9& M
coutAAI".del`%&AAendl M
coutAAI".del`%&AAendl M
coutAAI".del`%&AAendlM
coutAAI".del`%&AAendlM
N
Out!ut is
.
W
9
`ueue is em!t5 /
+n the a4o3e !rogram the statement Dueue D1I in3o(es the constructor Iueue%& which create an
em!t5 Iueue o47ect I" and initiali=e front and rear with N*''.
front
N*''
65
rear
After statement I".add`%.& the stac( 4ecome
front
N*''
rear
After statement I".add`%W& the stac( 4ecome
front
N*''
rear
After statement I".add`%9& the stac( 4ecome
front
N*''
rear
After the first I".del`%& statement the node currentl5 !ointed 45 front %i.e. node containing .& is
deleted from the Iueue, after deletion the status of Iueue is
front
N*''
rear
After the second I".del`%& statement the node currentl5 !ointed 45 front %i.e. node containing W& is
deleted from the Iueue, after deletion the status of Iueue is
front
N*''
rear
After the third I".del`%& statement the node currentl5 !ointed 45 front %i.e. node containing 9& is
deleted from the Iueue, after deletion the Iueue 4ecome em!t5 therefore N*'' is a assigned to 4oth
rear and front
front
N*''
rear
After the fourth I".del`%& statement, the error message UIueue is em!t5U dis!la5ed and the !o!%&
function return / to indicate that Iueue is em!t5.
Circu,'r Dueue u)i. 'rr'=
Circular Iueues o3ercome the !ro4lem of unutilised s!ace in linear Iueues im!lemented as arra5. +n
circular Iueue using arra5 the rear and front mo3es in a circle from /,",)Ksi=e:",/, and so on.
SincludeAiostream.hB
const int si=eC-M
class CIueue
L
int aOsi=ePM
int front,rear,an5itemM
!u4licG
3oid CIueue%&LfrontC/MrearC/Man5itemC/MN
.
W
.
W
9
W
9
66
3oid addC`%int item&M
int delC`%&M
NM
3oid CIueueGGaddC`%int item&
L
+f%frontCCrear 22 an5itemB/&
coutAAQCIueue is fullQAAendlM
else
LaOrearPCitemM
rearC%rear1"&@si=eM ,,rear will mo3e in circular order
an5item11M ,,3alue of the an5item contains no of items !resent in the Iueue
N
N
int CIueueGGdelC`%&
L
if%frontCCrear22 an5itemCC/&
coutAAQCIueue is em!t5QAAendlM return /M ,,/ indicate that CIueue is em!t5
else
Lint rCaOfrontPM
frontC%front1"&,si=eM
an5item::M
N
N
3oid main%&
LCIueue I"M
I".addC`%.&M
I".addC`%W& M
coutAAI".delC`%&AAendl M
I".addC`%9& M
coutAAI".delC`%&AAendl M
I".addC`%0& M
I".addC`%8& M
coutAAI".delC`%&AAendlM
coutAAI".delC`%&AAendlM
N
Out!ut is
.
W
9
0
St'c*E 6ueue) u)i. Arr'=) '- Li*e- Li)t : Pr'ctice 6ue)tio)
2 M'r*) 6ue)tio)
". Con3ert the following infiJ eJ!ressions to !ostfiJ eJ!ressions using stac(
". A 1 %6 ? C& b D > %E , ; > #&
). A ? 6 , C ? D b E ? # , H
.. %%A?6&:%%CZD&?E,;&?#
-. A16,C?D1;?#
W. A16:A,%6?%A:6:A?D&b6&
X. %61%C1D&?%E1;&,#&,H
9. %$R*E EE ;A'SE& 22 D %;A'SE EE $R*E&
0. %A , 6 1 C & , D 1 E , %; 1 # ? H , +&
8. %%%4:%c?d:e&1f&&,g&1%h?71J&
"/. A1%%%6?C&?%D1E&1;?#&b%H:J&
"". %A:6& ?%C,%D:E&1;:#
67
). E3aluate the following !ostfiJ eJ!ression E gi3en 4elowM show the contents of the stac( during the
e3aluation
". EC W,8,1),,,-,",",.,Z,?,1
). EC 0/,.W,)/,:,)W,W,1,:,?
.. EC ./,W,),b,"),X,,,1,:
-. EC"W, ., ), 1, ,, 9, 1 ), ?
W. EC)W, 0, ., : , , X, ?, "/ 1
X. EC0, 9, :, 8, 0, ?, :, ), , , .,-, ? ), , :
9. EC W,)/,"W,:,?,)W,),?,:
0. EC "/,1,"W,?,)W,W,,,)1
8. EC 9,X,),,,1,"0,:
"/. EC 9,X, 1, 0, ? , ), :, ., ? , ), -, ? , :
"". EC$R*E, ;A'SE, NO$, $R*E, OR, ;A'SE, AND, OR
"). EC;A'SE, $R*E, $R*E, NO$, AND, OR, AND
0 or 2 M'r*) 6ue)tio)
1. An array A[40][10] is stored in the memory along the column with each element
occupying 4 bytes. Find out the address of the location A[3][6] if the location A[30][10] is
stored at the address 000.
!. An array A[30][!0] is stored in the memory along the row with each element
occupying ! bytes. Find out the address of the location A[10]["] if the location A[!0][10]
is stored at the address 10000.
3. #e$ne functions in %&& to perform a '()* and '+' operation in a dynamically
allocated stac, considering the following -
struct .ode
/ int 0123
.ode 45in,3 63
class )7A%8
/ .ode 4 7op3
public-
)7A%89 :
/
7+';.(553
6
<oid '()*9 :3
<oid '+'9 :3
=)7A%89 :3
63
4. >rite a function in %&& to perform a Add and #elete operation in a dynamically
allocated ?ueue considering the following-
struct node
/
int empno 3char name[!0] 3@oat sal 3
.ode 45in,3
63
68
DATA1ASE A"D S6L
1')ic D't'/')e cocept)
D't' G: Raw facts and figures which are useful to an organi=ation. Te cannot ta(e decisions
on
the 4asis of data.
I&orm'tioG: Tell !rocessed data is called information. Te can ta(e decisions on the 4asis of
information
;ie,-G Set of characters that re!resents s!ecific data element.
Recor-G Collection of fields is called a record. A record can ha3e fields of different data t5!es.
;i,eG Collection of similar t5!es of records is called a file.
T'/,eG Collection of rows and columns that contains useful data,information is called a ta4le.
A ta4le generall5 refers to the !assi3e entit5 which is (e!t in secondar5 storage de3ice.
Re,'tioG Relation %collection of rows and columns& generall5 refers to an acti3e entit5 on which
we can !erform 3arious o!erations.
D't'/')eG Collection of logicall5 related data along with its descri!tion is termed as data4ase.
Tup,e: A row in a relation is called a tu!le.
Attri/ute: A column in a relation is called an attri4ute. +t is also termed as field or data item.
De.ree: Num4er of attri4utes in a relation is called degree of a relation.
C'r-i',it=: Num4er of tu!les in a relation is called cardinalit5 of a relation.
Prim'r= !e=: rimar5 (e5 is a (e5 that can uniIuel5 identifies the records,tu!les in a relation. $his
(e5 can ne3er 4e du!licated and N*''.
;orei. !e=: ;oreign Ke5 is a (e5 that is defined as a !rimar5 (e5 in some other relation. $his (e5 is
used to enforce referential integrit5 in RD6RS.
C'-i-'te !e=: Set of all attri4utes which can ser3e as a !rimar5 (e5 in a relation.
A,ter'te !e=: All the candidate (e5s other than the !rimar5 (e5s of a relation are alternate (e5s for
a relation.
D1A: Data 6ase Administrator is a !erson %manager& that is res!onsi4le for defining the data
4ase schema, setting securit5 features in data4ase, ensuring !ro!er functioning
of the data 4ases etc.
Structure- 6uer= L'.u'.e
S`' is a non !rocedural language that is used to create, mani!ulate and !rocess the
data4ases%relations&.
C%'r'cteri)tic) o& S6L
". +t is 3er5 eas5 to learn and use.
). 'arge 3olume of data4ases can 4e handled Iuite easil5.
.. +t is non !rocedural language. +t means that we do not need to s!ecif5 the !rocedures to
accom!lish a tas( 4ut 7ust to gi3e a command to !erform the acti3it5.
-. S`' can 4e lin(ed to most of other high le3el languages that ma(es it first choice for the
data4ase !rogrammers.
Proce))i. C'p'/i,itie) o& S6L
$he following are the !rocessing ca!a4ilities of S`'
1+ D't' De&iitio L'.u'.e ADDLB
DD' contains commands that are used to create the ta4les, data4ases, indeJes, 3iews,
seIuences and s5non5ms etc.
e.gG Create ta4le, create 3iew, create indeJ, alter ta4le etc.
2+ D't' M'ipu,'tio L'.u'.e ADMLB
DR' contains command that can 4e used to mani!ulate the data 4ase o47ects and to Iuer5 the
data4ases for information retrie3al.
e.g Select, +nsert, Delete, *!date etc.
0+ 3ie4 De&iitio:
DD' contains set of command to create a 3iew of a relation.
e.g G create 3iew
2+ D't' Cotro, L'.u'.e:
69
$his language is used for controlling the access to the data. Various commands li(e #RAN$,
REVOKE etc are a3aila4le in DC'.
7. Tr')'ctio Cotro, L'.u'.e ATCLB
$C' include commands to control the transactions in a data 4ase s5stem. $he commonl5 used
commands in $C' are CORR+$, RO''6ACK etc.
D't' t=pe) o& S6L
Just li(e an5 other !rogramming language, the facilit5 of defining data of 3arious t5!es is a3aila4le in
S`' also. ;ollowing are the most common data t5!es of S`'.
"& N*R6ER
)& CHAR
.& VARCHAR , VARCHAR)
-& DA$E
W& 'ON#
X& RAT,'ON# RAT
1+ "UM1ER
*sed to store a numeric 3alue in a field,column. +t ma5 4e decimal, integer or a real 3alue. #eneral
s5ntaJ is
Num4er%n,d&
There s!ecifies the num4er of digits and
- s!ecifies the num4er of digits to the right of the decimal !oint.
e.g mar(s num4er%.& declares mar(s to 4e of t5!e num4er with maJimum 3alue 888.
!ct num4er%W,)& declares !ct to 4e of t5!e num4er of W digits with two digits to the right of
decimal !oint.
2+ C<AR
*sed to store character t5!e data in a column. #eneral s5ntaJ is
Char %si=e&
where si=e re!resents the maJimum num4er of characters in a column. $he CHAR t5!e data can hold
at most )WW characters.
e.g name char%)W& declares a data item name of t5!e character of u!to )W si=e long.
0+ 3ARC<AR/3ARC<AR2
$his data t5!e is used to store 3aria4le length al!hanumeric data. #eneral s5ntaJ is
3archar%si=e& , 3archar)%si=e&
where si=e re!resents the maJimum num4er of characters in a column. $he maJimum allowed si=e in
this data t5!e is )/// characters.
e.g address 3archar%W/&M address is of t5!e 3archar of u!to W/ characters long.
2+ DATE
Date data t5!e is used to store dates in columns. S`' su!!orts the 3arious date formats other that
the standard DD:RON:\\.
e.g do4 dateM declares do4 to 4e of t5!e date.
7+ LO"$
$his data t5!e is used to store 3aria4le length strings of u!to ) #6 si=e.
e.g descri!tion longM
>+ RA#/LO"$ RA#
$o store 4inar5 data %images,!ictures,animation,cli!s etc.& RAT or 'ON# RAT data t5!e is used. A
column 'ON# RAT t5!e can hold u!to ) #6 of 4inar5 data.
e.g image raw%)///&M
S6L Comm'-)
a. CREA$E $A6'E CommandG
Create ta4le command is used to create a ta4le in S`'. +t is a DD' t5!e of command. $he general
s5ntaJ of creating a ta4le is
Cre'ti. T'/,e)
$he s5ntaJ for creating a ta4le is
create ta4le Ata4leB %
Acolumn "B Adata t5!eB Onot nullP OuniIueP OAcolumn constraintBP,
. . . . . . . . .
70
Acolumn nB Adata t5!eB Onot nullP OuniIueP OAcolumn constraintBP,
OAta4le constraint%s&BP
&M
;or each column, a name and a data t5!e must 4e s!ecified and the column name must 4e uniIue
within the ta4le definition. Column definitions are se!arated 45 comma. *!!ercase and lowercase
letters ma(es no difference in column names, the onl5 !lace where u!!er and lower case letters
matter are strings com!arisons. A not null Constraint means that the column cannot ha3e null 3alue,
that is a 3alue needs to 4e su!!lied for that column. $he (e5word uniIue s!ecifies that no two tu!les
can ha3e the same attri4ute 3alue for this column.
Oper'tor) i S6L:
$he following are the commonl5 used o!erators in S`'
". Arithmetic O!erators 1, :, ?, ,
). Relational O!erators C, A, B, AC, BC, AB
.. 'ogical O!erators OR, AND, NO$
Arithmetic o!erators are used to !erform sim!le arithmetic o!erations.
Relational O!erators are used when two 3alues are to 4e com!ared and
'ogical o!erators are used to connect search conditions in the THERE Clause in S`'.
Co)tr'it):
Constraints are the conditions that can 4e enforced on the attri4utes of a relation. $he constraints
come in !la5 when e3er we tr5 to insert, delete or u!date a record in a relation.
". NO$ N*''
). *N+`*E
.. R+RAR\ KE\
-. ;ORE+#N KE\
W. CHECK
X. DE;A*'$
Not null ensures that we cannot lea3e a column as null. $hat is a 3alue has to 4e su!!lied for that
column.
e.g name 3archar%)W& not nullM
ni!ue constraint means that the 3alues under that column are alwa5s uniIue.
e.g RollZno num4er%.& uniIueM
"rimary #ey constraint means that a column can not ha3e du!licate 3alues and not e3en a null 3alue.
e.g. RollZno num4er%.& !rimar5 (e5M
$he main difference 4etween uniIue and !rimar5 (e5 constraint is that a column s!ecified as uniIue
ma5 ha3e null 3alue 4ut !rimar5 (e5 constraint does not allow null 3alues in the column.
$orei%n #ey is used to enforce referential integrit5 and is declared as a !rimar5 (e5 in some other
ta4le.
e.g custZid 3archar%W& references master%custZid&M
it declares custZid column as a foreign (e5 that refers to custZid field of ta4le master. $hat means we
cannot insert that 3alue in custZid filed whose corres!onding 3alue is not !resent in custZid field of
master ta4le.
&hec# constraint limits the 3alues that can 4e inserted into a column of a ta4le.
e.g mar(s num4er%.& chec(%mar(sBC/&M
$he a4o3e statement declares mar(s to 4e of t5!e num4er and while inserting or u!dating the 3alue
in mar(s it is ensured that its 3alue is alwa5s greater than or eIual to =ero.
'efault constraint is used to s!ecif5 a default 3alue to a column of a ta4le automaticall5. $his default
3alue will 4e used when user does not enter an5 3alue for that column.
e.g 4alance num4er%W& default C /M
CREA$E $A6'E student %
RollZno num4er%.& !rimar5 (e5,
Name 3archar%)W& not null,
Class 3archar%"/&,
Rar(s num4er%.& chec(%mar(sB/&,
Cit5 3archar%)W& &M
D't' Mo-i&ic'tio) i S6L
After a ta4le has 4een created using the create ta4le command, tu!les can 4e inserted into the ta4le,
or tu!les can 4e deleted or modified.
71
I"SERT St'temet
$he sim!lest wa5 to insert a tu!le into a ta4le is to use the insert statement
insert into Ata4leB O%Acolumn i, . . . , column 7B&P 3alues %A3alue i, . . . , 3alue 7B&M
+NSER$ +N$O student VA'*ES%"/",YRohanY,Y]+Y,-//,YJammuY&M
Thile inserting the record it should 4e chec(ed that the 3alues !assed are of same data t5!es as the
one which is s!ecified for that !articular column.
;or inserting a row interacti3el5 %from (e54oard& 2 o!erator can 4e used.
e.g +NSER$ +N$O student VA'*ES%2RollZno<,<2Name<,<2Class<,<2Rar(s<,<2Cit5<&M
+n the a4o3e command the 3alues for all the columns are read from (e54oard and inserted into the
ta4le student.
N(TE:- )n S*+ we can repeat or re-execute the last command typed at S*+ prompt by typin%
,-. #ey and pressin% enter/
Ro,,Co "'me C,')) M'r*) Cit=
"/" Rohan ]+ -// Jammu
"/) Aneeta Cho!ra ]++ .8/ *dham!ur
"/. awan Kumar +] )80 Amritsar
"/- Rohan +] .9X Jammu
"/W San7a5 V++ )-/ #urdas!ur
"". An7u Raha7an V+++ -.) athan(ot
6uerie):
$o retrie3e information from a data4ase we can Iuer5 the data4ases. S`' SE'EC$ statement is used
to select rows and columns from a data4ase,relation.
SELECT Comm'-
$his command can !erform selection as well as !ro7ection.
SelectionG $his ca!a4ilit5 of S`' can return 5ou the tu!les form a relation with all the attri4utes.
ro7ectionG $his is the ca!a4ilit5 of S`' to return onl5 s!ecific attri4utes in the relation.
? SE'EC$ ? ;ROR studentM command will dis!la5 all the tu!les in the relation student
? SE'EC$ ? ;ROR student THERE RollZno AC"/)M
$he a4o3e command dis!la5 onl5 those records whose RollZno less than or eIual to "/).
Select command can also dis!la5 s!ecific attri4utes from a relation.
? SE'EC$ name, class ;ROR studentM
$he a4o3e command dis!la5s onl5 name and class attri4utes from student ta4le.
? SE'EC$ count%?& AS U$otal Num4er of RecordsQ ;ROR studentM
Dis!la5 the total num4er of records with title as U$otal Num4er of RecordsQ i.e an alias
Te can also use arithmetic o!erators in select statement, li(e
? SE'EC$ RollZno, name, mar(s1)/ ;ROR studentM
? SE'EC$ name, %mar(s,W//&?"// ;ROR student THERE RollZno B "/.M
E,imi'ti. Dup,ic'te/Re-u-'t -'t'
D+S$+NC$ (e5word is used to restrict the du!licate rows from the results of a SE'EC$ statement.
e.g. SE'EC$ D+S$+NC$ name ;ROR studentM
$he a4o3e command returns
"'me
Rohan
Aneeta Cho!ra
awan Kumar
Co-itio) /')e- o ' r'.e
72
S`' !ro3ides a 6E$TEEN o!erator that defines a range of 3alues that the column 3alue must fall for
the condition to 4ecome true.
e.g. SE'EC$ RollZno, name ;ROR student THERE RollZno 6E$TENN "// AND "/.M
$he a4o3e command dis!la5s RollZno and name of those students whose RollZno lies in the range
"// to "/. %4oth "// and "/. are included in the range&.
Co-itio) /')e- o ' ,i)t
$o s!ecif5 a list of 3alues, +N o!erator is used. $his o!erator select 3alues that match an5 3alue in the
gi3en list.
e.g. SE'EC$ ? ;ROR student THERE cit5 +N %_Jammu<,<Amritsar<,<#urdas!ur<&M
$he a4o3e command dis!la5s all those records whose cit5 is either Jammu or Amritsar or #urdas!ur.
Co-itio) /')e- o P'tter
S`' !ro3ides two wild card characters that are used while com!aring the strings with '+KE o!erator.
a. !ercent%@& Ratches an5 string
4. *nderscore%Z& Ratches an5 one character
e.g SE'EC$ RollZno, name, cit5 ;ROR student THERE RollZno '+KE U@.QM
dis!la5s those records where last digit of RollZno is . and ma5 ha3e an5 num4er of characters in
front.
e.g SE'EC$ RollZno, name, cit5 ;ROR student THERE RollZno '+KE U"Z.QM
dis!la5s those records whose RollZno starts with " and second letter ma5 4e an5 letter 4ut ends with
digit ..
ORDER 1Y C,'u)e
ORDER 6\ clause is used to dis!la5 the result of a Iuer5 in a s!ecific order%sorted order&.
$he sorting can 4e done in ascending or in descending order. +t should 4e (e!t in mind that the actual
data in the data4ase is not sorted 4ut onl5 the results of the Iuer5 are dis!la5ed in sorted order.
e.g. SE'EC$ name, cit5 ;ROR student ORDER 6\ nameM
$he a4o3e Iuer5 returns name and cit5 columns of ta4le student sorted 45 name in
increasing,ascending order.
e.g. SE'EC$ ? ;ROR student ORDER 6\ cit5 DESCM
+t dis!la5s all the records of ta4le student ordered 45 cit5 in descending order.
NoteG: +f order is not s!ecifies that 45 default the sorting will 4e !erformed in ascending order.
$ROUP 1Y C,'u)e
$he #RO* 6\ clause can 4e used in a SE'EC$ statement to collect data across multi!le records
and grou! the results 45 one or more columns.
$he s5ntaJ for the #RO* 6\ clause isG
SE'EC$ column", column), ... columnZn, aggregateZfunction %eJ!ression&
;ROR ta4les
THERE conditions
#RO* 6\ column", column), ... columnZnM
aggregate_function can 4e a function such as S*R, CO*N$, RA], R+N, AV# etc.
e.g SE'EC$ name, CO*N$%?& as [Num4er of em!lo5ees[
;ROR student
THERE mar(sB.W/
#RO* 6\ cit5M
<A3I"$ C,'u)e
$he HAV+N# clause is used in com4ination with the #RO* 6\ clause. +t can 4e used in a SE'EC$
statement to filter the records that a #RO* 6\ returns.
$he s5ntaJ for the HAV+N# clause isG
SE'EC$ column", column), ... columnZn, aggregateZfunction %eJ!ression&
;ROR ta4les
THERE !redicates
#RO* 6\ column", column), ... columnZn
HAV+N# condition" ... conditionZnM
73
e.g SE'EC$ S*R%mar(s& as [$otal mar(s[
;ROR student
#RO* 6\ de!artment
HAV+N# S*R%sales& B "///M
"ote: )e,ect )t'temet c' cot'i o,= t%o)e 'ttri/ute 4%ic% 're ',re'-= pre)et i t%e .roup
/= c,'u)e+
;uctio) '('i,'/,e i S6L
S`' !ro3ide large collection of in4uilt functions also called li4rar5 functions that can 4e used directl5
in S`' statements.
". Rathematical functions
). String functions
.. Date 2 $ime functions
1+ M't%em'tic', &uctio)
Some of the commonl5 used mathematical functions are sum%& a3g%&, count%&, min%&, maJ%& etc.
e.g. SE'EC$ sum%mar(s& ;ROR studentM
dis!la5s the sum of all the mar(s in the ta4le student.
e.g. SE'EC$ min%RollZno&, maJ%mar(s& ;ROR studentM
dis!la5s smallest RollZno and highest mar(s in the ta4le student.
2+ Stri. &uctio)
$hese functions are used to deal with the string t5!e 3alues li(e
ASC++, 'OTETR, *ER, 'EN, 'E;$, R+#H$, $R+R, '$R+R, R$R+R etc.
ASCII : Returns the ASC++ code 3alue of a character%leftmost character of string&.
S5ntaJG ASC++%character&
SE'EC$ ASC++%YaY& returns 89
SE'EC$ ASC++%YAY& returns XW
SE'EC$ ASC++%Y"Y& returns -8
SE'EC$ ASC++%YA6CY& returns XW
;or *!!er character YAY to YcY ASC++ 3alue XW to 8/
;or 'ower character YAY to YcY ASC++ 3alue 89 to "))
;or digit Y/Y to Y8Y ASC++ 3alue -0 to W9
"OTE: I& o t'/,e 'me i) )peci&ie- t%e S6L u)e) Du', t'/,e 4%ic% i) ' -umm= t'/,e u)e- &or
per&ormi. oper'tio)+
LO#ER : Con3ert character strings data into lowercase.
S5ntaJG 'OTER%string&
SE'EC$ 'OTER%YS$R+N# ;*NC$+ONY& returns string function
UPPER : Con3ert character strings data into *!!ercase.
S5ntaJG *ER%string&
SE'EC$ *ER%Ystring functionY& returns S$R+N# ;*NC$+ON
LE" : Returns the length of the character string.
S5ntaJG 'EN%string&
SE'EC$ 'EN%YS$R+N# ;*NC$+ONY& returns "W
REPLACE : Re!laces all occurrences of the second string%string)& in the first string%string"& with a
third string%string.&.
S5ntaJG RE'ACE%Ystring"Y,Ystring)Y,Ystring.Y&
SE'EC$ RE'ACE%YS$R+N# ;*NC$+ONY,YS$R+N#Y,YS`'Y& returns S`' ;unction
Returns N*'' if an5 one of the arguments is N*''.
LE;T : Returns left !art of a string with the s!ecified num4er of characters counting from left.'E;$
function is used to retrie3e !ortions of the string.
S5ntaJG 'E;$%string,integer&
SE'EC$ 'E;$%YS$R+N# ;*NC$+ONY, X& returns S$R+N#
RI$<T : Returns right !art of a string with the s!ecified num4er of characters counting from
right.R+#H$ function is used to retrie3e !ortions of the string.
S5ntaJG R+#H$%string,integer&
SE'EC$ R+#H$%YS$R+N# ;*NC$+ONY, 0& returns ;*NC$+ON
LTRIM : Returns a string after remo3ing leading 4lan(s on 'eft side.%Remo3e left side s!ace or
4lan(s&
S5ntaJG '$R+R%string&
74
SE'EC$ '$R+R%Y S$R+N# ;*NC$+ONY& returns S$R+N# ;*NC$+ON
RTRIM : Returns a string after remo3ing leading 4lan(s on Right side.%Remo3e right side s!ace or
4lan(s&
S5ntaJG R$R+R% string &
SE'EC$ R$R+R%YS$R+N# ;*NC$+ON Y& returns S$R+N# ;*NC$+ON
RE3ERSE : Returns re3erse of a in!ut string.
S5ntaJG REVERSE%string&
SE'EC$ REVERSE%YS$R+N# ;*NC$+ONY& returns NO+$CN*; #N+R$S
REPLICATE : Re!eats a in!ut string for a s!ecified num4er of times.
S5ntaJG RE'+CA$E %string, integer&
SE'EC$ RE'+CA$E%Y;*NC$+ONY, .& returns ;*NC$+ON;*NC$+ON;*NC$+ON
SPACE : Returns a string of re!eated s!aces. $he SACE function is an eIui3alent of using
RE'+CA$E function to re!eat s!aces.
S5ntaJG SACE % integer& %+f integer is negati3e, a null string is returned.&
SE'EC$ %YS$R+N#Y& 1 SACE%"& 1 %Y;*NC$+ONY& returns S$R+N# ;*NC$+ON
SU1STRI"$ : Returns !art of a gi3en string.
S*6S$R+N# function retrie3es a !ortion of the gi3en string starting at the s!ecified
character%startindeJ& to the num4er of characters s!ecified%length&.
S5ntaJG S*6S$R+N# %string,startindeJ,length&
SE'EC$ S*6S$R+N#%YS$R+N# ;*NC$+ONY, ", X& returns S$R+N#
SE'EC$ S*6S$R+N#%YS$R+N# ;*NC$+ONY, 0, 0& returns ;*NC$+ON
DELETE Comm'-
$o delete the record fro a ta4le S`' !ro3ides a delete statement. #eneral s5ntaJ isG:
DE'E$E ;ROR Ata4leZnameB OTHERE AconditionBPM
e.g. DE'E$E ;ROR student THERE cit5 C _Jammu<M
$his command deletes all those records whose cit5 is Jammu.
"OTE: It )%ou,- /e *ept i mi- t%'t 4%i,e comp'ri. 4it% t%e )tri. t=pe (',ue) ,o4erc')e
'- upperc')e ,etter) 're tre'te- ') -i&&eret+ T%'t i) UM'mmuV '- U:'mmuV i) -i&&eret 4%i,e
comp'ri.+
UPDATE Comm'-
$o u!date the data stored in the data 4ase, *ODA$E command is used.
e. g. *DA$E student SE$ mar(s C mar(s 1 "//M
+ncrease mar(s of all the students 45 "//.
e. g. *DA$E student SE$ Cit5 C _*dham!ur< THERE cit5 C _Jammu<M
changes the cit5 of those students to *dham!ur whose cit5 is Jammu.
Te can also u!date multi!le columns with u!date command, li(e
e. g. *DA$E student set mar(s C mar(s 1 )/, cit5 C _Jalandhar<
THERE cit5 NO$ +N %_Jammu<,<*dham!ur<&M
CREATE 3IE# Comm'-
+n S`' we can create a 3iew of the alread5 eJisting ta4le that contains s!ecific attri4utes of the ta4le.
e. g. the ta4le student that we created contains following fieldsG
Student %RollZno, Name, Rar(s, Class, Cit5&
Su!!ose we need to create a 3iew (C)tu-et that contains RollZno,name and class of student ta4le,
then Create View command can 4e usedG
CREA$E V+ET 3Zstudent AS SE'EC$ RollZno, Name, Class ;ROR studentM
$he a4o3e command create a 3irtual ta4le %3iew& named 3Zstudent that has three attri4utes as
mentioned and all the rows under those attri4utes as in student ta4le.
Te can also create a 3iew from an eJisting ta4le 4ased on some s!ecific conditions, li(e
CREA$E V+ET 3Zstudent AS SE'EC$ RollZno, Name, Class ;ROR student THERE Cit5
AB<Jammu<M
75
$he main difference 4etween a $a4le and 3iew is that
A T'/,e is a re!ositor5 of data. $he ta4le resides !h5sicall5 in the data4ase.
A 3ie4 is not a !art of the data4aseYs !h5sical re!resentation. +t is created on a ta4le or another 3iew.
+t is !recom!iled, so that data retrie3al 4eha3es faster, and also !ro3ides a secure accessi4ilit5
mechanism.
ALTER TA1LE Comm'-
+n S`' if we e3er need to change the structure of the data4ase then A'$ER $A6'E command is
used. 65 using this command we can add a column in the eJisting ta4le, delete a column from a ta4le
or modif5 columns in a ta4le.
A--i. ' co,um
$he s5ntaJ to add a column isG:
A'$ER $A6'E ta4leZname
ADD columnZname datat5!eM
e.g A'$ER $A6'E student ADD%Address 3archar%./&&M
$he a4o3e command add a column Address to the ta4le atudent.
+f we gi3e command
SE'EC$ ? ;ROR studentM
$he following data gets dis!la5ed on screenG
Ro,,Co "'me C,')) M'r*) Cit= A--re))
"/" Rohan ]+ -// Jammu
"/) Aneeta Cho!ra ]++ .8/ *dham!ur
"/. awan Kumar +] )80 Amritsar
"/- Rohan +] .9X Jammu
"/W San7a5 V++ )-/ #urdas!ur
"". An7u RAha7an V+++ -.) athan(ot
"ote t%'t 4e %'(e :u)t '--e- ' co,um '- t%ere 4i,, /e o -'t' u-er t%i) 'ttri/ute+ UPDATE
comm'- c' /e u)e- to )upp,= (',ue) / -'t' to t%i) co,um+
Remo(i. ' co,um
A'$ER $A6'E ta4leZname
DRO CO'*RN columnZnameM
e.g A'$ER $A6'E Student
DRO CO'*RN AddressM
$he column Address will 4e remo3ed from the ta4le student.
DROP TA1LE Comm'-
Sometimes 5ou ma5 need to dro! a ta4le which is not in use. DRO $A6'E command is used to
Delete , dro! a ta4le !ermanentl5. +t should 4e (e!t in mind that we can not dro! a ta4le if it contains
records. $hat is first all the rows of the ta4le ha3e to 4e deleted and onl5 then the ta4le can 4e
dro!!ed. $he general s5ntaJ of this command isG:
DRO $A6'E Ata4leZnameBM
e.g DRO $A6'E studentM
$his command will remo3e the ta4le student
76
Questions : Database and SQL : 1 mark questions
Q1 Write SQL queries to perform the following based on the table !"D#$%
ha&ing 'elds as (prod)id* prod)name* quantit+* unit)rate* pri,e* ,it+-
i. #isplay those records from table 'A+#(%7 where prodBid is more than 100.
ii. 5ist records from table 'A+#(%7 where prodBname is CAlmirahD
iii. 5ist all those records whose price is between !00 and "00.
i<. #isplay the product names whose price is less than the a<erage of price.
<. )how the total number of records in the table 'A+#(%7.
Q.. #e$ne the terms-
i. #atabase Abstraction
ii. #ata inconsistency
iii. %onceptual le<el of database implementationEabstraction
i<. 'rimary 8ey
<. %andidate 8ey
<i. Aelational Algebra
<ii #omain
/ 0arks Questions SQL
Q1 >rite )?5 commands for 9i: to 9<iii: on the basis of relations gi<en below-
1""2S
book)id 1ook)name author)name ublishers ri,e
%+pe qt+
,0001 5et us % )anFay mu,harFee G'H 4"0
%omp 1"
p0001 Ienuine J. Ku,hi FLA)7 '(H5. M""
Fiction !4
m0001 Kastering c&& 8anet,ar G'H 16"
%omp 60
n000! Nc&& ad<ance '. 'urohit 7#* !"0
%omp 4"
,000! .ear to heart )anFee< FLA)7 '(H5. 3"0
Fiction 30
3SS#4D
6oo(Z+D `t5Z+ssued
'/) ".
'/- W
'/W )"
i. 7o show the boo,s of FLA)7 '(H5 'ublishers written by '.'urohit.
ii. 7o display cost of all the boo,s written for FLA)7 '(H5.
iii. #epreciate the price of all boo,s of G'H publishers by "O.
i<. 7o display the H++8B.AKG1price of the boo,s whose more than 3 copies ha<e
been issued.
<. 7o show total cost of boo,s of each type.
<i. 7o show the detail of the most costly boo,.
62. Trite S`' commands for %a& to %f& and write out!ut for %g& on the 4asis of ROD*C$S relation
gi3en 4elowG
PRODUCT TA1LE
PCODE P"AME COMPA"Y PRICE STOC! MA"U;ACTURE #ARRA"TY
//" $V 6' "//// )// "):JAN:)//0 .
77
//) $V SON\ ")/// "W/ ).:RAR:)//9 -
//. C 'ENOVO .8/// "// /8:AR:)//0 )
//- C CORA` .0/// ")/ )/:J*N:)//8 )
//W HAND\CAR SON\ "0/// )W/ ).:RAR:)//9 .
a: 7o show details of all '%s with stoc, more than 110.
b: 7o list the company which gi<es warranty for more than ! years.
c: 7o $nd stoc, <alue of the H'5 company where stoc, <alue is sum of the products of
price and stoc,.
d: 7o show number of products from each company.
e: 7o count the number of 'A+#(%7) which shall be out of warranty on !0P.+NP!010.
f: 7o show the 'A+#(%7 name which are within warranty as on date.
g:. Ii<e the output of following statement.
9i: )elect %+(.79distinct company: from 'A+#(%7.
9ii: )elect KA09price:from 'A+#(%7 where >AAAA.72Q;3
5nswers: 1 mark questions
Q1
Ans i- select 4 from product where prodBid R 1003
Ans ii- select 4 from product where prodBname ; CAlmirahD3
Ans iii- select 4 from product where price between !00 and "003
Ans i<- select prodBname from product where price Q a<g9price:3
Ans <- select count94: from product3
Q.. #e$ne the terms-
i. Database 5bstra,tion
Ans- #atabase system pro<ides the users only that much information that is
reSuired
by them1 and hides certain details li,e1 how the data is stored and maintained in
database at hardware le<el. 7his conceptEprocess is #atabase abstraction.
ii. Data in,onsisten,+
Ans- >hen two or more entries about the same data do not agree i.e. when one of
them stores the updated information and the other does not1 it results in data
inconsistency in the database.
iii. $on,eptual le&el of database implementation6abstra,tion
Ans- Lt describes what data are actually stored in the database. Lt also describes
the
relationships eTisting among data. At this le<el the database is described logically
in
terms of simple dataPstructures.
i&. rimar+ 2e+
Ans - Lt is a ,eyEattribute or a set of attributes that can uniSuely identify tuples
within the relation.
&. $andidate 2e+
Ans - All attributes combinations inside a relation that can ser<e as primary ,ey
are candidate ,ey as they are candidates for being as a primary ,ey or a part of it.
&i. !elational 5lgebra
Ans - Lt is the collections of rules and operations on relations9tables:. 7he <arious
operations are selection1 proFection1 %artesian product1 union1 set diUerence and
intersection1 and Foining of relations.
&ii. Domain
Ans - it is the pool or collection of data from which the actual <alues appearing in a
gi<en column are drawn.
5nswers: / 0arks Questions:
Q1.
78
Ans i- select 4 from boo,s where publishers;DFLA)7 '(H5D
Ans ii- select sum9price4Sty: from boo,s where publishers;DFLA)7 '(H5D3
Ansiii- update boo,s set price;priceP0."4price where publishers;DG'HD3
Ans i<- select H++8B.AKG1price from boo,s1 issued where
boo,s.boo,Bid;issued.boo,Bid and SuantityBissuedR33
Ans <- select sum9price4Sty: from boo,s group by type3
Ans <i- select 4 from boo,s where price;9select maT9price: from boo,s::3
Q..
Ans a- select 4 from products where pname;D7ND and stoc,R1103
Ans b- select company from products where warrantyR!3
Ans c- select sum9price4stoc,: from 'A+#(%7) where company;DH'5D3
Ans d- select company1%+(.794: from products group by company3
Ans e- select count94: from products where 9C!0P.+NP!010DP manufacture:E36"Rwarranty3
Ans f- select pname from products where 9sysdateP manufacture:E36"Qwarranty3
Ansg 9i:- 4
Ans9ii:- 3000
Some pra,ti,e questions from D't'/')e '- S6L :
2 m'r*) Due)tio)
". That is relationF That is the difference 4etween a tu!le and an attri4uteF
). Define the following terminologies used in Relational Alge4raG
%i& selection %ii& !ro7ection %iii& union %i3& Cartesian !roduct
.. That are DD' and DR'F
-. Differentiate 4etween !rimar5 (e5 and candidate (e5 in a relationF
W. That do 5ou understand 45 the terms C'r-i',it= and De.ree of a relation in relational
data4aseF
X. Differentiate 4etween DD' and DR'. Rention the ) commands for each caterog5.
> m'r*) Due)tio)
".
T'/,e : Sc%oo,1u)
Rtno AreaZo3ered Ca!acit5 Noofstudents Distance $rans!orter Charges
" Vasant (un7 "// ")/ "/ Shi3amtra3els "/////
) Hau= Khas 0/ 0/ "/ Anand tra3els 0W///
. itam!ura X/ WW ./ Anand tra3els X////
- Rohini "// 8/ .W Anand tra3els "/////
W \amuna Vihar W/ X/ )/ 6halla Co. WW///
X Krishna Nagar 9/ 0/ ./ \ada3 Co. 0////
9 Vasundhara "// ""/ )/ \ada3 Co. "/////
0 aschim Vihar -/ -/ )/ S!eed tra3els WW///
8 Sa(et ")/ ")/ "/ S!eed tra3els "/////
"/ Jan( uri "// "// )/ Kisan $ours 8W///
%4& $o show all information of students where ca!acit5 is more than the no of student in order of
rtno.
%c& $o show areaZco3ered for 4uses co3ering more than )/ (m., 4ut charges less then 0////.
%d& $o show trans!orter wise total no. of students tra3eling.
%e& $o show rtno, areaZco3ered and a3erage cost !er student for all routes where a3erage cost
!er student is : charges,noofstudents.
%f& Add a new record with following dataG
%"", U Roti 4aghQ,.W,.),"/,Q (isan tours U, .W///&
%g& #i3e the out!ut considering the original relation as gi3enG
%i& select sum%distance& from school4us where trans!orterC U \ada3 tra3elsQM
%ii& select min%noofstudents& from school4usM
%iii& select a3g%charges& from school4us where trans!orterC U Anand tra3elsQM
79
%3& select distinct trans!orter from school4usM
2+
TA1LE : $RADUATE
S+"O "AME STIPE"D SU1MECT A3ERA$E DI3+
" KARAN -// H\S+CS X0 +
) D+TAKAR -W/ COR. Sc. X0 +
. D+V\A .// CHER+S$R\ X) +
- REKHA .W/ H\S+CS X. +
W ARJ*N W// RA$HS 9/ +
X SA6+NA -// CEHR+S$R\ WW ++
9 JOHN )W/ H\S+CS X- +
0 RO6ER$ -W/ RA$HS X0 +
8 R*6+NA W// COR. Sc. X) +
"/ V+KAS -// RA$HS W9 ++
A'B 'ist the names of those students who ha3e o4tained D+V " sorted 45 NARE.
A/B Dis!la5 a re!ort, listing NARE, S$+END, S*6JEC$ and amount of sti!end recei3ed in a
5ear assuming that the S$+END is !aid e3er5 month.
AcB $o count the num4er of students who are either H\S+CS or COR*$ER SC graduates.
A-B $o insert a new row in the #RAD*A$E ta4leG "",QKAJO'Q, .//, Ucom!uter scQ, 9W, "
AeB #i3e the out!ut of following sIl statement 4ased on ta4le #RAD*A$EG
%i& Select R+N%AVERA#E& from #RAD*A$E where S*6JEC$CQH\S+CSQM
%ii& Select S*R%S$+END& from #RAD*A$E THERE di3C)M
%iii& Select AV#%S$+END& from #RAD*A$E where AVERA#EBCXWM
%i3& Select CO*N$%distinct S*6DJEC$& from #RAD*A$EM
A&B Assume that there is one more ta4le #*+DE in the data4ase as shown 4elowG
T'/,e: $UIDE
.B That will 4e the out!ut of the following Iuer5G
SE'EC$ NARE, ADV+SOR ;ROR #RAD*A$E,#*+DE THERE S*6JEC$C RA+NAREAM
0+ Trite S`' command for %i& to %3ii& on the 4asis of the ta4le SOR$S
T'/,e: SPORTS
%a& Dis
!la 5
the
names of the students who ha3e grade _C< in either #ame" or #ame) or 4oth.
%4& Dis!la5 the num4er of students getting grade _A< in Cric(et.
%c& Dis!la5 the names of the students who ha3e same game for 4oth #ame" and #ame).
%d& Dis!la5 the games ta(en u! 45 the students, whose name starts with _A<.
%e& Assign a 3alue )// for Rar(s for all those who are getting grade _6< or grade _A< in 4oth #ame"
and #ame).
%f& Arrange the whole ta4le in the al!ha4etical order of Name.
MAI"AREA AD3ISOR
H\S+CS V+NOD
COR*$ER SC A'OK
CHER+S$R\ RAJAN
RA$HERA$+CS RAHESH
Student NO Class Name #ame" #rade #ame) #rade)
"/ 9 Sammer Cric(et 6 Swimming A
"" 0 Su7it $ennis A S(ating C
") 9 Kamal Swimming 6 ;oot4all 6
". 9 Venna $ennis C $ennis A
"- 8 Archana 6as(et4all A Cric(et A
"W "/ Ar!it Cric(et A Atheletics C
80
%g& Add a new column named _Rar(s<.
2+Trite S`' command for %i& to %3ii& on the 4asis of the ta4le Em!lo5ees 2 Em!Salar5
T'/,e: Emp,o=ee)
Empi- ;ir)t'me L')t'me A--re)) Cit=
/"/ Ra3i Kumar Ra7 nagar #c6
"/W Harr5 Taltor #andhi nagar #c6
"W) Sam $ones .. Elm St. aris
)"W Sarah Ac(erman --/ *.S. ""/ *!ton
)-- Ranila Sengu!ta )- ;riends street New Delhi
.// Ro4ert Samuel 8 ;ifth Cross Tashington
..W Ritu $ondon Shastri Nagar #c6
-// Rachel 'ee ")" Harrison St. New \or(
--" eter $hom!son "" Red Road aris
T'/,e: EmpS','r=
Empi- S','r= 1ee&it) De)i.'tio
/"/ 9W/// "W/// Ranager
"/W XW/// "W/// Ranager
"W) 0//// )W/// Director
)"W 9W/// ")W// Ranager
)-- W//// ")/// Cler(
.// -W/// "//// Cler(
..W -//// "//// Cler(
-// .)/// 9W// Salesman
--" )0/// 9W// salesman
Trite the S6L comm'-) for the following G
%i& $o show firstname,lastname,address and cit5 of all em!lo5ees li3ing in !aris
%ii& $o dis!la5 the content of Em!lo5ees ta4le in descending order of ;irstname.
%iii& $o dis!la5 the firstname,lastname and total salar5 of all managers from the ta4les
Em!lo5ee and em!salar5 , where total salar5 is calculated as salar514enefits.
%i3& $o dis!la5 the maJimum salar5 among managers and cler(s from the ta4le Em!salar5.
#i3e the Output of following S`' commandsG
%i& Select firstname,salar5 from em!lo5ees ,em!salar5 where designation C _Salesman< and
Em!lo5ees.em!idCEm!salar5.em!idM
%ii& Select count%distinct designation& from em!salar5M
%iii& Select designation, sum%salar5& from em!salar5 grou! 45 designation ha3ing count%?& B)M
%i3& Select sum%4enefits& from em!salar5 where designation C<Cler(<M
81
1oo,e' A,.e/r'
6oolean alge4ra is an alge4ra that deals with 6oolean 3alues%%$R*E and ;A'SE& . +n dail5 life we
normall5 as(s Iuestions li(e should + go for sho!!ing or notF Should + watch $V or notF etc.
$he answers to these Iuestions will 4e either 5es or no, true or false, " or /, which are truth 3alues.
Trut% t'/,e:
$ruth ta4le is a ta4le, which re!resents all the !ossi4le 3alues of logical 3aria4les,statements along
with all the !ossi4le results of gi3en com4inations of 3alues.
Lo.ic', Oper'tor)G
'ogical o!erators are deri3ed from the 6oolean alge4ra, which is the mathematical re!resentation of
the conce!ts without going into the meaning of the conce!ts.
1+ "OT Oper'tordO!erates on single 3aria4le. +t gi3es the com!lement 3alue of
3aria4le.
X X
/ "
" /
2+ OR Oper'tor :+t is a 4inar5 o!erator and denotes logical Addition o!eration and is re!resented
45 Q1Q s5m4ol
/ 1 / C /
/ 1 " C "
" 1 / C "
" 1 " C "
] \ ]1\
/ / /
/ " "
" / "
" " "
.. A"D Oper'tor > AND O!erator !erforms logical multi!lications and s5m4ol is %.& dot.
/./C/
/."C/
"./C/
"."C"
$ruth ta4leG
] \ ].\
/ / /
/ " /
" / /
" " "
1')ic Lo.ic $'te)
A gate is sim!l5 an electronic circuit, which o!erates on one or more signals to !roduce an out!ut
signal. #ates are digital circuits 4ecause the in!ut and out!ut signals are either low %/& or high %"&.
#ates also called logic circuits.
$here are three t5!es of logic gatesG
". +n3erter %NO$ gate&
). OR gate
.. AND gate
1+ "OT .'te : $his gate ta(es one in!ut and gi3es a single out!ut. $he s5m4ol of this
logic gate is
82
$his circuit is used to o4tain the com!liment of a 3alue.
+f ] C /, then ]< C ".
$he truth ta4le for NO$ gate is G
2+ OR .'te G $he OR gate has two or more in!ut signals 4ut onl5 one out!ut signal if an5 of the
in!ut signal is "%high& the out!ut signal is "%high&.
Trut% T'/,e &or T4o Iput OR .'te i) :
] \ ;
/ / /
/ " "
" / "
" " "
$he circuit diagram of two in!uts OR gate isG:

A"D .'te $he AND gate ha3e two or more than two in!ut signals and !roduce an out!ut signal.
Then all the in!uts are "%High& then the out!ut is " otherwise out!ut is / onl5.
] \ ;C].\
/ / /
/ " /
" / /
" " "
Circuit diagram of $wo in!ut AND gate
1')ic po)tu,'te) o& 1oo,e' A,.e/r':
6oolean alge4ra consists of fundamental laws that are 4ased on theorem of 6oolean alge4ra. $hese
fundamental laws are (nown as 4asic !ostulates of 6oolean alge4ra. $hese !ostulates states 4asic
relations in 4oolean alge4ra, that followG
+ +f ] DC / then JC" and +f ]DC" then JC/
++ OR relations%logical addition&
/ 1 / C /
/ 1 " C "
" 1 / C "
] ]
/ "
" /
] \ c ;C]1\1c
/ / / /
/ / " "
/ " / "
/ " " "
" / / "
" / " "
" " / "
" " " "
83
" 1 " C "
+++ AND relations %logical multi!lication&
/./C/
/."C/
"./C/
"."C"
+V Com!lement Rules
/ C ", " C /
Pricip', o& Du',it=
$his !rinci!al states that we can deri3e a 6oolean relation from another 6oolean relation 45
!erforming sim!le ste!s. $he ste!s areG:
". Change each AND%.& with an OR%1& sign
). Change each OR%1& with an AND%.& sign
.. Re!lace each / with " and each " with /
e.g
/1/C/ then dual is "."C"
"1/C" then dual is /."C/
1')ic t%eorem o& 1oo,e' ',.e/r'
6asic !ostulates of 6oolean alge4ra are used to define 4asic theorems of 6oolean alge4ra that
!ro3ides all the tools necessar5 for mani!ulating 6oolean eJ!ression.
". ro!erties of / and "
%a& /1]C]
%4& "1]C"
%c& /.]C/
%d& ".]C]
). +ndem!otence 'aw
%a& ]1]C]
%4& ].]C]
.. +n3olution 'aw
%]& C ]
-. Com!lementarit5 'aw
%a& ] 1 ]C"
%4& ]. ]C/
W. Commutati3e 'aw
%a& ]1\C\1]
%4& ].\C\.]
X. Associati3e 'aw
%a& ]1%\1c&C%]1\&1c
%4& ]%\c&C%]\&c
9. Distri4uti3e 'aw
%a& ]%\1c&C]\Z]c
%4& ]C\cC%]1\&%]1c&
0. A4sor!tion 'aw
%a& ]1]\C ]
%4& ]%]1\&C]
Some other rules of 6oolean alge4ra
]1]\C]1\
Demor.'V) T%eorem
A mathematician named DeRorgan de3elo!ed a !air of im!ortant rules regarding grou!
com!lementation in 6oolean alge4ra.
84
Demor.'V) ;ir)t T%eorem:
$his rule states that the com!liment of OR of two o!erands is same as the AND of the com!liments of
those o!erands.
Rathematicall5 it can 4e written asG:

Demor.'V) Seco- T%eorem:
$his rule states that the com!liment of AND of two o!erands is same as the OR of the com!liments of
those o!erands.
Rathematicall5 it can 4e written asG:
Deri('tio o& 1oo,e' eGpre))io:-
Miterm) '- M'Gterm)
Consider two 4inar5 3aria4les ! and y com4ined with an AND o!eration.
J<5<, J<5, J5<, J5
Each of these four AND terms re!resents one of the distinct areas in the Venn diagram and is called a
minterm or standard product.
Consider two 4inar5 3aria4les ! and y com4ined with an OR o!eration.
J<1 5<, J<1 5, J 1 5<, J 1 5
Each of these four OR terms re!resents one of the distinct areas in the Venn diagram and is called a
ma!term or standard sum.
n Varia4les can 4e com4ined to form )
n
minterms or maJterms.
Miterm) '- M'Gterm) &or T%ree 1i'r= 3'ri'/,e)
Miterm) M'Gterm)
G = [ Term De)i.'tio Term De)i.'tio
/ / / J<5<=< m
/
J151= M
/
/ / " J<5<= m
"
J151=< M
"
/ " / J<5=< m
)
J15<1= M
)
/ " " J<5= m
.
J15<1=< M
.
" / / J5<=< m
-
J<151= M
-
" / " J5<= m
W
J<151=< M
W
" " / J5=< m
X
J<15<1= M
X
" " " J5= m
9
J<15<1=< M
9
85
A
B
A
B

A 6oolean function ma5 4e re!resented alge4raicall5 from a gi3en truth ta4le 45 forming a
minterm for each com4ination of the 3aria4les that !roduces a " in the function and then
ta(ing the OR of all those terms.
;uctio) o& T%ree 3'ri'/,e)
0 y 1 ;uctio $2 ;uctio $3
/ / / / /
/ / " " /
/ " / / /
/ " " / "
" / / " /
" / " / "
" " / / "
" " " " "
$"C !%y%& 1 !y%&% 1 !y& C m
"
1 m
-
1 m
9
$'C !%y& 1 !y%& 1 !y&% 1 !y& C m
.
1 m
W
1 m
X
1 m
9
$he com!lement of a 6oolean function ma5 4e read from the truth ta4le 45 forming a minterm
for each com4ination that !roduces a / in the function and then ORing those terms.
$"% C !%y%&% 1 !%y&% 1 !%y& 1 !y%& 1 !y&%
EG'mp,e: EJ!ress the 6oolean function $%A,(,C& C A( 1 C as a sum of minterms.
Ste! " > Each term must contain all 3aria4les
A( C A(%C 1 C%& C A(C 1 A(C%
C C C%A 1 A%& C AC 1 A%C
C AC%( 1 (%& 1 A%C%( 1 (%&
C A(C 1 A(%C 1 A%(C 1 A%(%C
Ste! ) > OR all new terms, eliminating du!licates
$%A,(,C& C A%(%C 1 A%(C 1 A(%C 1 A(C% 1 A(C
C m
"
1 m
.
1 m
W
1 m
X
1 m
9
C %", ., W, X, 9&
EG'mp,e: EJ!ress the 6oolean function $%!,y,&& C !%y 1 !& as a !roduct of maJterms.
Ste! " > Con3ert the function into OR terms using the distri4uti3e law
$%!,y,&& C %!%y 1 !&%!%y 1 &&
C %! 1 !%&%y 1 !&%!% 1 &&%y 1 &&
C %y 1 !&%!% 1 &&%y 1 &&
Ste! ) > Each term must contain all 3aria4les
y 1 ! C y 1 ! 1 &&% C %! 1 y 1 &&%! 1 y 1 &%&
!% 1 & C !% 1 & 1 yy% C %!% 1 y 1 &&%!% 1 y% 1 &&
y 1 & C y 1 & 1 !!% C %! 1 y 1 &&%!% 1 y 1 &&
ste! . > AND all new terms, eliminating du!licates
$%!,y,&& C %! 1 y 1 &&%! 1 y 1 &%&%!% 1 y 1 &&%!% 1 y% 1 &&
C % M
/
M
"
M
-
M
X
&
C %/, ", -, X&
Co(er)io /et4ee C'oic', ;orm)
$he com!lement of a function eJ!ressed as the sum of minterms eIuals the sum of minterms missing
from the original function. $his is 4ecause the original function is eJ!ressed 45 those minterms that
ma(e the function eIual to ", whereas its com!lement is a " for those minterms that the function is /.
EJam!le G $ %A,(,C& C %/, ), -, X, 9&
$%%A,(,C& C %", ., W& C m
"
1 m
.
1 m
W
$a(e the com!lement of $% 45 DeRorgan<s theorem to o4tain $ in a different formG
$%A,(,C& C %m
"
1 m
.
1 m
W
&< C %m
"
< m
.
< m
W
<& C M
"
M
.
M
W
C %", ., W&
$o con3ert from one canonical form to the other, interchange the s5m4ols and , and list
those num4ers missing from the original form.
86
St'-'r- ;orm)
$he two canonical forms of 6oolean alge4ra are 4asic forms that one o4tains from reading a
function from the truth ta4le. 65 definition, each minterm or maJterm must contain all
3aria4les in either com!lemented or uncom!lemented form.
Another wa5 to eJ!ress 6oolean functions is in standard form. +n this configuration, the terms
that form the function ma5 contain one, two, or an5 num4er of literals.
$here are two t5!es of standard formsG the sum of products and the product of sums.
$he sum of !roducts is a 6oolean function containing AND terms, called product terms, of one
or more literals each. $he sum denotes the ORing of these terms.
EJam!leG $" C y% 1 !y 1 !%y&%
$he !roduct of sums is a 6oolean eJ!ression containing OR terms, called sum terms. Each
term ma5 ha3e one or more literals. $he !roduct denotes the ANDing of these terms.
EJam!leG $' C !%y% 1 &&%!% 1 y 1 &% 1 w&
A 6oolean function ma5 also 4e eJ!ressed in nonstandard form.
EJam!leG $) C %A( 1 C*&%A%(% 1 C%*%&
MiimiH'tio o& 1oo,e' eGpre))io):-
After o4taining SO and OS eJ!ressions, the neJt ste! is to sim!lif5 the 6oolean eJ!ression.
$here are two methods of sim!lification of 6oolean eJ!ressions.
1+ A,.e/r'ic Met%o-
2+ !'r'u.% M'p :
1+A,.e/ric met%o-:$his method ma(es use of 6oolean !ostulates, rules and theorems to sim!lif5 the
eJ!ression. Z Z Z Z
EG'mp,e++1. Sim!lif5 A6CD 1 A6CD 1A6CD 1A6CD
Z Z Z Z
Solution:: A6CD 1 A6CD 1A6CD 1A6CD
Z Z Z
CA6C%D1D& 1A6C%D1D&
Z Z
CA6C." 1 A6C." %D1DC"&
Z
CAC%616&
CAC." CAC
Z Z Z Z Z Z Z Z
EG'mp,e++2+. Reduce.. ]e\ece1]e\ce1]\ece1]\ce
Z Z Z Z Z Z Z
SolutionK ] \ c1]\c1]\c1]\c
Z Z Z Z Z Z Z
C] %\ c 1\c & 1]%\ c 1\c &
Z Z Z Z Z
C] %c %\1\&& 1]%c% \1\&&
Z Z Z Z
C]%c."& 1]%c."& %\1\C"&
Z Z Z
C]c1]c
Z Z
Cc%]1]&
Z
Cc."
Z
Cc
2+ U)i. !'r'u.% M'p :
A Karnaugh ma! is gra!hical dis!la5 of the fundamental !roducts in a truth ta4le.
;or eJam!leG
ut a " in the 4oJ for an5 minterm that a!!ears in the SO eJ!ansion.
6asic idea is to co3er the ,'r.e)t '-:'cet 4loc(s 5ou can whose side length is some !ower of ).
6loc(s can Uwra! aroundQ the edges.
87
;or eJam!le, the first K:ma! here re!resents & ' xy xy x y y x + = + = . %since
515<C"&
$he second K:ma!, similarl5, shows & ' xy xy x x y y + = + =
Remem4er, grou! together ad7acent cells of "s, to form largest !ossi4le rectangles of si=es that
are !owers of ).
Notice that 5ou can o3erla! the 4loc(s if necessar5.
Sum (f "roducts Reduction usin% 4- 5ap
* +n SO reduction each sIuare re!resent a minterm.
* Su!!ose the gi3en function is f%A,6,C,D& C %/,),9,0,"/,"W&
* Enter the " in the corres!onding !osition for each minterm of the gi3en function.
* Now the K:ma! will 4e as follows
CD C<D< C<D CD CD<
A1
A<6<
A<6
A6
A<6
;or reducing the eJ!ression first mar( Octet, `uad, air then single.
* airG $wo ad7acent "<s ma(es a !air.
* `uadG ;our ad7acent "<s ma(es a Iuad.
* OctetG Eight ad7acent "<s ma(es an Octet.
* air remo3es one 3aria4le.
* `uad remo3es two 3aria4les.
* Octet remo3es three 3aria4les.
Reduction of eJ!ressionG Then mo3ing 3erticall5 or hori=ontall5 in !air or a Iuad or an octet it can 4e
o4ser3ed that onl5 one 3aria4le gets changed that can 4e eliminated directl5 in the eJ!ression.
;or EJam!le
88
1 10
11 @
1 ?
12
1 17
10 12
>
1 8
7 2
1 2
0 1
1 0
+n the a4o3e EJ
Step 1 : I K Ra! while mo3ing from m
9
to m
"W
the 3aria4le A is changing its state Hence it can 4e
remo3ed directl5, the solution 4ecomes 1+CD W 1CD+ $his can 4e continued for all the !airs, `uads,
and Octets.
Step 2 : +n K ma! while mo3ing from m
/
to m
0
and m
)
to m
"/
the 3aria4le A is changing its state.
Hence 1V can 4e ta(en similarl5 while mo3ing from m
/
to m
)
and m
0
to m
"/
the 3aria4le C is changing
its state. Hence DV can 4e ta(enM the solution 4ecomes 1V+DV
T%e )o,utio &or '/o(e eGpre))io u)i. ! m'p i) 1CD 5 1VDV+
2 M'r*) 6ue)tio)
1. Trite the eIui3alent 6oolean EJ!ression for the following 'ogic Circuit
2. Trite the eIui3alent 6oolean EJ!ression ; for the following circuit diagram G
0. Trite the eIui3alent 6oolean EJ!ression ; for the following circuit diagram G
2. Con3ert the following 6oolean eJ!ression into its eIui3alent Canonical Sum of roduct
;orm%%SO&
%]<1\1c<&.%]<1\1c&.%]<1\<1c&.%]<1\<1c<&
7. Con3ert the following 6oolean eJ!ression into its eIui3alent Canonical roduct of Sum form %OS&G
A.6<.C 1 A<.6.C 1A<.6.C<
>. Draw a 'ogical Circuit Diagram for the following 6oolean eJ!ressionG
A.%61C<&
8. Trite the eIui3alent 6oolean EJ!ression ; for the following circuit diagramG
?. ro3e that ]\1\c1\c<C\ alge4raicall5
@. EJ!ress the ;%],c&C]1]<c into canonical SO form.
10. Trite the eIui3alent 6oolean EJ!ression for the following 'ogic Circuit.
89
11. +nter!ret the following logical circuit as 6oolean eJ!ression
12. Design %A16&.%C1D& using NAND #ate
10. ro3e J<.5<15.= C J<5=1J<5=<1J5=1J<5= alge4raicall5.
12. ro3e that %a<14<&%a<14&%a14<&Ca<4<.
17. A 6oolean function ; defined on three in!ut 3aria4le ],\,c is " if and onl5 if the num4er of "%One&
in!ut is odd %e.g. ; is " if ]C",\C/,cC/&. Draw the truth ta4le for the a4o3e function and eJ!ress it in
canonical sum of !roduct form.
0 M'r*) 6ue)tio) : 1oo,e' A,.e/r'
2. +f ;%a,4,c,d&Cf%/,),-,W,9,0,"/,"),".,"W&, o4tain the sim!lified form using K:Ra!.
3. +f ;%a,4,c,d& Cf%/,.,-,W,9,0,8,"","),".,"W&, o4tain the sim!lified form using KRa!
6. O4tain a sim!lified form for a 4oolean eJ!ression
;%*,V,T,c&C g %/,",.,W,X,9,"/,"-,"W&
7. Reduce the following 4oolean eJ!ression using K:Ra!
;%A,6,C,D& C f%W,X,9,0,8,"),".,"-,"W&
A)4er): 2 M'r*) 6ue)tio) : 1oo,e' A,.e/r'
1+ ;%,`&C%Y1`&.%1`Y&
2+ A<61A61A6<
0+ ]<%\<1c&
2+ ;%], \, c& Cg%- , W , X , 9&
Cf%/, " , ) , .&
C ]<. \<. c< 1 ]<. \<. c 1 ]<. \. c< 1 ]<. \. c
7. A.6<.C 1 A<.6.C 1A<.6.C< C g %/,",-,X,9& ORC%A161C&.%A161C<&.%A<161C&.%A<16<1C&.%A<16<1C<&
>+
8. %A1C&%A<16&
?. ]\1\c1\c<C\
'.H.S.
]\1\c1\c<
C ]\1\%c1c<&
C]\1\C\%]1"&
C\."
C\CRHS
@. ;%],c&C]1]<c C]%\1\<&1]<%\1\<&c
C]\1]\<1]<\c1]<\<c
90
C]\%c1c<&1]\<%c1c<&1]<\c1]<\<c
C]\c1]\c<1]\<c1]\<c<1]<\c1]<\<c
10. ]\1%]\&<1]<
11. a414<c1c<e<
12.
10. '.H.S.C J<5 15.=
CJ<5."1".5.= CJ<5%=1=<&1%J1J<&5.=
CJ<5=1J<5=<1J5=1J<5= CRHS
12. 'HSC%a<14<&%a<14&%a14<&
C%a<a<1a<41a<4<14<4&%a14<&
C%a<1a<41a<4<1/&%a14<&
Caa<1a<4<1aa<41a<44<1a<a4<1a<4<4<
C/1a<4<1/1&1/1/1a<4<Ca<4<CRHS
17.
] \ c ;
/ / / /
/ / " "
/ " / "
/ " " /
" / / "
" / " /
" " / /
" " " "
Canonical +,-
./0%1./%01./%0%1./0
A)4er): 0 m'r*) 6ue)tio)
1oo,e' A,.e/r'
2. ;%a,4,c,d&Cf%/,),-,W,9,0,"/,"),".,"W&
;%a,4,c,d&C6"16)16.
6"Cm/1m-1m")1m0CCc<d<
6)CmW1m91m".1m"WC4d
6.Cm/1m)1m01m"/C4<d<
;%a,4,c,d&Cc<d<14d14<d<
2. ;%a,4,c,d& Cf%/,.,-,W,9,0,8,"","),".,"W&

;%a,4,c,d&C6"16)16.16-
6"Cm/1m-1m")1m0Cc<d<
6)CmW1m91m".1m"WC4d
6.Cm".1m"W1m81m""Cad
6-Cm.1m91m"W1m""Ccd
91
;%a,4,c,d&Cc<d<14d1ad1cd
6. ;%*,V,T,c&C g %/,",.,W,X,9,"/,"-,"W&
;%*,V,T,c&C6".6).6..6-
6"CR/.R"C%*1V1T&
6)CR".R..RW.R9C%*1c<&
6.CR9.RX.R"W.R"-C%V<1T<&
6-CR"-.R"/C%*<1T<1c&
;%*,V,T,c&C %*1V1T&.%*1c<&.%V<1T<&.%*<1T<1c&
2. ;%A,6,C,D& C f%W,X,9,0,8,"),".,"-,"W&
;%A,6,C,D&C6"16)16.
6"CR/1R)1R01R"/C6<D<
6)CR/1R"1R-1RWCA<C<
6.CR01R81R""1R"/CA6<
;%A,6,C,D&C6<D<1A<C<1A6<
92
COMMU"ICATIO" A"D "ET#OR! CO"CEPTS
Poit) to remem/er
"et4or*
$he collection of interconnected com!uters is called a com!uter networ(.
$wo com!uters are said to 4e interconnected if the5 are ca!a4le of sharing and eJchanging
information.

U)'.e) o& "et4or*i.:
Resource Sharing
Relia4ilit5
Cost ;actor
Communication Redium
Resource Sharing means to ma(e all !rograms, data and !eri!herals a3aila4le to an5one on the
networ( irres!ecti3e of the !h5sical location of the resources and the user.
Re,i'/i,it= means to (ee! the co!5 of a file on two or more different machines, so if one of them is
una3aila4le %due to some hardware crash or an5 other& them its other co!5 can 4e used.
Co)t &'ctor means it greatl5 reduces the cost since the resources can 4e shared
Commuic'tio Me-ium means one can send messages and whate3er the changes at one end
are done can 4e immediatel5 noticed at another.
E(o,utio o& "et4or*i.
"8X8 : ;irst networ( came into eJistenceARPA"ET %ADVANCED RESEARCH ROJEC$ A#ENC\
NE$TORK&R+D 0/<S : "S;"ET %NA$+ONA' SC+ENCE ;O*NDA$+ON NE$TORK&
Iteret is the networ( of networ(s.
S#ITC<I"$ TEC<"I6UES
Switching techniIues are used for transmitting data across networ(s.
Different t5!es are G
1@>@
ARPA"E
T
1@?0
"S;et
1@@0
I"TER"
ET
to connect com!uters at
*.S. defense and different
uni3ersities.
a high ca!acit5 networ( to
4e used strictl5 for
academic and engineering
research.
the internetwor(ing of
ARANE$, NS;net and
other !ri3ate networ(s
93
Circuit Switching
Ressage Switching
ac(et Switching
Circuit S4itc%i.
Circuit switching is the transmission technolog5 that has 4een used since the first
communication networ(s in the nineteenth centur5.
;irst the com!lete !h5sical connection 4etween two com!uters is esta4lished and then the
data are transmitted from the source com!uter to the destination.
Then a call is !laced the switching eIui!ment within the s5stem see(s out a !h5sical co!!er
!ath all the wa5 from the sender to the recei3er.
+t is must to setu! an end:to:end connection 4etween com!uters 4efore an5 data can 4e sent.
$he circuit is terminated when the connection is closed.
+n circuit switching, resources remain allocated during the full length of a communication, after
a circuit is esta4lished and until the circuit is terminated and the allocated resources are freed.
Me))'.e S4itc%i.
+n this the source com!uter sends data or the message to the switching circuit which stores
the data in its 4uffer.
$hen using an5 free lin( to the switching circuit the data is send to the switching circuit.
Entire message is sent to the destination. +t reaches through different intermediate nodes
following the Ustore and forwardQ a!!roach.
No dedicated connection is reIuired.
P'c*et S4itc%i.
* Concei3ed in the "8X/Ys, packet switching is a more recent technolog5 than circuit switching.
* ac(et switching introduces the idea of cutting data i.e. at the source entire message is
4ro(en in smaller !ieces called !ac(ets which are transmitted o3er a networ( without an5
resource 4eing allocated.
* $hen each !ac(et is transmitted and each !ac(et ma5 follow an5 rout a3aila4le and at
destination !ac(ets ma5 reach in random order.
* +f no data is a3aila4le at the sender at some !oint during a communication, then no !ac(et is
transmitted o3er the networ( and no resources are wasted.
* At the destination when all !ac(ets are recei3ed the5 are merged to form the original
message.
* +n !ac(et switching all the !ac(ets of fiJed si=e are stored in main memor5.
DATA COMMU"ICATIO" TERMI"OLO$IES
D't' c%'e, $he information , data carr5 from one end to another
in the networ( 45 channel.
1'u- 9 /it) per )eco-
A/p)B
+t<s used to measurement for the information carr5 of
a communication channel+
Reasurement *nits G:
4it
" 65teC 0 4its
" K6S % Kilo 65te er Second&C "/)- 65tes ,
" K4!s %(ilo4its er Second& C "/)- 4its,
" R4!s % Rega 4its er Second &C"/)- K4!s
1'-4i-t%
+t is amount of information transmitted or recei3es
!er unit time.
+t is measuring in K4!s,R4!s etc unit+
94
Tr')mi))io Me-i'
data is transmitted o3er co!!er wires, fi4er o!tic ca4le, radio and microwa3es. the term YmediaY
is used to genericall5 refer to the !h5sical connectors, wires or de3ices used to !lug things
together.
1')ic commuic'tio) me-i' t=pe)
Co!!er
o unshielded twisted !air %ut!&
o shielded twisted !air %st!&
o coaJial ca4le %thinnet, thic(net&
;i4er o!tic
o single:mode
o multi:mode
+nfrared
Radio 2 microwa3e
T4i)te- P'ir C'/,e
$hese ca4les consist of two insulated co!!er wires twisted around each other in a dou4le
heliJ.
$wisting of wires reduces crosstal( which is 4leeding of a signal from one wire to
another.
T=pe):
*nshielded $wisted air %*$&
Shielded $wisted air %S$
S$ offers greater !rotection from interference and crosstal( due to shielding.
6ut it is hea3ier and costlier than *$.
USE ". +n local tele!hone communication
). ;or digital data transmission o3er short distances u!to " (m
A-('t'.e):
Eas5 to install and maintain
Sim!le
+neJ!ensi3e
'ow weight
Suita4le for small %'ocal& Networ(s
Di)'-('t'.e):
Not suita4le for long distance due to high attenuation.
'ow 4andwidth su!!ort.
'ow S!eed
Co'Gi', c'/,e
CoaJial ca4le consists of a solid co!!er wire core surrounded 45 a !lastic cladding shielded in
a wire mesh.
Shield !re3ents the noise 45 redirecting it to ground+

T=pe):
CoaJial ca4le comes in two si=es which are called thinnet and thicknet.
$hic(net G segment length u!to W// m
$hinnet G segment length u!to "0W m
USE:
+n $V channel communication
95
A-('t'.e):
6etter than twisted wire ca4le.
o!ular for $V networ(s.
Offers higher 4andwidth 2 S!eed
Di)'-('t'.e):
EJ!ensi3e than twisted wires.
Not com!ati4le with twisted wire ca4le.
Optic', ;i/re)
$hin strands of glass or glass li(e material designed to carr5 light from one source to
another.
Source con3erts %Rodulates& the data signal into light using 'ED %'ight Emitting Diodes& or
'ASER diodes and send it o3er the O!tical fi4er.
It co)i)t) o& t%ree p'rt):
". $he coreG glass or !lastic through which the light tra3els.
). $he cladding G co3ers the core and reflects light 4ac( to the core
.. rotecti3e coating G !rotects the fi4er
A-('t'.e)
Not affected 45 an5 (ind of noise.
High transmission ca!acit5
S!eed of 'ight
Suita4le for 4road4and communication
Di)'-('t'.e)
+nstallation reIuires care.
Connecting two O!tical fi4ers is difficult.
O!tical fi4ers are more difficult to solder
Rost eJ!ensi3e
Micro4'(e)
Ricrowa3es are transmitted from the transmitters !laced at 3er5 high towers to the recei3ers at a long
distance.
Ricrowa3es are transmitted in line of sight fashion, and also !ro!agated through the surfaces.
A-('t'.e)
Raintenance eas5 than ca4les.
Suita4le when ca4le can not 4e used.
Di)'-('t'.e)
Re!eaters are reIuired for long distance communication.
'ess 6andwidth a3aila4le
S'te,,ite
96
#eostationar5 satellites are !laced around .X/// KR awa5 from the earth<s surface. +n satellite
communication transmitting station transmits the signals to the satellite. %+t is called u!:lin(ing&. After
recei3ing the signals %microwa3es& it am!lifies them and transmit 4ac( to earth in whole 3isi4ilit5 area.
Recei3ing stations at different !laces can recei3e these signals. %+t is called down:lin(ing&.
A-('t'.e
* Area co3erage is too large
Di)'-('t'.e
* High in3estment
"et4or* -e(ice)
Mo-em
A modem is a com!uter !eri!heral that allows 5ou to connect and communicate with other
com!uters 3ia tele!hone lines.
Rodem means Rodulation, Demodulation.
RodulationG A modem changes the digital data from 5our com!uter into analog data, a
format that can 4e carried 45 tele!hone lines.
DemodulationG $he modem recei3ing the call then changes the analog signal 4ac( into
digital data that the com!uter can digest.
$he shift of digital data into analog data and 4ac( again, allows two com!uters to s!ea(
with one another.
RM- 27 Coector
RJ:-W is short for Registered Jac(:-W. +t is an eight wire connector which is commonl5 used to
connect com!uters on the local area networ(s i.e., 'AN.
"et4or* Iter&'ce C'r-) AEt%eret C'r-B
A networ( card, networ( ada!ter or N+C %networ( interface card& is a !iece of com!uter
hardware designed to allow com!uters to communicate o3er a computer et4or*. +t !ro3ides
!h5sical access to a networ(ing medium and often !ro3ides a low:le3el addressing s5stem
through the use of RAC addresses. +t allows users to connect to each other either 45 using
ca4les or wirelessl5.
Repe'ter)
A re!eater is an electronic de3ice that
recei3es a signal and retransmits it at a
higher le3el or higher !ower, or onto the
other side of an o4struction, so that the
signal can co3er longer distances without
degradation. +n most twisted !air Ethernet
configurations, re!eaters are reIuired for
ca4le runs longer than "// meters.
97
<u/)
A hu4 contains multi!le !orts. Then a !ac(et arri3es at one !ort,
it is co!ied to all the !orts of the hu4. Then the !ac(ets are
co!ied, the destination address in the frame does not change to a
4roadcast address. +t does this in a rudimentar5 wa5, it sim!l5
co!ies the data to all of the Nodes connected to the hu4.
1ri-.e)A networ( 4ridge connects multi!le networ(
segments at the data lin( la5er %la5er )& of the OS+
model. 6ridges do not !romiscuousl5 co!5 traffic to all
!orts, as hu4s do, 4ut learn which RAC addresses are
reacha4le through s!ecific !orts. Once the 4ridge
associates a !ort and an address, it will send traffic for
that address onl5 to that !ort. 6ridges do send
4roadcasts to all !orts eJce!t the one on which the
4roadcast was recei3ed.
.
S4itc%e)
Switch is a de3ice that !erforms switching. S!ecificall5, it forwards and filters OS+ la5er ) datagrams
%chun( of data communication& 4etween !orts %connected ca4les& 4ased on the Rac:
Addresses in the !ac(ets. $his is distinct from a hu4 in that it onl5 forwards the datagrams to
the !orts in3ol3ed in the communications rather than all !orts connected. Strictl5 s!ea(ing, a
switch is not ca!a4le of routing traffic 4ased on + address %la5er .& which is necessar5 for
communicating 4etween networ( segments or within a large or com!leJ 'AN.
Router)
Routers are networ(ing de3ices that forward data !ac(ets 4etween networ(s using headers
and forwarding ta4les to determine the 4est !ath to forward the !ac(ets. Routers wor( at the
networ( la5er of the $C,+ model or la5er . of the OS+ model. Routers also !ro3ide
interconnecti3it5 4etween li(e and unli(e media %R;C "0")&.
A router is connected to at least two networ(s, commonl5 two 'ANs or TANs or a 'AN and its
+SYs networ(.
$ATE#AY
A #atewa5 is a networ( de3ice that connects dissimilar
networ(s. +t esta4lished an intelligent connection 4etween a
local area networ( and eJternal networ(s with com!letel5
different structures.
"et4or* topo,o.ie) '- t=pe)
"et4or* topo,o.=
98
Com!uter networ(s ma5 4e classified according to the networ( to!olog5 u!on which the
networ( is 4ased, such as 6us networ(, Star networ(, Ring networ(, Resh networ(, Star:4us
networ(, $ree or Hierarchical to!olog5 networ(, etc.
Networ( $o!olog5 signifies the wa5 in which intelligent de3ices in the networ( see their logical
relations to one another.

5esh Topolo%y
$he 3alue of full5 meshed networ(s is !ro!ortional to the eJ!onent of the num4er of
su4scri4ers, assuming that communicating grou!s of an5 two end!oints, u! to and including
all the end !oints.
Star Topolo%y
$he t5!e of networ( to!olog5 in which each of the nodes of the networ( is
connected to a central node with a !oint:to:!oint lin( in a Yhu4Y and Ys!o(eY
fashion, the central node 4eing the Yhu4Y and the nodes that are attached to the
central node 4eing the Ys!o(esY %e.g., a collection of !oint:to:!oint lin(s from
the !eri!heral nodes that con3erge at a central node& > all data that is
transmitted 4etween nodes in the networ( is transmitted to this central node,
which is usuall5 some t5!e of de3ice that then retransmits the data to some or
all of the other nodes in the networ(, although the central node ma5 also 4e a
sim!le common connection !oint %such as a Y!unch:downY 4loc(& without an5
acti3e de3ice to re!eat the signals.
8us Topolo%y
$he t5!e of networ( to!olog5 in which all of the nodes of the
networ( are connected to a common transmission medium
which has eJactl5 two end!oints %this is the Y4usY, which is also
commonl5 referred to as the 4ac(4one, or trun(& > all data that
is transmitted 4etween nodes in the networ( is transmitted o3er
this common transmission medium and is a4le to 4e recei3ed
45 all nodes in the networ( 3irtuall5 simultaneousl5
%disregarding !ro!agation dela5s&.
Rin% Topolo%y
$he t5!e of networ( to!olog5 in which each of the nodes of the
networ( is connected to two other nodes in the networ( and with the
first and last nodes 4eing connected to each other, forming a ring > all
data that is transmitted 4etween nodes in the networ( tra3els from
99
% e r # e r
% e r # e r
% e r # e r
one node to the neJt node in a circular manner and the data generall5 flows in a single
direction onl5/
Compter "et4or*)
A communications networ( is two or more com!uters connected to share data and resources
are Unetwor(ed.Q $he sim!le idea 4ehind com!uter networ(ing is to allow users to access
more information and gi3e them access to de3ices not directl5 attached to their UlocalQ s5stem,
such as !rinters or storage de3ices
Loc', Are' "et4or* ALA"B
A networ( co3ering a small geogra!hic area, li(e a home, office, or 4uilding. Current 'ANs are most
li(el5 to 4e 4ased on Ethernet technolog5. ;or eJam!le, a li4rar5 will ha3e a wired or a
communications networ( is two or more com!uters connected to share data and resources
are Unetwor(ed.Q $he sim!le idea 4ehind com!uter networ(ing is to allow users to access
more information and gi3e them access to de3ices not directl5 attached to their UlocalQ s5stem,
such as priter) or )tor'.e -e(ice)+
Metropo,it' Are' "et4or* AMA"B
A Retro!olitan Area Networ( is a networ( that connects two or more 'ocal Area Networ(s or
Cam!us Area Networ(s together 4ut does not eJtend 4e5ond the 4oundaries of the immediate
town, cit5, or metro!olitan area. Rulti!le routers, switches 2 hu4s are connected to create a
RAN.
#i-e Are' "et4or* A#A"B
TAN is a data communications networ( that co3ers a relati3el5 4road geogra!hic area %i.e.
one cit5 to another and one countr5 to another countr5& and that often uses transmission
facilities !ro3ided 45 common carriers, such as tele!hone com!anies. TAN technologies
generall5 function at the lower three la5ers of the OS+ reference modelG the !h5sical la5er, the
data lin( la5er, and the networ( la5er.
"et4or* Protoco,)
Protoco,)
A !rotocol means the rules that are a!!lica4le for a networ(.
+t defines the standardi=ed format for data !ac(ets, techniIues for detecting and correcting
errors and so on.
A !rotocol is a formal descri!tion of message formats and the rules that two or more machines
must follow to eJchange those messages.
E.g. using li4rar5 4oo(s.
T=pe) o& protoco,) 're:
". H$$
). ;$
.. $C,+
-. S'+,
<=perteGt Tr')&er Protoco, %<TTP& is a communications !rotocol for the transfer of
information on the intranet and the Torld Tide Te4. +ts original !ur!ose was to !ro3ide a
wa5 to !u4lish and retrie3e h5!erteJt !ages o3er the +nternet.
H$$ is a reIuest,res!onse standard 4etween a client and a ser3er. A client is the end:
userM the ser3er is the we4 site.
;TP A;i,e Tr')&er Protoco,B is the sim!lest and most secure wa5 to eJchange files
o3er the +nternet. $he o47ecti3es of ;$ areG
$o !romote sharing of files %com!uter !rograms and,or data&.
$o encourage indirect or im!licit use of remote com!uters.
100
$o shield a user from 3ariations in file storage s5stems among different hosts.
$o transfer data relia4l5, and efficientl5.
TCP/IP ATr')mi))io Cotro, Protoco, / Iteret Protoco,B
TCP : is res!onsi4le for 3erif5ing the correct deli3er5 of data from client to ser3er. Data can 4e
lost in the intermediate networ(. $C adds su!!ort to detect errors or lost data and to trigger
retransmission until the data is correctl5 and com!letel5 recei3ed.
IP : is res!onsi4le for mo3ing !ac(et of data from node to node. + forwards each !ac(et 4ased
on a four 45te destination address %the + num4er&. $he +nternet authorities assign ranges of
num4ers to different organi=ations. $he organi=ations assign grou!s of their num4ers to
de!artments. + o!erates on gatewa5 machines that mo3e data from de!artment to organi=ation
to region and then around the world.
SLIP/PPP ASeri', Lie Iteret Protoco, / Poit to Poit Protoco,B
S'+, !ro3ides the a4ilit5 to trans!ort $C,+ traffic e3er serial line 4etween two com!uters.
$he home user<s com!uter has a communications lin( to the internet. $he home user<s com!uter
has the networ(ing software that can s!ea( $C,+ with other com!uters on the +nternet. $he
home user<s com!uter has an identif5ing address %+ address& at which it can 4e contacted 45
other com!uters on +nternet. E.g. dial u! connection.
Te,et-
+t is an older internet utilit5 that lets us log on to remote com!uter s5stem. +t also facilitates for terminal
emulation !ur!ose. $erminal emulation means using a !c li(e a mainframe com!uter through
networ(ing.
%i& Run telnet client: $5!e telnet in run dialog 4oJ.
%ii& Connect to telnet site :s!ecif5 the host name, !ort and terminal t5!e.
%iii& Start 4rowsing: surf the shown site with !ro3ided instruction.
%i3& ;inall5 disconnect:!ress Alt1;-.
#ire,e))/Mo/i,e Computi.
Tireless communication is sim!l5 data communication without the use of landlines. Ro4ile
com!uting means that the com!uting de3ice is not continuousl5 connected to the 4ase or central
networ(.
". $SMA$,o/', S=)tem &or Mo/i,e commuic'tioB: it is leading digital cellular s5stem. +n
co3ered areas, cell !hone users can 4u5 one !hone that will wor( an5 where the standard is
su!!orted. +t uses narrow4and $DRA, which allows eight simultaneous calls on the same radio
freIuenc5.
). CDMAACo-e Di(i)io Mu,tip,e Acce))BG it is a digital cellular technolog5 that uses s!read:
s!ectrum techniIues. CDRA does not assign a s!ecific freIuenc5 to each user. +nstead ,e3er5
channel uses the full a3aila4le s!ectrum.
.. #LLA#ire,e)) i Loc', LoopB : T'' is a s5stem that connects su4scri4ers to the !u4lic
switched tele!hone networ( using radio signals as a su4stitute for other connecting media.
-. Em'i,AE,ectroic M'i,B: Email is sending and recei3ing messages 45 com!uter.
W. C%'t: Online teJtual tal( in real time , is called Chatting.
X. 3i-eo Co&ereci.G a two wa5 3ideo!hone con3ersation among multi!le !artici!ants is
called 3ideo conferencing.
9. SMSAS%ort Me))'.e Ser(iceB: SRS is the transmission of short teJt messages to and from
a mo4ile !one, faJ machine and or + address.
0. 0$ '- ED$E: .# is a s!ecification for the third generation of mo4ile communication of mo4ile
communication technolog5. .# !romises increased 4andwidth, u! to .0- K4!s when a de3ice is
stationar5.
ED$EAE%'ce- D't' r'te) &or $,o/', E(o,utio B is a radio 4ased high s!eed mo4ile
data standard.
"ET#OR! SECURITY CO"CEPTS
rotection methods
101
1 Aut%oriH'tio : Authori=ation confirms the ser3ice reIuestorYs credentials. +t determines if
the ser3ice reIuestor is entitled to !erform that o!eration.
2 Aut%etic'tio : Each entit5 in3ol3ed in using a we4 ser3ice the reIuestor, the !ro3ider and
the 4ro(er%if there is one& : is what it actuall5 claims to 4e.
0 Ecr=ptio > con3ersion of the form of data from one form to another form.
2 1iometric S=)tem : in3ol3es uniIue as!ect of a !ersonYs 4od5 such as ;inger:!rints, retinal
!atterns etc to esta4lish his,her +dentit5.
7 ;ire4',, : A s5stem designed to !re3ent unauthori=ed access to or from a !ri3ate
networ( is called firewall. it can 4e im!lemented in 4oth hardware and software or com4ination
or 4oth.
T%ere 're )e(er', t=pe) o& &ire4',, tec%iDue)-
? P'c*et &i,ter- acce!ts or re7ects of !ac(ets 4ased on user defined rules.
? App,ic'tio .'te4'=- securit5 mechanism to s!ecific a!!lication li(e ;$ and $elnet
ser3ers.
? Circuit ,e(e, .'te4'= - a!!lies securit5 mechanism when a connection is esta4lished.
? ProG= Ser(er - +nterce!ts all messages entering and lea3ing the networ(.
Coo*ie) : Coo(ies are messages that a we4 ser3er transmits to a we4 4rowser so that the we4
ser3er can (ee! trac( of the user<s acti3it5 on a s!ecific we4 site. Coo(ies ha3e few !arameters
name, 3alue, eJ!iration date
<'c*er) '- cr'c*er) -
Hac(ers are more interested in gaining (nowledge a4out com!uter s5stems and !ossi4l5 using this
(nowledge for !la5ful !ran(s.
Crac(ers are the malicious !rogrammers who 4rea( into secure s5stems.
C=/er L'4 -
+t is a generic term, which refers to all the legal and regulator5 as!ects of internet and the Torld Tide
Te4.
#E1 SER3ERS
### A#ORLD #IDE #E1B
+t is a small !art of +nternet. +t is a (ind of A!!lication of internet.+t is a set of !rotocols that allows us to
access an5 document on the Net through a naming s5stem 4ased on *R'S. +nternet was mainl5 used
for o4taining teJtual information. 6ut !ost:TTT the internet !o!ularit5 grew tremendousl5 4ecause
of gra!hic intensi3e nature of www.
Attri/ute) o& ###
%i& U)er &rie-,=: www resources can 4e easil5 used with the hel! of 4rowser.
%ii& Mu,time-i' -ocumet):A we4 !age ma5 ha3e gra!hic, audio, 3ideo, and animation etc at a time.
%iii& <=perteGt '- %=per,i*):the d5namic lin(s which can mo3e towards another we4 !age is
h5!erlin(.
%i3& Iter'cti(e :www with its !ages su!!ort and ena4le interacti3it5 4etween users and ser3ers.
%3& &r'me:dis!la5 of more than one section on single we4 !age.
#e/ )er(er: +t is a TTT ser3er that res!onds to the reIuests made 45 we4 4rowers.
e.g. G A!ache, ++S, TS%ersonal we4 ser3er for Tindows 80&.
#e/ /ro4)er: +t is a TTT client that na3igates through the Torld Tide Te4 and dis!la5s we4
!ages. E.g.G ;ire;oJ Na3igator, +nternet EJ!lorer etc.
#e/ )ite): A location on a net ser3er where different we4 !ages are lin(ed together 45 d5namic lin(s
is called a we4 site. Each we4 site has a uniIue address called *R'.
#e/ p'.e : A document that can 4e 3iewed in a we4 4rowser and residing on a we4 site is a we4
!age.
<ome p'.e: a we4 !age that is the starting !age and acts as an indeJed !age is home !age.
#e/ port', : that facilitates 3arious t5!e of the functionalit5 as we4 site. for e.g.
www.5ahoo.com,www.rediff.com
Dom'i 'me: An internet address which is a character 4ased is called a Domain name. Some most
102
common domains are com, edu, go3, mil, net, org, and co.Some domain names are location 4ased
also. ;or e.g. au for Australia, a for Canada, in for +ndia etc.
UR': A *R' %uniform resource locator& that s!ecifies the distinct address for each resource on the
internet.e.g.htt!G,,enc5cle.msn.com,getinfo,st5!es.as!
#e/ %o)ti. : means hosting we4 ser3er a!!lication on a com!uter s5stem through which electronic
content on the internet is readil5 a3aila4le to an5 we4 4rowser client.
<TML -
+t stands for H5!er $eJt Rar(u! 'anguage that facilitates to write we4 document that can 4e
inter!reted 45 an5 we4 4rowser. +t !ro3ide certain tags that are inter!reted 45 the 4rowser how to
dis!la5 and act with the teJt, gra!hics etc. tags are s!ecified in AB.
;or e.g.
A4od5 4gcolorCgreenB it is o!ening tag
A,4od5B it is closing tag.
4od5 is the tag with 4gcolor attri4utes.
XML AeXte)i/,e M'r*up L'.u'.eB
]R' is a mar(u! language for documents containing structured information. Structured information
contains 4oth content %words, !ictures etc.& and some indication of what role content !la5s.
D<TML: +t stands for D5namic H5!er $eJt Rar(u! 'anguage. DH$R' refers to Te4 content that
changes each time it is 3iewed. ;or eJam!le, the same *R' could result in a different !age
de!ending on an5 num4er of !arameters, such asG
?geogra!hic location
?time of the da5
?!re3ious !ages 3iewed 45 the user
?!rofile of the reader
#E1 SCRIPTI"$ O $he !rocess of creating and em4edding scri!ts in a we4 !age is (nown as we4:
scri!ting.
SCRIPT: A Scri!t is a list of commands em4edded in a we4 !age. Scri!ts are inter!reted and
eJecuted 45 a certain !rogram or scri!ting >engine.
T=pe) o& Script):
1. C,iet Si-e Script: Client side scri!ting ena4les interaction within a we4 !age.
Some !o!ular client:side scri!ting languages are V6Scri!t, Ja3aScri!t, H%H5!er $eJt
re!rocessor&.
2+ Ser(er-Si-e Script): Ser3er:side scri!ting ena4les the com!letion or carr5ing out a tas( at the
ser3er:end and then sending the result to the client >end.
Some !o!ula ser3er:side Scri!ting 'anguages are H, erl, AS%Acti3e Ser3er ages&, JS%Ja3a
Ser3er ages& etc.
OPE" SOURCE TERMI"OLO$IES
TERMI"OLO$Y 9 DE;I"ITIO"S:
;ree So&t4're: $he S,T<s is freel5 accessi4le and can 4e freel5 used changed im!ro3ed
co!ied and distri4uted 45 all and !a5ments are needed to ma(e for free S,T.
Ope Source So&t4're: S,w whose source code is a3aila4le to the customer and it can 4e
modified and redistri4uted without an5 limitation .OSS ma5 come free of cost 4ut nominal
charges has to !a5 nominal charges %Su!!ort of S,T and de3elo!ment of S,T&.
;LOSS A;ree Li/re '- Ope Source So&t4'reB : S,w which is free as well as o!en source
S,T. % ;ree S,T 1 O!en Source S,T&.
$"U A$N*<s "ot UniJB : #N* !ro7ect em!hasi=e on the freedom and its o47ecti3e is to create
a s5stem com!ati4le to *N+] 4ut not identical with it.
;S; A;ree So&t4're ;ou-'tioB : ;S; is a non >!rofit organi=ation created for the !ur!ose
of the free s,w mo3ement. Organi=ation funded man5 s,w de3elo!ers to write free software.
OSI AOpe Source Iiti'ti(eB G O!en source software organi=ation dedicated to cause of
!romoting o!en source software it s!ecified the criteria of OSS and its source code is not
freel5 a3aila4le.
#0CA#or,- #i-e #e/ Co)ortiumB G T.C is res!onsi4le for !roducing the software
standards for Torld Tide Te4.
103
Propriet'r= So&t4're: ro!rietar5 Software is the s,w that is neither o!en nor freel5
a3aila4le, normall5 the source code of the ro!rietar5 Software is not a3aila4le 4ut further
distri4ution and modification is !ossi4le 45 s!ecial !ermission 45 the su!!lier.
;ree4're: ;reeware are the software freel5 a3aila4le , which !ermit redistri4ution 4ut not
modification %and their source code is not a3aila4le&. ;reeware is distri4uted in (inary $orm
%read5 to run& without an5 licensing fees.
S%'re4're: Software for which license fee is !a5a4le after some time limit, its source code is
not a3aila4le and modification to the software are not allowed.
Loc',iH'tio: locali=ation refers to the ada!tation of language, content and design to reflect
local cultural sensiti3ities .e.g. Software 'ocali=ationG where messages that a !rogram
!resents to the user need to 4e translated into 3arious languages.
Iter'tio',iH'tio: O!!osite of locali=ation.
OPE" SOURCE / ;REE SO;T#ARE
LiuG : 'inuJ is a famous com!uter o!erating s5stem . !o!ular 'inuJ ser3er set of !rogram >
'AR%'inuJ, A!ache, R5S`', H&
MoHi,,' : Ro=illa is a free internet software that includes
a we4 4rowser
an email client
an H$R' editor
+RC client
Ap'c%e )er(er: A!ache we4 ser3er is an o!en source we4 ser3er a3aila4le for man5
!latforms such as 6SD, 'inuJ, and Ricrosoft Tindows etc.
A!ache Te4 ser3er is maintained 45 o!en communit5 of de3elo!ers of A!ache
software foundation.
MYS6L : R\S`' is one of the most !o!ular o!en source data4ase s5stem. ;eatures of
R\S`l G
Rultithreading
Rulti >*ser
S`l Relational Data4ase Ser3er
Tor(s in man5 different !latform
Po)t.reS6L : ostgres S`' is a free software o47ect relational data4ase ser3er .
ostgresS`' can 4e downloaded from www.!ostgressIl.org.
P'.o : ango !ro7ect is to !ro3ide an o!en source framewor( for the la5out and rendering of
internationali=ed teJt into #$K 1 #NORE en3ironment.ango using *nicode for all of its
encoding ,and will e3entuall5 su!!ort out!ut in all the worlds ma7or languages.
OpeO&&ice : O!enOffice is an office a!!lications suite. +t is intended to com!ati4le and
directl5 com!lete with Ricrosoft office.
OOo Version "." includesG
Triter %word !rocessor&
Calc%s!readsheet&
Draw%gra!hics !rogram&
etc
Tomc't : $omcat functions as a ser3let container. $omcat im!lements the ser3let and the
Ja3aSer3er ages .$omcat comes with the 7as!er com!iler that com!lies JSs into ser3lets.
P<PA<=perteGt Preproce))orB : H is a widel5 used o!en source !rogramming language
for ser3er side a!!lication and de3elo!ing we4 content.
P=t%o: P=t%o is an interacti3e !rogramming language originall5 as scri!ting language for
Amoe4a OS ca!a4le of ma(ing s5stem calls.
1or 2 M'r*) 6ue)tio)
1+ EJ!lain function of hu4 and router.
A):
104
<u/: A hu4 contains multi!le !orts. Then a !ac(et arri3es at one !ort, it is co!ied to all the !orts
of the hu4. Then the !ac(ets are co!ied, the destination address in the frame does not change to a
4roadcast address. +t does this in a rudimentar5 wa5, it sim!l5 co!ies the data to all of the Nodes
connected to the hu4.
). Router : routers are networ(ing de3ices that forward data !ac(ets 4etween networ(s using
headers and forwarding ta4les to determine the 4est !ath to forward the !ac(ets
). EJ!and the following terms
%i& *R' %ii& +S %iii& DH$R' %i3& CDRAG
A)I %i& *R'G *nified Resource 'ocator
%ii& +SG +nternet Ser3ice ro3ider.
%iii& DH$R'G D5namic H5!er $eJt Rar(u! 'anguage

.. Differentiate 4etween message switching and !ac(et switching
A): Ressage Switching > +n this form of switching no !h5sical co!!er !ath is esta4lished in
ad3ance 4etween sender and recei3er. +nstead when the sender has a 4loc( of data to 4e sent, it is
stored in first switching office, then forwarded later. ac(et Switching > Tith message switching there
is no limit on 4loc( si=e, in contrast !ac(et switching !laces a tight u!!er limit on 4loc( si=e.

-. Trite two a!!lications of C54er 'aw.
A): $wo a!!lications of c54er law are Digital $ransaction and Acti3ities on +nternet.
W. EJ!lain #SR.
A)G #lo4al s5stem for mo4ile, communications is a technolog5 that uses narrow4and $DRA, which
allows eight simultaneous calls on the same radio freIuenc5. $DRA is short for $ime Di3ision Rulti!le
Access. $DRA technolog5 uses time di3ision multi!leJing and di3ides a radio freIuenc5 into time
slots and then allocates these slots to multi!le calls there45 su!!orting multi!le, simultaneous data
channels.
X. Trite difference 4etween coaJial and o!tical ca4le.
A)G CoaJial ca4le consists of a solid wire core surrounded 45 one or more foil or wire shield , each
se!arated 45 some (ind of !lastic insulator. O!tical fi4ers consists of thin strands of glass or glass li(e
material which are so constructed that the5 carr5 light from a source at one end of the fi4er to a
detector at the other end.
9. Trite two ad3antage and disad3antage of RI"$ to!olog5.
A):
A-('t'.e):
". Short ca4le length.
). No wiring closet s!ace reIuired.
Di)'-('t'.e):
". Node failure causes networ( failure
). difficult to diagnose faults
0. Define O!en Source Software, ;ree Software, ;reeware, and Shareware+
A)G
;ree So&t4're : $he S,T<s is freel5 accessi4le and can 4e freel5 used changed im!ro3ed co!ied
and distri4uted 45 all and !a5ments are needed to made for free S,T.
Ope Source So&t4're : S,w whose source code is a3aila4le to the customer and it can 4e
modified and redistri4uted without an5 limitation .OSS ma5 come free of cost 4ut nominal charges
has to !a5 nominal charges %Su!!ort of S,T and de3elo!ment of S,T&.
;ree4're: ;reeware are the software freel5 a3aila4le , which !ermit redistri4ution 4ut not
modification %and their source code is not a3aila4le&. ;reeware is distri4uted in (inary $orm %read5
to run& without an5 licensing fees.
S%'re4're: Software for which license fee is !a5a4le after some time limit, its source code is not
a3aila4le and modification to the software are not allowed.
0. That is the difference 4etween TAN and RANF
AnsG RAN %Retro!olitan Area Networ(& is the networ( s!read o3er a cit5.
105
TAN %Tide Area Networ(& s!read across countries.
"/. That is the !ur!ose of using ;$F
!ns: &i'$o !romote sharing of files %com!uter !rograms and,or data&.
%ii&$o encourage indirect or im!licit use of remote com!uters
"". That is a RodemF
AnsG A modem is a com!uter !eri!heral that allows 5ou to connect and communicate with other
com!uters 3ia tele!hone lines.
"). How is a Hac(er different from a Crac(erF
!ns: Hac(ers are more interested in gaining (nowledge a4out com!uter s5stems and !ossi4l5 using
this (nowledge for !la5ful !ran(s.
Crac(ers are the malicious !rogrammers who 4rea( into secure s5stems
".. EJ!and the following terms with res!ect to Networ(ingG
%i& Rodem %ii& T'' %iii& $C,+ %i3& ;$
AnsG %i& Rodem G Rodulator,Demodulator
%ii& T''G Tireless in 'ocal 'oo!
%iii& $C,+G $ransmission Control rotocol,+nternet rotocol
i3& ;$G ;ile $ransfer rotocol

"-. That are rotocolsF
!ns: A !rotocol means the rules that are a!!lica4le for a networ(.
+t defines the standardi=ed format for data !ac(ets, techniIues for detecting and correcting
errors and so on.
A !rotocol is a formal descri!tion of message formats and the rules that two or more machines
must follow to eJchange those messages.
E.g. using li4rar5 4oo(s.
$5!es of !rotocols areG
". H$$
". ;$
). $C,+
.. S'+,
"W. That is the difference 4etween Re!eater and a 6ridgeF "
AnsG A Re!eater is a networ( de3ice that am!lifies and restores signals for long distance transmission
where as a 6ridge is a networ( de3ice that esta4lished an intelligent connection 4etween two local
networ(s with the same standard 4ut with different t5!es of ca4les.
<OTS A<I$<ER ORDER T<I"!I"$ S!ILLSB
2 M'r*) 6ue)tio)
1+ Knowledge Su!!lement Organi=ation has set u! its new centre at Rangalore for its office and we4
4ased acti3ities. +t has four 4uilding as shown in the diagram 4elow -
Centre to Centre distance 4etween 3arious 4uildings
alp-a
/amma
e(a
0amb$a
106
Al!ha to 6eta W/m
6eta to gamma "W/m
#amma to 'am4da )Wm
Al!ha to 'am4da "9/m
6eta to 'am4da ")Wm
Al!ha to #amma 8/m
%a& Suggesting a ca4le la5out of connection 4etween 4uilding state with 7ustification where Ser3er,
Re!eater and hu4 will 4e !laced. )
%4& $he organi=ation is !lanning to lin( its front office situated in the cit5 in a hill5 region where
ca4le connection is not feasi4le, suggest an economic wa5 to connect it with reasona4l5 high
s!eedF

A): %i&$he most suita4le !lace to house the ser3er of this organi=ation would 4e 4uilding #amma ,
as this 4uilding contains the maJimum num4er of com!uters , thus decreasing the ca4ling cost for
most of the com!uters as well as increasing the efficienc5 of the maJimum com!uters in the networ(
Distance 4etween al!ha to gamma and 4eta to gamma is large so there re!eater will reIuire and hu4
is necessar5 for all !remises 4ecause it is used in local networ(ing.
%ii& $he most economic wa5 to connect it with a reasona4le high s!eed would 4e to use radio wa3e
transmission, as the5 are eas5 to install, can tra3el long distances, and !enetrate 4uildings easil5, so
the5 are widel5 used for communication, 4oth indoors and outdoors. Radio wa3es also ha3e the
ad3antage of 4eing omni directional, which is the5 can tra3el in all the directions from the source, so
that the transmitter and recei3er do not ha3e to 4e carefull5 aligned !h5sicall5.
2+ Software De3elo!ment Com!an5 has set u! its new center at Jai!ur
for its office and we4 4ased acti3ities. +t has - 4loc(s of 4uildings as shown in
the diagram 4elowG
Center to center distances 4etween 3arious 4loc(s
6lac( A to 6loc( 6 W/ m
6loc( 6 to 6loc( C "W/ m
6loc( C to 6loc( D )W m
6loc( A to 6loc( D "9/ m
6loc( 6 to 6loc( D ")W m
Al!ha )W
6eta W/
#amma ")W
'am4da "/
!lp-a
/amma
e(a
0amb$
a
1epea(er
23
23
23
23
107
6loc( A to 6loc( C 8/ m
Num4er of Com!uters
6lac( A )W
6loc( 6 W/
6loc( C ")W
6loc( D "/
e"& Suggest a ca4le la5out of connections 4etween the 4loc(s.
e)& Suggest the most suita4le !lace %i.e. 4loc(& to house the ser3er of this
com!an5 with a suita4le reason.
e.& Suggest the !lacement of the following de3ices with 7ustification
%i& Re!eater
%ii& Hu4,Switch
e-& the com!an5 is !lanning to lin( its front office situated in the
cit5 in a hill5 region where ca4le connection is not feasi4le, suggest
an economic wa5 to connect it with reasona4l5 high s!eedF
A):
Ae1B %An5 of the following o!tion&
'a5out O!tion "
'a5out O!tion )
Ae2B $he most suita4le !lace , 4loc( to house the ser3er of this organi=ation would 4e 6loc( C, as this
4loc( contains the maJimum num4er of com!uters, thus decreasing the ca4ling cost for most of the
com!uters as well as increasing the efficienc5 of the maJimum com!uters in the networ(.
Ae0B
%i& ;or 'a5out ", since the ca4ling distance 4etween 6loc(s A and C, and that 4etween 6 and C are
Iuite large, so a re!eater each would ideall5 4e needed along their !ath to a3oid loss of signals
during the course of data flow in these routes.
;or la5out ), since the distance 4etween 6loc(s A and C is large so a re!eater would ideall5 4e
!laced in 4etween this !ath.
%ii& +n 4oth the la5outs, a hu4,switch each would 4e needed in all the 4loc(s, to
+nterconnect the grou! of ca4les from the different com!uters in each 4loc(.
Ae2B $he most economic wa5 to connect it with a reasona4le high s!eed would 4e to use radio wa3e
transmission, as the5 are eas5 to install, can tra3el long distances, and !enetrate 4uildings easil5, so
the5 are widel5 used for communication, 4oth indoors and outdoors. Radio wa3es also ha3e the
ad3antage of 4eing omni directional, which is the5 can tra3el in all the directions from the source, so
that the transmitter and recei3er do not ha3e to 4e carefull5 aligned !h5sicall5.
.. Ram #oods 'td. has following four 4uildings in Ahmeda4ad cit5.
108
Com!uters in each 4uilding are networ(ed 4ut 4uildings are not networ(ed so
far. $he com!an5 has now decided to connect 4uilding also.
%a& Suggest a ca4le la5out for these 4uildings.
%4& +n each of the 4uildings, the management wants that each 'AN segment gets
a dedicated 4andwidth i.e. 4andwidth must not 4e shared. How can this 4e
achie3edF
%c& $he com!an5 also wants to ma(e a3aila4le shared +nternet access for each of
the 4uildings. How can this 4e achie3edF
%d& $he com!an5 wants to lin( its head office in #V" 4uilding to its another office in
Ja!an.
%i& Thich t5!e of transmission medium is a!!ro!riate for such a lin(F
%ii& That t5!e of networ( would this connection result intoF
A):
9a: 7otal cable length reSuired for this layout ; M" mts
94& $o gi3e dedicated 4andwidth, the com!uters in each 4uilding should 4e
connected 3ia switches as switches offer dedicated 4andwidths.
%c& 65 installing routers in each 4uilding, shared internet access can 4e made
ossi4le.
%d& %i& Satellite as it can connect offices across glo4e.
%ii& TAN %Tide Area Networ(&
Computer Sciece 6ue)tio P'per 4it% So,utio M'rc% 2011
$ime allowedG. hours RaJimum Rar(sG9/
1+ A'B #%'t i) -i&&erece /et4ee T=pe C')ti. '- Autom'tic T=pe Co(er)ioJ A,)oE .i(e '
)uit'/,e C55 to i,,u)tr'te /ot%+ 2
A)+ EJ!licit $5!e Casting is used to con3ert 3alue of one t5!e to another t5!e for eJam!le
float JC%float& . , )M ,, ".W will 4e assigned as result, 4ecause . is con3erted into ../
Automatic $5!e Con3ersion is the t5!e con3ersion done 45 the com!iler where3er reIuired. e. g.
float JC.,)M ,,here "./ will 4e assigned as result, 4ecause " is automaticall5 con3erted in "./
1+ A/B #rite t%e 'me) o& t%e %e'-er &i,e)E 4%ic% i)/'re e))eti',,= reDuire- to ru/eGecute t%e
&o,,o4i. C55 co-e:
(oi- m'iAB 1
Xc%'r C<ETeGtK LWQ5(e Attitu-eQI
&orAit iW0ITeGtK iLWV\0VVi55B
i&ATeGtKiLWWV UB
coutZZe-,I
e,)e XC<WtoupperATeGtKiLBI
coutZZC<I
Y
109
Y
A)+ ReIuired header files are io)tre'm+% and ct=pe+%
1+ AcB Re4rite t%e &o,,o4i. pro.r'm '&ter remo(i. t%e )=t'ctic', error) Ai& '=B+ U-er,ie
e'c% correctio+ 2
ic,u-eZio)tre'm+%T
t=pe-e& c%'r K?0L Stri.I
(oi- m'iA B
XStri. SWQPe'ceQI
it LW)tr,eASBI
coutZZSZZV %') UZZLZZVc%'r'cter)VZZe-,I
Y
A)+
SincludeAiostream.hB
SincludeAstring.hB
t5!edef char StringO0/PM
3oid main% &
L
String SCQeaceQM
int 'Cstrlen%S&M
coutAASAAQ has UAA'AAQ charactersQAAendlM
N
1+ A-B ;i- t%e output o& t%e &o,,o4i. pro.r'm: 0
]ic,u-eZio)tre'm+%T
(oi- S4itc%O(erAit AK LE it "E it Sp,itB
X &or Ait !W0I !Z"I !55B
i&A!ZSp,itB
AK!L5W!I
e,)e
AK!LSW!I
Y
(oi- -i)p,'=Ait AK LE it "B
X &orAit !W0I !Z"I !55B
A!^2WW0B J coutZZAK!LZZQ^Q : coutZZAK!LZZe-,I
Y
(oi- m'iA B
X it <K00E 20E 70E 20E 10E 7YI
S4itc%O(erA<E>E0BI
Di)p,'=A<E >BI
Y
A)+
./@-"
W)@X/
-/@)W
1+ AeB ;i- t%e output o& t%e &o,,o4i. pro.r'm : 2
]iic,u-eZio)tre'm+%T
(oi- m'iA B
X it S6ueeE Mo(e) K LWX11E22E00E22YI
6ueeWMo(e)I
Mo(e)K2L5W22I
coutZZQ6uee _QZZS6ueeZZe-,I
S6uee-W11I
6uee5W2I
coutZZQ"o4 _QZZS6ueeZZe-,I
6uee55I
coutZZQ;i',,= _QZZS6ueeZZe-,I
6uee55I
coutZZQ"e4 Ori.i _QZZMo(e)K0LZZe-,I
Y
110
A)+
`ueen h ""
Now h WW
;inall5 h --
New Origin h /
1+ A&B $o t%rou.% t%e C55 co-e )%o4 /e,o4E '- &i- out t%e po))i/,e output or output) &rom
t%e )u..e)te- ouput optio)AiB to Ai(B+ A,)oE 4rite t%e miimum '- m'Gimum (',ue)E 4%ic%
c' /e '))i.e- to t%e ('ri'/,e M="um+ 2
]ic,u-eZio)tre'm+%T
]ic,u-eZ)t-,i/+%T
(oi- m'iA B
X r'-omiHeABI
it M="umEM'GW7I
M="umW205r'-omAM'GBI
&or Ait "WM="umI "ZW27I "55B
coutZZ"ZZQSQI
Y
AiB 20S21S22S20S22S27
AiiB 22S20S22S27S
AiiiB 20S22S
Ai(B 21S22S20S22S27
A)+
%ii& ))?).?)-?)W?
2+ A'B Di&&ereti'te /et4ee Co)tructor '- De)tructor &uctio 4it% re)pect to O/:ect
Oriete- Pro.'r'mmi.+ 2
Ans. Constructor is automaticall5 in3o(ed when an o47ect is created and it is used to iniali=e data
mem4ers of the o47ect. Constructors can 4e o3erloaded. Thereas Destructors are automaticall5
in3o(ed when an o47ect goes out of sco!e and it is used to free all resources allocated 45 the o47ect
during run:time. Destructor cannot 4e o3erloaded.
2+A/B #rite t%e output o& t%e &o,,o4i. C55 co-e+ A,)oE 4rite t%e 'me o& &e'ture o& O/:ect
Oriete- Pro.r'mmi. u)e- i t%e &o,,o4i. pro.r'm :oit,= i,,u)tr'te- /= t%e &uctio) AiB to
Ai(B: 2
]ic,u-e Zio)tre'm+%T
(oi- LieA B //;uctio AiB
X &or Ait LW1I LZW?0I L55B coutZZQ-PI
coutZZe-,I
Y
(oi- LieAit "B //;uctio AiiB
X&orAit LW1I LZW"I L55B coutZZQSQ
coutZZe-,I
Y
(oi- LieAc%'r CE it "B //;uctio AiiiB
X&orAit LW1ILZW"IL55B coutZZCI
coutZZe-,I
Y
(oi- LieAit ME it "B //;uctio Ai(B
X&orAit LW1I LZWIL55B coutZZMSLI
coutZZe-,I
Y
(oi- m'iAB
Xit AW@E1W2ECW0I
c%'r !WV]VI
LieA!E1BI
LieAAECBIY
Ans.
S S S S
8 "0 )9
;unction %i& to function %i3& re!resent function o3erloading %!ol5mor!hism&.
2+Ac+BDe&ie ' c,')) App,ic't i C55 4it% &o,,o4i. -e)criptio: A2B
111
Pri('te Mem/er)
A -'t' mem/er A"o A A-mi))io "um/erB o& t=pe ,o.
A -'t' mem/er "'me o& t=pe )tri.
A -'t' mem/er A..AA..re.'te M'r*)B o& t=pe &,o't
A -'t' mem/er $r'-e o& t=pe c%'r
A mem/er &uctio $r'-eMeA B to &i- t%e $r'-e ') per t%e A..re.'te M'r*) o/t'ie-
/= ' )tu-et+ EDui(',et A..re.'te m'r*) r'.e '- t%e re)pecti(e $r'-e) 're )%o4
') &o,,o4)
A..re.'te M'r*) $r'-e
T W ?0 A
Le)) t%' ?0 '- T W >7 1
Le)) t%' >7 '- T W 70 C
Le)) t%' 70 '- T W 00 D
Le)) t%' 00 E
Pu/,ic Mem/er)
A &uctio EterA B to ',,o4 u)er to eter (',ue) &or A"oE "'meE A.. 9 c',, &uctio
$r'-eMeA B to &i- t%e $r'-e
A &uctio Re)u,t A B to ',,o4 u)er to (ie4 t%e cotet o& ',, t%e -'t' mem/er)+
A)+
class A!!licant
L long ANoM
char NameO)WPM
float AggM
char #radeM
3oid #radeRe% &
L if %Agg B C 0/&
#rade C _A<M
else if %Agg BC XW 22 Agg A 0/ &
#rade C _6<M
else if %Agg BC W/ 22 Agg A XW &
#rade C _CM
else if%AggBC.. 22 Agg AW/&
#rade C _D<M
else
#radeC _E<M
N
!u4licG
3oid Enter % &
L cout AAQVn Enter Admission No. UM cinBBANoM
cout AAQVn Enter Name of the A!!licant UM cin.getline%Name,)W&M
cout AAQVn Enter Aggregate Rar(s o4tained 45 the Candidate GUM cinBBAggM
#radeRe% &M
N
3oid Result% &
L cout AAQVn Admission No. UAAANoM
cout AAQVn Name of the A!!licant UMAANameM
coutAAQVn Aggregate Rar(s o4tained 45 the Candidate. U AA AggM
coutAAVn #rade O4tained is U AA #rade M
N
NM
2+A-B A)4er t%e Due)tio) AiB to Ai(B /')e- o t%e &o,,o4i. 2
class Student
L int RollnoM
char SNameO)/PM
float Rar(sM
!rotectedG
3oid Result% &M
112
!u4licG
Student% &M
3oid Enroll % &M
3oid Dis!la5 % &M
NM
class $eacher
L long $CodeM
char $Name O )/PM
!rotected G
float Salar5M
!u4lic G
$eacher% &M
3oid Enter % &M
3oid Show % &M
NM
class Course G !u4lic Student, !ri3ate $eacher
L long CCodeO"/PM
char CourseNameOW/PM
char StartDate O0P, EndDateO0PM
!u4licG
Course% &M
3oid Commence% &M
3oid CDetail% &M
NM
AiB #rite t%e 'me) o& mem/er &uctio)E 4%ic% 're 'cce))i/,e &rom o/:ect) o& c,'))
Cour)e
AiiB #rite t%e 'me) o& ',, -'t' mem/er)E 4%ic% i)/'re 'cce))i/,e &rom mem/er &uctio
Commece o& c,')) Cour)e
AiiiB #rite t%e 'me) o& ',, t%e mem/er)E 4%ic% 're 'cce))i/,e &rom o/:ect) o& c,'))
te'c%er+
Ai(B #%ic% t=pe o& i%erit'ce i) i,,u)tr'te- i t%e '/o(e C55 co-eJ
So,utio
%i&. 3oid Commence% &, 3oid CDetail% &, 3oid Enroll % &, 3oid Dis!la5 % &M
%ii& CCode, CourceName, StartDate, EndDate, Salar5,
%iii& 3oid Enter % &, 3oid Show % &M
%i3& Rulti!le inheritance
0+ A'B #rite ' $et2;rom1AB &uctio i C55 to tr')&er t%e cotet &rom oe 'rr'= ALLK L to t4o
-i&&eret 'rr'=) O--K L '- E(eK L+ T%e O--K L 'rr'= )%ou,- cot'i t%e (',ue) &rom o--
po)itio)A1E0E7EF+B o& ALLK L '- E(eK L 'rr'= )%ou,- cot'i t%e (',ue) &rom e(e po)itio)
A0E2E2EFB o& ALLK L+
EG'mp,e: I& t%e ALL 'rr'= cot'i) 12E02E7>E>8E?@E@0 t%e O--K L 'rr'= )%ou,- cot'i 02E >8E@0
'- t%e E(eK L 'rr'= )%ou,- cot'i 12E7>E?@ 0
A)+
3oid #et);rom"%int A''O P,int n, int OddO P,int E3enO P&
Lint 7C/,(C/M
for %int iC/M iAnM i11&
if%i@)CC/&
E3enO711PCaOiPM
else
OddO(11PCaOiPM
N
0+ A/B A 'rr'= $K70LK20L i) )tore- i t%e memor= ',o. t%e ro4 4it% e'c% o& it) e,emet)
occup=i. ? /=te)+ ;i- out t%e ,oc'tio o& $K10LK17LE i& $K0LK0L i) )tore- 't 2200+ 0
A)+
Address of #OiPO7PCaddress of #O/PO/P 1%i?num4er of columns !resent in arra5 17&?si=eof%element&
Address of #O"/PO"WPC-)//1 %"/?)/1"W&?0C-)//1)"W?0C-)//1"9)/CW8)/
0+ AcB #rite ' &uctio i C55 to per&orm De,ete oper'tio o ' -='mic',,= ',,oc'te- 6ueue
cot'ii. Mem/er) -et'i,) ') .i(e i t%e &o,,o4i. -e&iitio o& "ODE: 2
)truct "ODE
113
X,o. MoI //Mem/er "um/er
c%'r M'meK20LI //Mem/er "'me
"ODE SLi*I
YI
class `ueue
LNODE ?front, ?rearM
`ueue%& LfrontCN*''M rearCN*''MN
!u4licG
3oid AddI%&M
3oid Delete%&M
NM
3oid `ueueGGDelete%&
Lif%frontCCN*''&
coutAAQ`ueue is em!t5QM
else
LNODE ?t C frontM
front C front:B'in(M
if%frontCCN*''&
rear C N*''M
delete tM
N
N
0+ A-B #rite ' DSUMAB &uctio i C55 to &i- )um o& Di'.o', E,emet) &rom ' "G" M'triG+
AA))umi. t%'t t%e " i) ' o-- um/erB 2
Ans.
int DS*R%int AO P,int N&
Lint i, dsum"C/,dsum)C/M
for%iC/MiANMi11&
Ldsum"1CAOiPOiPM
dsum)1CAON:%i1"&POiPM
N
return %dsum"1dsum):AON,)PON,)P&M,,4ecause middle element is added twice
N
0+ AeB E(',u'te t%e &o,,o4i. po)t&iG ot'tio o& eGpre))io+
TrueE ;',)eE "OTE A"DE TrueE TrueE A"DE OR 2
NO$ AND AND OR
ansG $rue
2+ A'B O/)er(e t%e pro.r'm )e.met .i(e /e,o4 c're&u,,= '- &i,, t%e /,'*) m'r*e- ')
)t'temet1 '- )t'temet2 u)i. )ee*.ABE )ee*pABE te,,pAB '- te,,.AB &uctio) per&ormi. t%e
reDuire- t')*+ 1
]ic,u-eZ&)tre'm+%T
c,')) ITEM
Xit IoIc%'r I'meK20LI&,o't priceI
pu/,ic:
(oi- Mo-i&=PriceABI//t%e &uctio i) to mo-i&= price o& ' p'rticu,'r ITEM
YI
(oi- Item::Mo-i&=PriceAB
X&)tre'm ;i,eI
;i,e+opeAPITEM+DATQEio)::/i'r=Rio)::iRio)::outBI
it CIoI
coutZZQItem "o to mo-i&= price:QI ciTTCIoI
4%i,eA;i,e+re'-AAc%'r SBt%i)E )iHeo&AITEMBBB
Xi&ACIoWWIoB
XcoutZZQpre)et Price :QZZPriceZZe-,I

;alse
$rue
114
coutZZQc%'.e- Price :QI ciTT PriceI
it ;i,epo)WCCCCCCCCCCCCCCCCCCCI //St'temet1
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCI //St'temet2
;i,e+4riteAAc%'r SBt%i)E)iHeo&AITEMBBI
Y
Y
;i,e+c,o)eABI
Y
A)+
Statement"G int ;ile!osC;ile.tellg%&M
Statement)G ;ile.see(!%;ile!os:si=eof%+$ER&, iosGG4eg&M
2+A/B #rite ' &uctio i C55 to cout t%e o+ o& P<eQE or PS%eQ 4or-) pre)et i ' teGt &i,e
P)tor=+tGtQ+ 2
I& t%e &i,e P)tor=+tGtQ cotet i) ') &o,,o4):
He is !la5ing in the ground. She is
!la5ing with her dolls.
T%e output o& t%e &uctio )%ou,- /e
Cout o&<e/S%e i &i,e i): 2
A)+
SincludeAfstream.hB
SincludeAstring.hB
SincludeA!rocess.hB
int count%&
Lifstream ifile%Ustor5.tJtQ&M
if%Difile&
L coutAAQcould not o!en stor5.tJt file UM eJit%:"&M N
else
Lchar sO)/PM int cC/M
while % Difile.eof%& &
LifileBBsM
if %%strcm!%s,QHeQ&CC/&EE%strcm!%s,QSheQ&CC/&&
c11M
N
ifile.close%&M
coutAAQCount of He,She in file is GQAAcountM
return cM
N
N
2+AcB #rite ' &uctio i C55 to )e'rc% &or c'mer' &rom ' /i'r= &i,e PCAMERA+DATQ cot'ii.
t%e o/:ect) o& c,')) CAMERA A') -e&ie- /e,o4B+ T%e u)er )%ou,- eter t%e Mo-e,"o '- t%e
&uctio )%ou,- )e'rc% '- -i)p,'= t%e -et'i,) o& t%e c'mer'+ 0
c,')) CAMERA
X,o. Mo-e,"oI &,o't Me.'PiGe,I it [oomI C%'r Det'i,)K120LI
pu/,ic:
(oi- EterAB X ciTTMo-e,"oTTMe.'PiGe,TT[oomI .et)ADet'i,)BI Y
(oi- Di)p,'=ABX coutZZMo-e,"oZZMe.'PiGe,ZZ[oomZZDet'i,)ZZe-,I Y
,o. $etMo-e,"oAB X retur Mo-e,"oI Y
A)+
3oid search%long RNo&
Lifstream ifile %UCARERA.DA$Q, iosGGin E iosGG4inar5&M
if % D ifile &
L coutAAQcould not o!en CARERA.DA$ file UM eJit%:"&M N
else
LCARERA cM int foundC/M
while%ifile.read%%char ?&2c, si=eof%c&&&
Lif%c.#etRodelNo%&CCRNo&
Lc.Dis!la5%&M foundC"M 4rea(MN
N
115
N
if%foundCC/& coutAAQgi3en RodelNo not foundQM
N
7+ A'B #%'t -o =ou u-er)t'- /= Se,ectio '- Pro:ectio) i re,'tio', ',.e/r' J 2
Consider the following ta4le ER'O\EE and salgrade and answer %4& and %c& !art of the `uestionG
T'/,e: EMPLOYEE
ECODE NARE DES+# S#RADE DOJ DO6
"/" A4dul Ahmad E]EC*$+VE S/. ).:Rar:)//. ".:Jan:"80/
"/) Ra3i Chander HEAD:+$ S/) "):;e4:)/"/ )):Jul:"809
"/. John Ken RECE$+ON+S$ S/. )-:Jan:)//8 )-:;e4:"80.
"/- Na=ar Ameen #R S/) "":Aug:)//X /.:Rar:"80-
"/W ri5am Sen CEO S/" )8:Dec:)//- "8:Jan:"80)
T'/,e:SAL$RADE
S#RADE SA'AR\ HRA
S/" WX/// "0///
S/) .)/// ")///
S/. )-/// 0///
A/B #rite S6L comm'-) &or t%e &o,,o4i. )t'temet): 2
%i& $o dis!la5 the details of all ER'O\EES in descending order of DOJ.
%ii& $o dis!la5 NARE and DES+# of those ER'O\EES whose SA'#RADE is either S/) or SO..
%iii& $o dis!la5 the content of all the ER'O\EES ta4le, whose DOJ is in 4etween /8:;e4:)//X and
/0:Aug:)//8.
%i3& $o add a new row with the following
"/8, _Harish Ro5<, _HEAD:+$<, _S/)<,</8:Se!:)//9<, <)":A!r:"80.<
AcB $i(e t%e output o& t%e &o,,o4i. S6L Duerie) : 2
%i& SE'EC$ CO*N$%S#RADE&, S#RADE ;ROR ER'O\EE #RO* 6\ S#RADEM
%ii& SE'EC$ R+N%DO6&, RA]%DOJ& ;ROR ER'O\EEM
%iii& SE'EC$ NARE,SA'AR\ ;ROR ER'O\EE E, SA'#RADE S
THERE E.S#ARDECS.S#RADE AND E.ECODEA"/.M
%i3& SE'EC$ S#RADE, SA'AR\1HRA ;ROR SA'#RADE THERE S#RADEC<S/)<
A)+ Selection means selecting some rows%tou!les& from a relation according to gi3en condition
e.g. i
!riceB)/./
%+tem&
ro7ect o!eration 5ields a 3ertical su4set of a gi3en relation%i.e. select all tu!les containing onl5 gi3en
columns of a relation&.
e.g. g
NARE, DES+#
%Em!lo5ee&
%4&%i& SE'EC$ ? ;ROR ER'O\EE ORDER 6\ DOJ DESCM
%ii& SE'EC$ NARE,DES+# ;ROR ER'O\EE THERE SA'#RADE +N%_S/)<,S/.<&M
%iii& SE'EC$ ? ;ROR ER'O\EE THERE DOJ 6E$TEEN _/8:;e4:)//X< AND </0:Aug:)//8<M
%i3& +NSER$ +N$O ER'O\EE
VA'*ES%"/8,<Harish Ro5<,<HEAD:+$<,<S/)<,</8:Se!:)//9<,<)":A!r:"80.<&M
%c& %i& CO*N$ S#RADE
) S/.
) S/)
" S/"
%ii& ".:Jan:"80/ "):;e4:)/"/
%iii& NARE SA'AR\
A4dul Ahmad )-///
Ra3i Chander .)///
%i3& S#RADE SA'AR\1HRA
S/) --///
>+A'B 3eri&= t%e &o,,o4i. u)i. trut% t'/,e: 2
116
X5Y+[WAX5YB+AX5[B
A)+
] \ c \.c X5Y[ %]1\& %]1c& AX5YBAX5[B
/ / / / 0 / / 0
/ / " / 0 / " 0
/ " / / 0 " / 0
/ " " " 1 " " 1
" / / / 1 " " 1
" / " / 1 " " 1
" " / / 1 " " 1
" " " " 1 " " 1
>+A/B #rite t%e eDui(',et 1oo,e' EGpre))io &or t%e &o,,o4i. Lo.ic Circuit : 2
Ans. ;C.`<1.R<
>+AcB #rite t%e SOP &orm o& ' 1oo,e' &uctio ;E 4%ic% i) repre)ete- i ' trut% t'/,e ')
&o,,o4): 1
Ans.
;C*<V<T<1*<VT1*VT<1*VT
>+ A-B Re-uce t%e 1oo,e' eGpre))io u)i. !-M'p: 0
;%A,6,C,D&Cf%/,",),-,W,X,0,"/&
Ans. ;%A,6,C,D&C6"16)16.
6"Cm/1m)1m01m"/C6<D<
6)Cm/1m"1m-1mWCA<C<
6.Cm/1m-1m)1mXCA<D<
;%A,6,C,D&C6<D<1A<C<1A<D<
8+A'B I et4or*i.E 4%'t i) #A"J <o4 i) it -i&&eret &rom LA"J 1
A)+
TAN is Tide Area Netor( 'AN is 'ocal Area Networ(
S!an across countries
'ow data rate
Diameter of not more than a few (ilometer
High data rate
8+A/B Di&&ereti'te /et4ee XML '- <TML+ 1
A)+ +n H$R'%H5!er$eJt Rar(u! 'anguage&, 4oth tag semantics and the tag set are fiJed whereas,
]R' %e]tensi4le Rar(u! 'anguage& is a meta:language for descri4ing mar(u! languages, ]R'
!ro3ides facilit5 to define tags and the structural relationshi!s 4etween them. All the semantics of an
]R' document will either 4e defined 45 the a!!lications that !rocess them or 45 st5lesheets.
8+AcB #%'t i) #E12+0 J 1
A)+ $he term #e/ 2+0 is associated with we4 a!!lications that facilitate !artici!ator5 information
sharing, intero!era4ilit5, user:centered design, and colla4oration on the Torld Tide Te4. A Te4 )./
site allows users to interact and colla4orate with each other in a social media dialogue as creators
%!rosumers& of user:generated content in a 3irtual communit5, in contrast to we4sites where users
%consumers& are limited to the !assi3e 3iewing of content that was created for them. EJam!les of
Te4 )./ include social networ(ing sites, 4logs, wi(is, 3ideo sharing sites,hosted ser3ices, we4
a!!lications, mashu!s and fol(sonomies.
8+A-B Out o& t%e &o,,o4i.E i-eti&= c,iet )i-e )criptA)B '- )er(er )i-e )criptA)B 1
AiB M'(')cript AiiB ASP AiiiB (/)cript Ai(B MSP
A)+ Client side scri!ts are Ja3ascri!t, and 34scri!t, ser3er side scri!ts are AS, and JS
* V T ;
/ / / "
/ / " /
/ " / /
/ " " "
" / / /
" / " /
" " / "
" " " "
117
8+AeB $re't Su-ie) Ui(er)it= i) )etti. up it) Ac'-emic )c%oo, 't Su-er "'.'r '- p,'i.
to )et up ' et4or*+ T%e ui(er)it= %') 0 'c'-emic )c%oo,) '- oe '-mii)tr'tio ceter ')
)%o4 i t%e -i'.r'm /e,,o4: 2
'aw school )W
$echnolog5 school W/
Admin center ")W
6usiness school .W
%i&Suggest the most suita4le !lace% i.e school,center& to install the ser3er of this uni3ersit5 Sugewith a
suita4le reason.
%ii&Suggest an ideal la5out for connecting these school,center for a wired connecti3it5.
%iii&Thich de3ice will 5ou suggest to 4e !laced,installed in each of these school,center to e&&iciet,=
connect all the com!uters within these school,center.
%i3&$he uni3ersit5 is !lanning to connect its admission office in the closest 4ig cit5,which is more than
.W/ (m from the uni3ersit5.Thich t5!e of networ( out of 'AN,RAN or TAN will 4e formedFJustif5 5our
answer.
%f&Com!are O!en Source Software and ro!rietar5 Software.
%g&That are coo(iesF
A)+
%i&Admin Center 4ecause it contains maJimum num4er of com!uters %using 0/:)/ rule&.
%ii& 6*S to!olog5 is the 4est suita4le ca4le la5out.
%iii&Switch
%i3&TAN 4ecause 'AN and RAN cant not s!anmore than "// (m.
A&BComp're Ope Source So&t4're '- Propriet'r So&t4're+ 1
A)+O!en Source Software can 4e freel5 used %source code is a3aila4le to 5he customer& 4ut it does
not ha3e to free of charge
ro!rietar5 Software is the software that is neither o!en nor freel5 a3aila4le%source code is not
a3aila4le,further distri4ution and modification is either for4idden or reIuires s!ecial !ermission 45 the
su!!lier or 3endor.
A.B#%'t 're coo*ie)J 1
Coo(ies are messages that a we4 ser3er transmits to a we4 4rowser so that the we4 ser3er can (ee!
trac( of users acti3it5 on a s!ecific we4 site.
'aw school to 6usiness school X/m
'aw school to $echnolog5 School 8/m
'aw school to Admin Center ""Wm
6usiness school to $echnolog5 School -/m
6usiness school to Admin Center -Wm
$echnolog5 school to Admin Center )Wm
118
)age 119 o+ 119

Anda mungkin juga menyukai