-

(With c++)

PabliMed1,..··

_' -.'~ --~----.I;!J 'f.

LAXMl PUBLIUA:TIONS (Pl VID 22.,'Go,ldon DQuet~. New Delh:i~110002"

~, • rO.··.~.l.l43._ .•.. '.25.'.2&._ .• · .. 1 .. '2 .. '. £axe.'i1 ., 1 ,011:13162279

Bn:uu::-hes:

.' lL291l~ R&d Main .. Road" lX, Cmss Cibwmajpe.t. Ban:pl0l'e (P.htme; 080-26, 61 15 611.)

• 26~ Damodatan Street, T., Nspr.jC.hennai (Rhone , 044·M 34 4/i :.\i,6)

• ,st.Bened:kt''!!, &ad.Coohin (Phone ~ tl484-2:.W W (4)

• Pan. Bazar,Bmll, .Bari, Guwahati (p~ ;: Oa61~254 36 69,.25138, 8 U '. 4-2463, .I$tFlooI"~ ~~ ,Byderabad. (P~ ~; ,o.ro..24 ''To (047),

• Mila. Tallita (!t:tm.ik, .N,D .. 366, ·Jaitandhar Cit¥' (P:hOJre : O~81·.222:l!2 72)

,. U:WA, Is',Ffooit,. S:N. Banerjee ,RQRd,. Kolkata (PhoMs, oa.a.:22 2'1 ,37 '75,2227 5Z4,7) .. 1.8~ MadllD Mclum MalwJ'll Miarg.Lucknow (Plw~ ; 0522:.220 95 7,8)

~ 128A,Bfock 3. Firat F1,oor. ,Noorani Bui~ding,L.J,.Road~M:umbal: (P~: 022~24463998) ,. &dha, GGlI'i:nd S'keet, 'Tharpagna, 'Rancbi (p~.;; 0651·200 '77 54)

W.Eis8l1,;:U: wwwJanniipUiblicatians.,oom

Tl2-a93,2.;2;IO~NEW APPR TO COMPUTER SC XU

Pric« :: h • .00 Onl~

Copyr'ghted rneleria'

1

30

:2~.

,3. 4.,

IlATABASHAND SQL .BOOLEAN.AtGEBRA.

OOM!SlNro;AflON AND NETWORK DOlIDBl?JS

8 8,

8,

o.

UNIT I : PROGRAMMING IN C ....

REV1iEW; c++ oov@m In Class-D.

De-fiJ]!_ing a symltml ;o;.a;me U8ing type'd~rkJlY";\I'(H:d and d~,fiO!~ng a m~ :us~ng #d_~,6ne diif'el;)-

tive:

Need. Fo:rTJ,seJ;..:ooti.ned, oow type; Sb'uC'~8= DefinIng a Structure, h1larLng struCitUl"@ variables ,Accessi:ng stmctu:r:e elements .• P;a:ssrrl:g strn.etunl,wFunetions las; value andrefelJen,e@' a:r;gumenfJpa:mmeter •. FUncti.on.rntumi:ng stnu::turo, An::ay ofstruetur:c:s, pass~ngoo. RlITayofslru.oture ·as ,an :argum;ent/a pa:rametertoa functiolli~

Objee,*Odented·~1

Co:noopt, of:Object Orien,ted Programming Data.rudi.n8'~ Da'ta. enca,psulati:On,. Class and Object. Abstract class and, 'Concrete class" Polymorphism (fm.pillerm.en:tafio,n of,poiymorphism. ~mg F'unction. over ~orul:lng asan emmp~e in C-++), Inheritance, Advoot:age.!J: of Obi ed Orien,ted Programming,OV(lI' ear1i&r progrrummjng m~odologi(l$.,

hnple~entatiODol 'O'bjee:,t Orieuied Programmi,n~1 tAJneepil!! in, C++

Definition ofe cJ.ag, Members ,of a cll!i!i~Data,.Membersruul M.ember :Fufi.Ct!ons (methods), Using Pi'iV8IU: and. Pub~ic visibilii,ty'm •• default visibili~ mode (:pri.vate); Member funcHon, defim.tio~ imdde ,clMs definition. and 'Olltmde cl.a:8s definitioaooiing;~ msolution operarol' ,(~:); Dec. !h\tai~.on. of,c~bJecm as :ms~oos ofa cl.:a:ss~ac~eS!!ilmg :memIDenl; from. dQjed.(~)'~ AlTay oftype elass, Obj:ects; as ~Cufictlgn ~ents-pUS!by value ,aDd pass ~Yfe,rBn!~C8j

Con!itnJ:cmr and, ~

Constmdor. Special. Cb_aract,eria,tl,es,. DeelaraUiID .M.d. DefinUhm il)fit! e(Ln.5t-nr(;t'{r!:'~][)er~u~t Co:n.~'r. Overloaded Conatruclots. Copy e~ctot'~ COOJrni.licw;t W1I:th dcfa~Utllit'[gUmr:ntsj

Destrueter; Speclarn ChHra~ri:s,fi.cai. Declaration and. d~fini.tlicm.o:fd€8~ct:o:r;

Inheritance (EXtending G~as&1!9}; Cwl.cept o:fhtherl,t8nee,. B;l:BB Cla..ss,_IJe-clVedi Class. [)~ii:n-. iug d~rive:ddtl~e..5~ Pro~ Vi:l:ibiHtYIDOO(li; S~ng~!I,'lle\'el iriberitR_nce, M,dtHev.el inneriitance ana.

Copyrighlad rnaterla

Miull:ip~e mheritamloo. Pri:va1ltely derived,. :Pu~liI€'J:y derivro.fIlndPmtl3'C:tOOly derived c~ass. t!;~tbill~IW .(lr~,mN:!'$from objects and within derived class(jes);

Data He Handtiug

N Bed. for a da.ta file.'~ 'm dahl. fi]es-mext file .and.Bina.r:r fiIDe:

Basic file o(per,IlI,1t:iorJ:EI on m:tt file: lCR:atin;giWrlit:lngJ13~ into file" :Iwad[ng a:ndM«n~Jn~ta,tion ofteJi:tf:i!om an aLrready exisUll1Ig1lex.t FMe (aeOO5s.i:ngsequ.el1t~iwly).

BL'_ ~ne;Crenti(l~ ;I)f61e Wri.' ,~t!ll. intofi1e' Se

dam. ~fi. afi.1ll'l; Im~1emenmtw:n of abGvement~Gned, diitafile handlliQg in. C++;

Con1!PO~oobs ofC++w be: W300 with fiirueJm:ndiling;.

Headetrffirne; fBtrna.m,h;:ifiIRNam. of~r-ream,rst'maJm cJ3flses;;

Openin'i! a '~. ·s;ree !~ m. (lIlt; anA apR 'mod&aj,

U3~ng ~i:clg ope:ra:wts fGil' ,mUng teD ·Itothe :111e Ilfi.d teading; .tc:xt. fromtbc :lUe;.~(). getO. p.to. ga,uiaeO mld clioseOfunctiom; De,metmg e.nd-M"-fi1e {with ,or' witho:u:!tW!~ng 0010 mnction);

Open.ing 8. [bin.a.ry rueming' in, out"end. ap,mades.;

openH:i readO~write() and. close 0 :mJ!,rti~ns;Detootlng end-(l'f~file, (iloriU:l ,lit "oV1itbOUit 'WIing oofOftllW:th::Hl}; te]lgO. tli:'llDOt ~lugO. seekpO 'i.~nctio:!'J:S.

PfI·hlilm'B •

Dcdomtion and Initiwi:zation. ofPointsnj .Dynrun~t: Dlen:lOry alloont"ionld!etillacation. operatnrs: new, delete;: Pointe:rs and Array,s: Array of Po in ten;, ifoiiHliter to an a~.y (l diimenmomd 8:rray)', Funetionn!:tuming a poin.ter,. Referenc@!variab'h~s and. use of.ali.as; FunctioB.caI.by reference. ;P01nter to strucbues:Defernnoe operumr: '" ,A>.j. slilfrof4!roncia!! $truutu:res;

Imll'E.:2 :D.M:l S1mllU:m:mES ,AttaY8

On~'~fld tw'!} ,mmms:i.Qni'lll a~~ S~qllenHal altoca,tiQn ait1!d. ,nd~ ca]~l.dBti:on;

Que d:D.mensiol.1!~. at'l'R!' 'full,)!'emal, S~g: (LineM', Bm8!J' 'searrih)l.blJserti(]ltB ofwte'!:e. mentinl'ln MTay, dele!t1on ofan eWementfrom .lUlsn'8y,. S~ (Imierl.ion!, ,selOOti~ Bubb1J.l ~rt)~ CO:O'C8It;.e(naJition oftw:l) linear 3,'rrnys •. mflrging 'ort:w'O :sol'teda;mYSi

''I\v~diim.e!lUlio:na1lQl'8:yra::'Tra:yersal,Fin.ding:s,umldliff~regeeoftw,I)N:!!M,an~)i\S(X)n!tai.w.ng

n:umi(\ri'e V,aJj,Ues,: btte:rcJir!liDging Row ood Column e~e-merlits· :iiaa two' dimensionall:~

Staek. ~ Md [.inkOO.l1Upl",men,tation ol,staek)

Oper8!tion:G on. Staik (PUSH and POP) oodi:ts Imp,l.emen,ta;ti<m, :in O+'*'~ Co;ll.VJJtlclQg cxpresaionafrom INFIX wPOS,TFIXnoblitcio:n and evwuatian.,ofPoetfutexpJi~8fun;;

Qu,eu.e:. (Circu_lar ~y andU__Uedlmp~emeldatlOD)

OperatilUtEi an. Qj~eue (lnsed; and Delete) ,end its Implem.entotionm. CoH.

IOOT'S.DATAJlj;.';E ANDSQI~ Datooase C'OW,'IQ:!ts

RclEliiiorull data. model:CCuoopt.ofdnm.a;W.tuple'. rol!!!tlon •.• by. rprlm.ar:y 'k:ey, aIDter,"ate k;ey. carulidll!:t@ key;, ll@~ational algebra: Selectie:n. Pmj~ Um(ln and C~!!~anproduct

Copyrighled materia

Structured. 'Query Lmlguo.lf:!

G&ne',ral Conoopts:: Ad'Vanta~ ofl1l!s,in,g SQL,.Da:m De:rmiltionLooIDla~ Md D~t:a MMipu-

hlHoR LMgulligt!;

:01lJ~.t1i"P@: NUMBEJ&. CHA.RJl.CTER DATE; SQL commands"

C.REATE TJ\BLE, DBOPTABLE.ALTER TABlE.UPDATE. .. SIET ...• ~~ NSERT .. n'~IETE; SELEC;l't DlSTINCTtFROMt:wHEREj IN'jBm'WEEN, GIlOlJP BY,. HAVING. ORDER

SQL fURl!tiOlns: SUM:, .AVG. COUNT.·P:dJ\X ~dM.iN;

No .. .lmp[l,tltlito:l'J!b1I!tiOI1, 'CJf the nhOVI! ~mcj],tiamed ,to:mm!im.ds ooll[d be dOlle, Wl mly .8QJ:, sllpoortodi SIJt\w.!lni;.

UNJ~,r4 • BOOlli.EAN AI.OE.RRA

E,vO:~utlon QfBcolean JlJg¢b!ra" .Bh\~·'Val.ued 'Q;!;la:n:titi~, Boo~e~ Vari.3ibJe,. Bool'eD!!l C'on~ smnt and .Boo~elmJ Opcrailro.m: ANn, OR" NOT;, TruthT'Ilbfe;s: IC]031il!~~ Pronl\lrb. Com:muiOO:tD:ve lAliw. ~illl'l.ti.V'CI :t.a'W',.ldenrtityL~. mV~l',se Lll.w. PrinlJilipl(! m,Durdi1¥ •. ldempgt;ent Law" ~Ojs;" ~ril)Uitive lUIw "Absl)l'pi'ion Law, ~nvdlrot!(lH l~w. DsM~OCoUl'sI..:!iw and! tW.!i:l"~ppHca.ti.olllis;

rObtaini:ng' SWllI .. @-f Prod:w:m: (SOP~'andlT<lducl Qr Sum. (POS} fGml: &omihc Truthta;ble.

Redncing Boolean EXp:ressian. rsop :and. BOS,) to .~ts; minimal feiM1j Use ,of Kama.ugh !I!oJap for l1lillimlsati.O~. efBootean. exP'reSBl(lM ~~p m 4variable,s);

Basic Logillc G3Jtes (N'Or'ID'.AND. OR, NANn. NOR.) find thelrllSe~n cl~ts ..

UNIT 6,; QQMMtJNICNrnON ANONETWORIrCONCEp:'fS Ev,olution .o,fN:etwGJ'klin,g;: ARPANET. [lloornet, IiIlIi.wrspnoo;

Di,fierent ways of send1Ilg dB/ita across th" netwm'k. w~.tih.re(~:renooto .~.witc.hij]g tech!1iq~u~:. IData.Co:mmttnicatwn 'TiE!!ntIlno.logie.s:: Conc:ept;ofCha:nneJ" Biand!, Brund~t:h (H':z;, KH:I,:, l\fH:;:)

and. Datn.kBnsfer mte (bps, Khps,Mbps:, Gbps.Tb,Pf,i);

'bansmissiQnmed;ia: 'heisted pair cahle. coaxial cab~e., o:pth::al tiber~ il:'J!f~ r;adio linkj

m1ml<Warv'B lin.k and. sa.W].HwruiJDk.

N'letw'lM-k. devilles:: Modeut~ RJ46 connector'! Bthemet Cant, Hub:r SwliMh,. R!iluwj Gateway;, Oiffe:relOlt TGp@logiie~BlUli, ,stm'~ 1'roo: 'Coooopm ortAN. WAN~ MAN~

ProOOcol~ TCPlIP~ FHe'fi'an~r~r Pr~to(ol(FTP)~PPP., I..eve'l~Rem.om Login (Teln.et). Internet, WiQrel~llbf~e Comn:Ii1.!~m:~t:i.or:lt GHM, GD'MA..\VIJ .. aG·. SM8.Vo]cemaiLElectronic!M.ai]. Chat .. VideoOmfemncing;

.N,ctwl0r'k Security Corn.OOJ2M; GllbE'.r.taw .. Fil~WaJ!t Coot~!M. Hac\ere nnd Cmckem;

Web F'ages;Hypel"'TeN:t Markm~p LainlJlllage ~HTMJ["'), ,a:!dlensibte Markup Langu.age (D1'J .. ); H),pet TextTranst"el' ,ProOOco~. (HTfP); DO'main .Nam~;p URL, Pl'Ql;!)co~ .Add_~; We~ii.te. Web Browser:, W,eb Servers; Web Hosting.

Copyrighled materia'

1 'Revie,w of C++, . '"",Of •• n uu I , '"u."",.. n,I,", u •• , ' s.,....;130

:1,,3 StWCi!l,,.;:re 'M a ,0++" 'l?:rQsmm, .",. """"""",,,,,,,,",,, ''''''''''T>'''"'''''''' ., •• '.,.'." ."'.'",,. •• ."."", •• ,.", ... , • ., •. " •• ., .5 :1ll,4, It1ip~tfOutAAt, mth an, and, COlli!; • ", . ., , .• " .•• , .• " •• , •••• " .•• , .•. " ..... ," , ••• ." .•• , .• ,., •• , .•. ."., ,.,. ", .•• , .• , •• ." ... " •• , .•• , .• 5,

.:t6 'Creei,oog ;S~IJI!r..ce, :File, ., .. ,.,., .. ..,. ... , .. , .. ..0 ."" ..... "".,,, ., .... ".,.,,, ... "" • ..0 .. " ... "" ... " .n ......... "., .... ,., ........ "., • .nun .. 7' 1,,6 ,Cofflp~lin;g' .2i,Qd IAnkrng '.,.".",., .. ,'," ".' ,,',',., .• '.'.,',','., .• '"""",." ... ",.""", •• , .""' •• ,'.,.",,,,,,,.,,,,_,,,.,' •. ,,'.,., .• , ......• ',, .• ", •. " .•• ,.,.",."", 7

1. '1 auoni ng the· ProJrrBl'mI " .. ,,"""" ."" " .. ", .... """" .. ".", .""."" .. """"" ., .. ,," ".' .,," ."""",, .... ". " ... ",."' "' .. ,,.,, ,., ... ,."" .. 13-

1,.8 Hot F::eys 'in Tlrroo' C++ " .. ,,", .. " .. , " .. ".,.,,""" n" .". n •• ,," .",. .,," " ", •• ".,,"" .n." .. " •. "",, , ,," •. """,, , 9

]1.,:9 Types. :at En'tGit5; ,and :Debu,ggWlfig ., .,",. "I .,"","",",. ,", .,."",,. "" "" • ." • .,,, """" "","",,,,,,," """""""""" .. '.,", .,"" "".,. :~,l LJJ) Thlwnl1l ... , .• '." .' •• ,.,',' ""1"" """"""""'1 "'I" 1"""'" '1"""' I ... to." .,,".,, .• I'" I'" Ito ."1 .• ''"' •.••••• '1 •• '1' •• ''''''1''''"'''''1'''1''''1 ."1· •••••• '1 'E2. 1.,11 ~. '~srnns, ,~~,i"~!I,!,I!lII!I ,~;t; ~ r.+!I·Ii~,!I·Ii,..~;. ~,i·Ii"',,·Ii,..i .;Pi Ii", ~,r.",~·r.~,i·i..,,,· .. '';;;;' ~,r." i..,~,i~J~·ilr. ~ i,r.i i"';;;·illr.i'iI.o;~ .'Ii"·iri." .,r.i'.'r. i·.,r.i'.,r.;;;,i"''''i '.';' i,"';,i'."'i'.';,i'i ;;;,.,.",,,"0;,. :26

.t.12 'Type COHv,ersion 'j .• """,,,.,,,,,,,,,,. ". "j"" """""""""" ''''''j' ""'''' I., .,,. .•.. ,., ,.", .. ,.,,.,.,, .. ,,., " ...... ,.", •..•. ".", ... ", ..... ,,' '21 L 1.3 S~boHc C'o~t.a.:I1!iI:.s •. ,.." .• ,., .• ",,, .... " • ., •.•• """,. "".", .•. ,."., .•• ,." , •.. " .• ,",.".", .• " " ... ,,, •• ,.,, .• ,, •••• ,,. ", •. ,., ,.", .• :27

1.14 Con1l:ml,61tmctg:res, ... " .. " .... ,,,,' "'.n .... ".," •.• ",.".,,"" '" ...... ',t.',." .'''.,' .... ,'''',",.,,,, .• ',, ,.t •• '''."' ","""",,j,' .. ,.""." •.• ,.00

&vi£w Que,~'.~' (lind ~n::~<;es "., .•. , .•• U'''''''''''''''''''''''''''''''.''''''.,,,·.,, ••• ,u',, •• ,,,, •• ,,,, .•• , .• ., •• ,."", 00 1.,15 Coni>O'le [n;PlltiOutPIl:t Ope.ra;t[@ns""."",,,,,,,, .. ,,,,,,,,.,,.,.,,.,.,,,, .• ,,, ",' "",,,,.,",,,,.,,,.,,.1"""""'''''''.',1.,,,,,,,,,, .'.' .,,,' •.• ,'.', 62

1.]'6 'M'an]pu]a!~, .,.., " n,," •• "" "'" •. ""." •••• " .... " •• " •• " •• " •• "." ... ,," " ... "." ..... " .. """"""'"." ... "",, ... ,. ""1"'" •• " •.• "" •• '72

1, 17 ~,ys" ,,, ... ,, .. " . ., """ ""'" , ... ".""" ... ,, .,," '"" n •• ,," .,," ... " ... ,," .""." .. " .. " .. " .. ", ... ,," """, .. " """"","'" " " •• ".,."." ••• " •. ". '73

l:ruB Functicm8

,82

1.11l9 ROOI!lmian """I..,-",-.,-.-" ... ,.,,,TT'- '98

1.20 ,scope Ru~e8 of'FuncltiioM Hi'!td V.ari:llJblelii, ,,, "",.,'"" .. ", , 'j"'."""''''"' .",."",,","j., .. ,,,, ,,, . .,, 'Ull

L21 DllJtaSuH;,tl;Cfl~' .. '" .. " .. ,,'.,," ""'''''''''' """"""""""" '''''''''''''' "'''' ."''''''' """""'''',',''''j ,,"I .. I,., """"',,,,,,,,, ",, ".".104

.Rij:1vi-l'luJ' Q'u,!,!gl:lOO!,o;; Otl'!Jl; F4Wj'Ses """""""",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",,",,,,,,,,,",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 1J9

1.22 Defin~~g~rmb(i~, N:a~!lS~~g '~l?GdcrKc.mO'rd .. " ..........•.......•.... ". , ., .. ," , 0" •••. "'"'°.121,

:1,23; DeOOing'8 ,M'acro,'Ug#~ '~ctiw ".,,, ,." ,., .. " .. , ,' ..•..... , .. u , " •• ,.;, ••• 128

1.2<1 M9.Cl'05 'VIfij1t.b . .Argument:!;!, , ".,,,,, .• , .• ,." """"""""""""""." .•••... ".,., .• ",., "" ,.".""".,, ••••. ,., 124!

1..25· Ni'ee-d for' User D!~din,ed!D!:l!m ~e ","',",',"",."'" .• , """""'", ... ,., " ,,, ".,.""",,,,,,,,,,,,, ",,,,",',''","',",,',, ... , 127

R:eufew ,QUest/OlliS and hnise.'l . "" .. ,," .,," "'" .,," .,,"", .. " I! " ,,"", .. " ""'., """, """,, ,,.,,'" •• ,, 129

~

Copyrighlad materia

:2 StnJ}.ctures ,.;u. II_.., ;p; • .., iI iIi ,.p • ., i:t 1111 "."t.,tj ,,'i,L.;jo,_.J.,:.,~""',., ,;P;''';''It'" .. '' 131''I!!!!II''''''!,118

2:.1 Defimng B. Sitt.mI':tu::r:e '.n •••• , •• ,., •• ,., •• ,., ••• , ••• " , , •• , , , •• " •• ,'.+',,.. '"., , , •. ~.31

,2:;2, 'Dechiiriil1!8 S.B'ti:~! VB:t:i.a'brnes """ "" .. , ,," " .. " " .. ",. ." .. ,," .",. ." ,"'" ", •. , .. " .. "t1 ",",", .• ",."." '~M

,2.3 ,~5~ng ,s!nc~Elie'ment5 " .. " .. " .. ".""'" .. """" ,,",,.,, .. "" •...... " " ,. " .. ' , .. "' , ,. '[$4:

'2J3.1 StrlJeture A:s.simments .•. " ,"" .. " .. " .. "",, .. "" .. " ,," .",,,n. " " n ., '."",,. 'Tha5

'2,,3;2N'~ SitrPdum j, "' •••• "'.,. " ,., .. ,.,,,., ,, .. ,,.,,,., .. ,, •••.•• ,, " "" • "" 'illaG

.2~4· 'p,~~ng ,strucrtures.w .Ftmcl~ons U,,". """"""" .,,""" •• "",, .. "u""''' .,," "." .. " .. " ' , ,.",.,", •• ]:39

'2~.4·.l Pa~by:Va1,u~ .. " •.. , ,."" , .. , , ...• , , " " '.,'"'.,,, .. ,," .. "' .. " ... ""."". " . .,.. ."""",,. ", .. ..,. .",.." .. "" " .. ,," 1.89

:!;t,·l2 :P'ass; &1\1' .Reference , ..• ",,, " , " ,.,,,., " .•... , ,.,., " , •.. , , "., "., , ., , ", , ..• ,~43

'2~5 F'ullct[oJ\ lRet~miDg ,stf\IU:tUl'e' ., , •. , ••••• ' •••• , , , , , .. " .. ,.,.,', •• , ,,,.".,,, , , ,., .. no " ..• IM

'2~.6 Structtl.I'eSand.Ar:ra.JI!1i uno no ."" oon •• n no ""."'" " c>.,," ,," " •• "'"""""'-H."''''''''''"".,d..,,,,, .• _ll::Z

2.:1),1 M.EI:y ,ofStTIillict,m f' ,.'ifOV·"'j·"t'"t"' ','"p.'''ij.;.'.,.''''' 't;j.;j'tt',.· it.;';·j'ti"im "'t. '" HI"" • 1ft

:2A:t:2, .Ar.rlliJliS, ·\\O\i:tlb~fI. EUl'il!i!CWt,e,s , •. , ,,,.' , " ••. "."."" " ••• " "'.,,.'''" •• n .,," ,," .,,"""" 1.5"0

:2,.{t3P~n:m M: An-ay >0£ S'trod:UfeS 8:8 an Awmentla Param.eter

ft,Q fII function ......... , ... , .. , ........... , ........ -." ....... -,-, .. - .. , .. -.,-- ... -.-, .. --,--.,-.,-,--.,--.-., '[.51

.R~t!ieuJ Qucsti:cJ'l.$ and Ext!~J!; •........ " " .. "' ..•••. '."' •. " , .. ' ....• ,.,. ,." , •. ""., " ".,,,.,, •• , .. " 161

S Ob"ect Or.ieDood.Pl"O-"'-

3.::Ii. COfioopt, cfObjoct Ori@fitmi! Programming , •.... , .. " " , .• ,,,., .. n , " ,,,. ]ill9

3.1.1 Da:m Hiidiing , , .. " "." ".,', .• , " .• ,', "" .. ". "" .. " .. ,," ."".,.. .. ,," .,," ." .. "" .,.. ."". ]'7{1

3 •. 1.2 'Da,Ul, ,Ab~b;ac~Qfi M.d, 'In~pmtaJ!t;ion .• , , , •.• , •.•. " •...• " •• ,',. ,,', •.• ,. ., , •• " .. ",'" .. ". " •. ,," "n ." .•.• 110

:B.1.8 Chtss; ~d Obj',eel: . no no ."" .. n.n •• n ""c' " .. " .n" "." " '''_., ~ '''~ •• ~~ ~~ ••. ,. 1.11.

3.1.4 ,AbstliJBlct ClUB and CoMlC1:!erte. ,CJ.a:ss no " ""' no n , .. ,.u ,.1'71

3 . .1.5 P~]rn~ ."",,,m " .• " ""." " .. " .. " " .. "",, , • ., " " ,,., " .•• 1:71.

11'7.5

8.2 AdJvanb.ges efObjiect Ori:ented Pro.w:mmi:~g Over IEarJie'l."Programmii:lllg

Me.tluOOflmegie.!> ,.,."." •• "." .. """".,, •• "."., "."" "".,," .• " " ," .",. ""."', "" .. ,.".,, " ,,, .. , ·.,, "u •• ,," " """.,.176

4.2 Members ,m 8. C~a.s5'-!Datn Members 8ll!d Nember ~cti'ons <MethGd!!;}, ... " ....... " ... ".119

4·.3 Using JPrivarU! and hbli~ V~!!i~1iJ~ty .Mooes ." •.....•........ "." " n." •••••• n ••••••••••••• "",,.l!:7:9

4,4 'Defgruai~t, 'Vh;;i1bilitv':Dfgile <JIri,v@'b:Jr) ~tti-'fi·'fj'-·ftj'jt~' .. 'fj·-'f-'-·ft~ ftj·-tzt'-,','[i _'-j'-'--'j·"'-,'-t. -ti'f'"'j'-'--,tj', tin fti'fttztt. -t 180

.. l5 Member !Rundboo Ikfinition ., 1\81

4. SW "!fl!!;:idp,Cla;i(jDefi!ll!n'ij:an ]1,61

4.5.2 .outside C~agg DefinItiio:n. Us~ng SCQPe R~lultlo.n, O~li3Jt.i.on (:::) " 1.82

4.·0.3 'Nestin! {brM~ber 'Fun.cttn:ns •. , •.•.• "." .•.•. ,"'." •. , •••.••.• ,."."", •••.•••. " ••.• ",.,., •.•. " .•.• , .• ",.,., •. ,."." .•.• ,.,., •.• ,., .• ".,. ,m!S9'

:4,~.5i4 ,MW:ng en, ,Qutsid,o :ilrunctten I'nliin',e .·r.'~ol·r."'~ ",.~ ~ h,.,r. ...... ;,..~ ~;t;".~+."' ~.r., .. " ~,~",~ u'r.,~ .. ''',.;'''- ~~'''·r. ,r.,u''',''~ _·u',,'r. .. ·.' :~,89

.:t,S De¢letmtign ,qfQbJeet@. ;8J~e, oJt@,Clau 1'· ~ ... ··~t"'·r ... un ... ·et1' .. · ' .. ·rt"'t1'1'~u .. u-t .. _uj- ,· .. u -t] ·'t-,a .. ,., 'R

Copyr1ghled matoria'

4J:!, ~.:!l!~ afTypei C:l:asa , ..•. " .•. ,,, .••..• """ .•••.• " ", w ••• ,," " " ••• " "u • .,.. "u" .., ••• " "." •• " •••• ,," " .. ", .. " .. ]tOO

4.9,].P~ 'byValUIR' ." " "', , "n" .. "' .. """" ,," "." " ,," ."" ".,,"" " ",. ."". " " .Jl:99-

4 •. 9',,2PfIlSs b;yRefemnee ,." "" .. " .. ,., .. ,., •. , ,., .. , , •. ,., .. " , ...•.. , , .," .,., ., .. , " ..• , .• , , rom

4.10 Fl:l!IITIctlORfl Reb:m'IUiJ:tg Object .. n",.,."." •• " .. ", •.• "".", .. "". ".'"'''''''' ,., .. " .. ""'"".,,, '."n' '.' " •• " ••.. ", •..• " .,,, .• ,, 20S

4.11 ,s,mtic' Class Mem,bets , , , n. ", ,", ,",n.!t' "., , !t , "",!t, 'b' ,*".",,,,,,,.,,,,. 211:0

!.ILU.l St,ati,cD.lIrlemoor " , •. , •••• , •• , .• ,., •.•• ,.w , ,., t ••• 2l:O

2U

,~L'JI!i Friend! CIDasse:s , , , , ' •• *, "M' ' •• , ,. • .. H" ' ••............ _.2l6

4..'16 Friends asillirid~ , ...•..•..•.. ,. "'" ., .. " ,,, . .,"'.",,., .. , .. " " •. "" .. , , ,,""" """ •..• " .•. " .. ""'" .•.. " .•• 2i1.'j!'

at:',u,w IQ~$tiQll:$ a:nd .E~r-cM€Ii: ' , , " " .. " " "'" , .. , ..• , .. " .. ",. , •. , ,', ".,,' ", ..• ""., .•... 284

0: Coustructors BRei DeslrnctDEs ,. HU .. ' U • .,. u ·21f1..,....2G6

5.l ·CoJl&'i;rwct,o[' _ .,._ __ , .••••• _u •• _ uw •••• __ mB

5~ i.'m Soocial Cha:t11~ridl,es o~i"!Co~t.nlteOOn +~,~ .. ,~,Ii~,'i,~;t,~·r.+~,Ii+;!· ... ,'i,Ii+'i·Ii+'i·~·r..,.,+.·r.,".,i,Ii. Ii"' .. i+'i'.,r..·.'~.I.liI.,.,ilii,., .. '" 236

.5.1 2; DeclFiymOn and iIlefiTJiiHflD ora Con ntruclm 237

,5,1 <'1 PEimmaOO:rizQJTIJI CMs:I;nl;m.nm ' . .., •. .'. ,T" ,.., *' ." .," • 239

:5.1.5 'Ovet~oad!ed Cow.sf.r;utOOm ,........ • ,., ' 240

:fil.S Copy' IC~l:6r " .,,,,,,,, ,, ..,,,,,' ••. ",.", ,".", " ,. " •. " .• ""''' .. '', ,.".,', " .. " 242

:5.1.'7 D·ynamicIniltializarl!.:illlili. o,fO\bJects, "'" ..•.•.... " " .. " .. ,,"., .. , ....... ,,,.,,,, .. ,, " .•... ,,, ,, •.. ,. "'" ,,. 2414

5,.,1,.8 Ctlns,b:'I1JIcto:rs and, 'Prirr:lithre' TYpes, ....... , .... " .. , ,., .. , " " .. , ••• " .. ,'""" .•... " ,, .... "" .. ".""."." .. "". 245

$.1,9 'Cune:1l:::l'iJDictor m,th De:m,ult ,ArguDleiIlill!, .. "" •••••. " ,., •. , ••••. " , ,," .•• " •. " , 245

52, Doottucillm; 2M}

:5.2.1 Special ~Cb@i:isMC5. (lfDeatmC'OOt3 , .. " , •••.. " ,., .•..•... ", "n." 246

5,2,2 DAIl~arrn;tlion and 1DEl,finitigti ofDru;tnJetrtt . . W

Re~:je.w .U~ and ,~,~S iI'~'.~ r.+~ t.'I,~,.+~.t.+'I,~,t.~,~,t.~.~,.;! r.+~'i,t~·t.+=,i·t. .. '~'U·i't.~,i't.~ i;t;"'t.' .. "i'r.T.~'~~'i·r.+~,U'~·t."'=·i+i·i·r.~·i'U'~'t.'" 2£i5

16~,l :Conce;ptt oflEnh.imta.n.ee .. :B,a.s;e !crnas;s~ :Derj,ve-d ct.ass +~,"+~,t.+~,t.'I,~·.+=,t.+H+~·r.,~~·=+~ t.+~·i+.~,~I~,,·~,~~·r.,IiH,~~,i· .. ~,~, .. ;t;~·r. '2A7, 16 •. 1,1 'N,~ and Obj~."oo Qfl['lhKi;t~I!1I~~ ,,,., .• ,,.,,', ..... ,,, ••... · •• w •• ·, •• ·."'''''''.,,.·, •• , ••................ 257

:fll.:2·0ift'ereet F:orml!il oflnhflrita'll,oo , ........ , no " • , ... .,.,' .... ,' . ,. ,. ,.. • ...., ." ±

, n.:2.S7

16~2. D~! Deriv:oo C~ill " ", , ". " "" ".,."." .. " , ,,"'''' 'n, , . ., .• 259

16,.3 VwibiUty MOOJes, " .. " .,,' ."".,,, .. ".,,, ,, ,, " " _ ,"""c ••• _ .. """ .. _ •• ~c .. ".'n ·.c"'."., , c •.• _._ " c..,_4c.:&l

16\,3.:1 T.ne .PirlviliteVis,~bllity:MOO.e ""' " .. " .. , .. ", .. "".".,,' .. " " .. " .. "'."" " .. ".", ",, .• " .. , ", .. " 260

16Jl.2 The Public 'VlS~bil:ity Mod,!:! .",n"., , •... , " .. " .• ', .• , ,.,"'''"."" ,'' .. , , ,"'" .. ,., .. , ," 261

,6 .. 8.3 Th~ 'Pmt~tetlVi5ibi1ity M.ede ..•.. , , , .. , , " , .. " , .. ,." ..•.... , .,," ".,." .. ", .. , :262

IllS MuU:ilevellnh:eri'~ ..... " ........ " .. "'''''.'m.''.,.'' ........ " ...... "'''',, •. ., •. ,,'''' ...... ,,,,, ... , .... , .... "' ..... ,,,,, ...... ·268

Copyrighlad materia

,6\,6 Multiple ilnhent!l;D!oo, .,.", ....•.. "., .. , ...•.• " " , ..•... , m.' .. ' ',' •. , " ' 'H' ."' •• ,.", ',.,.,." .. ,., •• 271

16~i6,1 Ammgpit~oo '[n Mu]:ti.p!e mnlleritatmoo "", ".",.,., "',., ' , .. " " .• ' .. " , , '., 273,

15.6 2: ¥iliual Ba..q,e C1WliSr .... '\l ........ . • 278 16\,1 'Priv,ately Ded.:v:ed el:ess ,l!Indruces:sibility ofl'rlf,embe'J:S, (.i1l),nl Obj,se'j:.o.; and. wUlirlh~

Derived C1ass(;esl .. " " ."",, " .. " .. " " .. " "" .. " " .. " " " .. "". " " .. " " " " " .. , " " " "." 280

i'M~:Pu.bUca1b ,De:I"l.vOO ,C]nss and AlJcefls,ibility of Members fm.m Ohj'ects, andi,w],thin

DeD¥:edL~} """".', ... '""",,, ... , ..... ",,, """',,,",,",' """"',,,, ",,'''''' "" ",'. """'t."" .",.".,,,,, .. ,,,,,,,,.,,,,,,,,,,,,,,.,, ••• ,,,282,

6,B, l O'iJ~ding ~a.,'.je CruaJssMe~~ ."".".,., "".".",,"., ,,,,,,., ... ,.,,,,, ... '."' .,,, ... ,,,.,,., •• , .. ,,,,,, .'. 285,

16.8.2: Scope Re::iShlltlOl'D:,'1v:l.th, Ovemdden ,MeMbers •. "." .. ".,.,., ..... , .. "., ,,'" .. , .. , .. , .. ",.,286

G,9 Base' e]f!SS m:nitja;!~ll!!tion ~C'Qnst>N,ct1]iI;$, in. Den,¥ed !crna~ .,., ", , .• 'TO """, "'''''" •• , ,,'," 288, 6.:l[OiProtectedl:r Derl,ved C.lass mtd A.a:ess~lbilit1!' ofllember:sfrcom O~$ectaall,d within

'Dmiwd 'C']ass{ll\Sl .. ,.,,,.co, .. ,,, •• ,"',,., ",.,. "", .,., , •• ", ", •.• " ""." •• " •.•• co,,"'. "'.' " •• """"", ''''.'",'.'m"",,'' ,,' ' . ., '.OC,,' ,,,, .,",." .,.,,200,

6.:1 n, Makjng' a ,Priv.aw Jl!I<iember InlIlM:i;~m"te " ".,.' .•. ' .. " " , .•. , , •.. , .• ",,, "" .. ,." " .. ,,292

60112 fII'ybrld In:lierit-ancEl "" ."" .. " ."". "".'" .. n".". "" ..... " •• "". " •• " .. " .. " "" •• ,," ."" "" •• " " .• "",,.,," .,," .""""" .. ,, .,," ."". " •• ".294

6t fI lnvoMiMom nfCol;!i stmct:4!]!llaElP DI@:stD]CI!)OI1'S ., .. 298

6.14 N~"ti~g I!)fGlasseiji '(C4m.ta!~~e:Q;t '0:1:' O.'1mP9!:l~t"\io-:a (jrAm,!I'f:egati~:n) """"""" ,.,.,' ,., "" .. ,3(1'1.

Review ,Qwestiou ,al':Id' ,E~r;ei&f!s •.. " ".,; .. "." " .",.' .• " """;",.' •.. "" . ., ,,. ""''',.,'',, , •.. " •.. " ,300

'1 .Data Pile ,H'aJIl,dlin i' '."'IiIiIU!iiliill!liili;tUjlUln.!Ii.iIi"'iIi ••• i11I~""'iIi<jJ." •• !iillllj;UJn tiI! ....... iIiiIi:Ui ••• ~."'''j •• <i!I,31~71'8

, , ' , "" ",,""'," "",",, ,',"","'," ... 915

1,:2 lJmgSi ofnata.m~ep;TextFii~e and. 8maI'! File n •• ' ; " "" ;316

7.2 liHas,icEHe Opelnti~ns Oil TaXIltnle ,." .. " " ,." ....•... ", 316

7.3 Compen€lnts ofC++w be Hsm. wiililll. F~~I.'lHnHdHng " .•. ' "" , '_,, , 319

7,,$J. Headier Flle;fijtNf:lm .. h;i ~tream, ofil'>fream .• f~tr~'m. cla.'!!se."1 'to." " ~1J!9

,1'.4l 'Opening a Text ~le' in :in. 'I!!IU,t and appbfodes '.', .. ' ... ' ,' .• ""., ," ,'" "",',"" .. ,., "" ,""" .. ,."., ".".' .. ".,. 82Jl'

7.4.1 'O~L) '!'!..1!,(ld~ lU.m ,', .... , ... ,.'., ...•. ", •. "" .... '.' ....... , ... ",',., .•••. , , ... ,. , ••.•.. , ..... , •••.• """' .• ",,, ..... , .... , 322:

7,5, U:sl!:n:g C.~eadmg O~!l:'trt;Q'E!i! £'0'1: Wrin--gg'Text to the File I!IndlR.etlm~,)_"'ru;:t from

tbe JllI'Ue' 3",3

7Jill F~]eIDplJ!tand.Outpult n!3~ng Fw1dammrtl!iJ. D\Iltca'Types 323

7.5,.2 Fi].;:,lnp:iIlit and Outp,ut UsIJ'lg.Abstract Datlil ~s,." .. , •• " .. '''''., ,., "" ' .,'" 3:35

1.,5.,a gp.e!lll). gw ):'iPlI~( ), ptilhle, and. c]O$e{ }I F\mcti(llfls .... ,., .... , ."co." ••••••• , ,"", " ... 326,

7'-1),4 De.b~ct~ng ,~.-of-:5]e (\"Ifi.tb 'O:F Wlth()u't Using ,e:ofi:J. Fu:neti~n)., ,,' ., ,' ', .. , 33.TIl

Omni.!\g a. Btmary Fita Usin:gm, I),.' _d!P1P .Modes , , 332

7' •. 7 The fD]n!u( },,!iea:d( ),' wrlte( l-ernd, 'c~m;e,( ) lt1Unetl!Ons: " ' ", , •.. ;,.'.," "'"." .• '." ,' " •. , •• "" , '" 332

7',,7',,]11 Reading' a:nd.W'n:tc~ng Class Objects, " "" " .. ".'." n " ••• " " " .. " " •• "." ,338

7 .. 7,,2 De:tecUng end.-of!.ifUe (With or 1ilVitho1i.li.t Using '001 ), :Fun.cti:~ll).m " 34-2

7'.8 'PHI! fointen an,dl Their Mil!B~pui~att;(lm, ,,' " ' .• , .. '., ,.,., •. ,.'., ".'." •.• ,.,.' ",. " .. " ",342:

'i,J:!.]!, ]jJ\efa:urnt Actions, '~il(! ReElidin -, or W:ritin -, .'." ' , .• """",",,, .... , ... '""". ,," "",,,"',. "" ,. 342

7.8.:2 i"i1!(!Point!;ll:"S: Mnmipelatilon F~iIilnB {tIlUg( },teijIgO,.1l!eqkg{ Th.

~Ig!] ~,c~~ons,) .. n", .. " ." "."""."""" ",,,,,"',, "".n " •• " ,,,.,,, •• ,,,." ••• " , •••• ''' ",~3

7.8,3 Offset SlpecUi.C8Iti:on .. " .. ""',, " .. """",, n" ." "" .. " " " ""',, •• " "" .. " ." "" .. " 343i

COP"fJlightocl material

1.5.26, 'Dom..ei.n ,N.am,eB .,., .... , .... ,i.,. i,~.,i .. i· ... i:.,~~,i,i .. i,.~·i,~~,i,.,~i.i,..,i,..,i,."'.·i,.i~i,.i,i,.i,li,.i.i,.,..·.,.i •• i,i,.i~i,..,.·i,.iI,i,.~,i,. ... ,i ... i •••• ,~.,i,~,..·i,~~,i ... ,.~-li 1:&1:

15.26 URL (UnifI)TIn Reoollrt'e ~to\!:'') •••••• ' " •••.••.••.. "."" ". " n •• n •• "" •••• n ••••.• ..,.u n '783

m,so Web Servem " u.'" "" .. n , n.' ' j '786,

15.31 'Web HI)$ti:mg , ,., , , , , .. ,." , , , , "., , ,.'786 .

.Re.ttie.w' IQ_stWIr'lS' aM .:Exeremes' , ,,, .. , , .. " ,," ."" ,,"" "" " " "" " .. 789

COP"fJlighted material

UNIT 1

PIROGIRAMIMING IN C,++

Copyriphled materia

Reviieww of C++:

In (ihject..orient.ed.~ng(OOP,p:mnou:nced.~p''!I). data. Bn.ci. theifiS~niS iorp~ ling that data are· eombinedin·to a. oo1f~n:l;ained .. modu1Je known S;B an "'O:bjeet~ wmch can boo used in. otMrp:-ti!lgram8.

Objecl:-oriiented. p~ognmuningt.akes more tineoo~earn iha:n traditiou.ai.J programming beeauee it tnet1n8 tr4liniDg oneoolf to a new way of thinking. Howe'Wiet'tthe beau¢YofOOP is; tbatan object. can be UHd. r,epeatedly in different appUcaUons ood. by di£Fe:ren:t p'rogranmlere,. ,speeding 11p d.evelopm@nt time and. I.Gwering costs .. Object:.orien.ted. progra.m.ntiog basthI'e'& iiimpo._rt:a:!;!!,t ,.,o~c:ept8,Damely encapsulation,. in"u~ritance. andpoiymotphi6ffl..An example of an OOP Iatl;g1lUI!p, .U!: C+ ..... - the pl,UJ9 aigru;: 8tand for "morn:! than C'I ....... which oombmes tbe t·radi .. tlof;la~ C programming la~e withofl.Jeet-orierrled capaibilUy. Wilth C""h :p.~" can write standard code In C wl.t.h.oU.t the obje()t.-o.ri~flited features:j. use(lbj~oriented features:, or do R mixture ofbot'h.

1,~1~ WHAT 18 Cofo.· ,

c++ is an objectoritrnteaprognn:rn:ning mrtRMtfle. It. was, developed. by Bj:8!me S,troust.nJ.p :in 1983 at the .AT &: T BeI!lLabom,tories:,. New Jersey" USA. lIt i8baeically 8. aupm' !Set of O.which provlided low level Fell:tu.reB and. ,sUnu~8. 67,whlchproWi.d!Bdthe elMS eonoept. Tbeversion. I~O ~e available co'lnpU!llCially in t985~ v,er$lon :2 ·in 1;989 and. v,mioJ! ,3iin 1.992.

It baa the following eh.ar:aeterist;i~;

m RedUCtlS comphuity while GOlving probletm! •.

W) COr:reetnesB ,ofre.s.ntEi ill ttl1:SUl'Eld.

um Aft"a.rd8ble in. ~ ofhordw~ and Qther l'e$Ul'COO.

(iu) Easler and ¢he8!pe.t' forin.tegr.alting ,msting H,f:\w8/rE; taciUties andlliibniriea. (tOPO'rtable i~e ... esn beu~. ,on dilrerent t~ ofoom.pu.tenl 'Wli,th l~tUe arno cltange! lin the pl'lO.grams.

C+ .... waG origiinatly named ·{lS 'CwiUl dassel". The pam.eis derived from tbe' .• ncrem.en.t' operawr in C, w·hiC:b. is, ·H:~. So Ci"+m ml. ~meQted. version (i[0.

C++ C8!H. be used fur writing !ptoofi!~htta1 eode liike C but ]!1t8 .rea1&bTengthlie8inth,e wdtiogQf obj,eet;oQriented (llIOgrarruJ.

AnoltJect orien.wdpn>gn:un. is 8 ,eo.lection of discrete objects, which .Bte· self contained eol1.emoii8 ofiboth. d.8i.tt1.~. Eid. functions tha.tinte:r:act.with ,othet objectB.

0++ adds ~.lijbheritaDoet. Jundi<Ln ,o.verludlDI' .ood!. operator ov~rI.oad~ iug. Witbth.e help of these one ,can create abtl,traet daila. typeB, inherltpreperiies from .: mstm.s: data types and. ·l!J;ee· polymQrphiSiDl.Thuawe ~. gy that; C'++is ~.advM!rement of C~ providing u additi(l~ set olobject-Qtie:n,t.ed facilities ..

Copyriphted materia'

: With In/! help, of C+,,", one can dol!t'.twp edi:tor~. Qom:piil1rs" IUJmrn;!J~ic'llhonstVste1l:1$, aata/xJ:ses' iiti!d a.n;y ~r real life, ,appi"trobo,. .syMCln,

1.2. C++ CHARACTER. ·SET

We ha,vetwo c,hHracier :sets, In. C++., The6e aile: u) ,sOUral' charm'ters

.. (U) E'sc.tipe' seque.ncesl.E:re~l.di(jn, cJiai"acie.r:s

(O .... rce 'Characters

The Isour08 tex!t is Icrna,biid wi;t~ the help of source charaetees, FoU(lw.i.ng .are the ,SQ~rte

chl;lracters : -

Al,phabets Digits

S~ial C'-hara.cter.8

A toZ~ silo z,and ~ {under&OO:re) Ot:09

+ ~'!!'1'" ..... % ~ ! &; ;: 0 {} U?'''; ; \ '" b~anlm

(Iii) Escap. Saqu.ncaaJ,&aclidion C:haracter8

These aile interpreteOat eoceeutlon. tittlE!'. Thevalue9 oUhese charaet.er!ilfolN impmemen.~, tation-defined ..

C++ uses :EI()me cll.aracte'rs lSuoh as lin.e f~d~ f(l:rmfeed, t:abete, tmough exeeuti()f:I"cl18r~'

aden i.e., whieh can:Rot- be printed or displayed d!~teCtJy.. -

Each. of these cha:rnctets bas a 'liliuiqtllll1lmp].emefitB,t]nn.aefinedval1J:e whiclca:n be. assigned to a Mogle ChJlil'tu:1.er. E'rJ,ery ~pe 8eiq'u.ence chafflCl~r start" with a backslWlh (\) fOllo.wed by ccnamcUr but bOfh are consJdered as Q, Bil1lJ.lec.haroct~J:.

Tbe mn.owing ta.bJe shows some erth.e eSC8ipe isequence (:'hca.reolters ;

\n \17 \f \t \"1 \b \\ \;8,

.End ofihtr:mg End ofline C~retu:m FGnn treed HQrirwoJtal tab· Vel!1.lieaj,tab Batibpa<;e BRcblash Alert

.NuU

'1\lIk.eath.e ContrrGl wne.xctlirne

, Takes tha C'oDtml to DQUplU'agra:p.b Twe(be COll~l tonex\ logi~page

Taikes the Co.nJwl to next hor.ltDntatta'l!nWrtJCIB P08ltJo.ll Tuesthlt C'OII!trnl w nmvemnl tabula.tion .positi:On

TtIi.'k.,es.. th. e .. C.'&.iIl. ,tnlll ... w. C dl.'e p .. rei vi:I)!l:e·pos~tiQn ~n th." ~nt lin." I ~t.s with. a, baCkiIlsa'h \

Provide8 WlBudible Illeri

AJ&lJlif a character is put after ~cblaeh (\) in QutpUlt statement., we Wiillbave the same cha:raeter appearing in toe output..

Forexa:mple oout <<"\J'; win raBu]t; in display of J.

Here tcb.eidentifier C(lut, (promlunced. aa ·aee.-ou~')m a pred.efined (lbjec~ iisuaed {'Or' ,!J,tandard ~U.'P~lt ,9trea:m (s~en) in 'CT~+. ')lle ope[awl'« caned ;fnsenwll or put to operator i!leI~de ~o:r. :1ns0rt.8) the' contents on. iits right, w the O'bjiecton. ita ~etl.

Copyr"ghtcd rnaleria'

REVIEW OFG+t

1.,3 .• , STRUCTURE: OF A 'C;,++ [PROGRAM

A. C':If+ pIrO!;f"umi is 9. ,ooUecUon of' £lll"lIctio.m. Th!t.pngsID alae conmins the list of ]ib\ruy[IUe(:B~ , included fol:' 8:ihlliillg the conte:nlS 'W' the' prflg.ram. For ,I!'ump]e.

I ff' fit fI ,m.e:l:;:Hl!1J1ii:

lillincllll~'~ioSll.:-ell.C'I.n> 'fhea.der rl~e hl5e. oK £I1:'ll:J:IJ::ac~5!!1cr diroecli:ive;i

~jnclL.lw"'t;g:nio.I1" I!'b,e",de~ Ene

vaia mlllil'lO

c l 't:'!!jiC~ u,~ U £1:l.f.lC:!; !,.t:l ~ ~c e .L,::ar' ~hE; ~r!!.dJ! r.t clJlJt.-:-~'·~eh'l;l:m;: t;~ t-he ~g("ld ci c~~ PC~g;:;'l!Nll.iI"l'9\Il'·;'

P~~l.1

l· ~_~. -~~,

TWle nbov'e e'J!:llmph::: con;t<l!!ins 'Oi,~y ODe li,mctioD, mmm, ( ).

Euery C++p,rog.mm. mu~t ia!le ,11 functiQ~ ma:iill1. ( ) Q,I;i' ~ne p,r-qg:r;g:m '~G'Utiort alw~~~

be~ with main ( )'. :It ~s, iIli .: ~ fu['d ltl!ngua:ge.

II r-e!!lr.H~@nts a comment and. It.ay sta-rtanywh_er-e

II te!plte::!lents a 5i~g],e Un,>!! CQmment ,iHl:d d,6' :not, require IdoaiirJl,g sJWbll'l II R 'comment ~s~predby the ,compiler

TiI.eHhmry filel aostr-eliilD.h> HI, uaed tor ke/polld. cDutandi.<c4moJl> :f:O!l" t1.!mcltion ch'scr O,wnic:n me ex:llm.pJes orWlieofprelP!lil~e-:S'sor. dir.e.ctive$.

The ,(U.nc"iontype. t~ll'BUB ,aibmd'th,IJ.' .kind of~t~'I''''~~, if ~,y i here 'V'o,ld tn,t)~nlSthi!l"t tQerU:(U~!timli. d<les; Dl!)t retlln).!llv'ailI;ue.

The' openifllQ' braoe m m,II/TIts the' begin ~ftbe block 'ilf COde&iDd. 'Clo!,!~ngbt,tliaJ m'Iml!l'ks t~,€!: lend olf th!E!,itllock.

cl:rs~:t If ) ~[!!Ot~O:f! 'c~~ru'S the sc~n. The stllltement

<lout....:::..:::'" Wlewom.e to the: UJ(},.td of C++:progrcamming\n "; dhlp~ay.sthe string m q)l!IQ!;ai ...

UOf! ma:~b on till!! ,ScreSH. :EVlety mmpleb'!' statemen.tin c++ mud hete!l'U!inaledJ 'witch B I>e:m;i .. !Do'lilm. ,(;).

Pr."m~ ~11 C++ art w'l"i~l~~ h~ l(li~. C++ rdMt'i"1l'U~&hesi!Jt~wu~, lower ond' uppe.l" OO:ie leUenr.

1,04., INPUT10iUTIPiU'IW~TH elln AND CODt:

l~ eol-+ g. ~-:troam 1"'I!1e~8 fJJf1,OW' of data fro:rnsource' .t'o tlestina,tion.

'ThfJ: bJ:yword em. (pmnounced j:jaee~ilmll:l)]:S1i pin!dermed I~bject m C~t that cONesponds t;Qthm!sW:Ddnrdw.put sritrnam.F10r ,e;xmnple'.

Ilci€r.loJmT;.ralite5 t;hBl uae cot tin

t ICOIl\!"~'!:¥-lClIFI Cit e:e.l.~i1J~ ternpolI:'dt.llIr5t;g f.a]H:an}l@it.

ificl-l.lll~":'io;st.r:ealii. h> / /I:@t" c-i.rn oillnd 00,11.1'1:

.'.l:m:: 1 ucl:e <coni ID • h'" lIfo!!; cb::u:r- t I

'll'oid JM.in ~ ~

{

G:1 U:G e { J ,;

eeue ... ·.;;:·IEm::ef: f.,1i·e t. mperr':ft;.ure In ee.lS'fus\n". ~Hl>:>c~nt.;'

htlr""1.e'"'c!E!~t+32. (].

C.OIJ t « 0:: 'T \n.eel So i 1l'3 t.eJ1!IperBt. UJ:'fl ..... <";1:6 r.t ~ ; Clil[,lt:«'''\t1E~~lrI!!M~J, 'tr;~:p$.Xi!!I t.J,.1ril!-'to:-:-f<!.h:r;;

Progmm I.J!

The 0UtJPl.llit ofpr,ogtoIn 1.:2 wiiUbe :

Eu'OO([' a..@ oom:pemhttem 'm~sfWl' 17

Cebii:lii!B:t.empe:r:atu.:-re ~.S.7 ,F.·e:~ait mmpet,lii.ttHe ~ 98·.599:998,

TheabQvep:ropa.m haa: » e:d:rl.!l.cotiio:o. Qr' ge~'from ope:m~:r" CCllit lB,the venable in whh:h. we want, to BmirEl' the y,alli.re. The: Ifrxtmdi.oiD. '1llJ)eI'a.tol:' tak:esu..e va]~e from thes:tr1ll1B.mJ object on. :its:le!ft :a:nd plaee~ :it 'm tllevariable @iIl i~, rigiJit, w,hich 'wintle skl:red if'll, the RA1M!, (r,lllldom. Di'cee.~~ memury) •.

F~IWoM~gn~re mg.w!:!, tills .P~\iS :

Fiir.1.1,. ·Standillil.'d, input c.hl

The id~ntim;lll" couf; '(pronounoed '~~.~~ull!';) is ,e, :predle:6Bad. obj:eet in C++ tha,telU'f.\e~ ~fKInd8 'to lie, stimdam, output ~m.

1"heabo,~ progr8!i!n. ':ll:as« ms~TttQ~ (11" put .~ operat:o:r. It ol!lJbuts the conltents oft-he 'v8lri:nible on.~w rigbt htb.e !object. onitB :~eft. Here :it ~dsthe! shring ''''Enter t'e;tempemttllfc.in ce~a,i!l!l"oo OOllt wiiChseg,(b: itw, t&.emomtO'.rand 8~mi1a:rly olliei[' outpUll; Btate:menll;awoik~

F0I!10W'i:ll!g tlPa aihowl!I; ·tihiBprnc8ss;

I~ I

'me rtJ)farlfd ~8e .r)finy;)~t (»)' Qr Q!J:Uli!.!:t (..::q <lFrot'Qf.t in ~ 5ti(jU'mimt ,1£ k'1i<llIWR os cascooillW wIN) ~fflto.rs.

VOU 101-----1 cow: LM- __ -I <:<: 1+-----11 RAM

1....- __ ----'1 L...- __ ----l

COP\l[ ghted materia

7

1 .. 5 .. CREATIN:G SOURCE FILE

'Turbo C~ ptmviides an IDE (IntegratOO.De"ielopment Envirommen.t)rol' developing and. edit. ingapN)~. Or!! the DOSi ~!Jtem ed.lin or any otiberediwr present otaword. procellsot sy.stem. inMn. document mod,l;! .can be 1U(ld. Om UN.IX, y.ou. can1J;se vi OJ' ,eCimn. ,editor £01' creatin,g and edliUingtb.esour:ce code. Tlurte s.hoU~d be 8. proper file name extension flYr a C++ prograrn .. furoo C++ u:se e:Uension . .epp' (c plus pJ;u;s}r(l~ a. C++ prqram .. You mus'. co.Q;Sult 1M o.pe.roting· SJ'liwm. mcmual which you ore hav.ing an yOlll" B;Ystem.

Tihef:oUowi!!!lg ngureshoWli fbe InE ~elll of 1\tr1oo C++. The I!l~ag"wind()w may be opened if req~l;Jited,.

Main Menu

Compile Debug PI'Qj,$ct Optioo!'iWlnrkrw ~p'

==_= =_=_===:::::,,: NONAMEOO;CPP '1~nll

'"

F1 Help Po:! Sa.va Fa Clpen RIt-f{j, Compne IF9 Make F10' Menu StatuslililG

lUlllE Op&~ingScfgen)

'The Fo]~owing steps *ci'I;cl!d be fQllow.ed, F~ cmating' a sa~ file ~

l.Yo~ must MWl ,devel'oped the program (souroe OO€kJ .that.YOU want to 8tO~ ,in the file.

2. A, 8Qitnblep.ie· .oo.me Co.f your- clwice') should ,be selec.ted l'Or atorins the ,progmm.

3. Enter the p~rn.in t~ CQmpuUJ.l' tlnd mrm it ,i", the fik ,YQuha!!.!c "l«~, known M wurce. CfIile file.

Supposew€ se~.ect thelUename nY.cPP foil' s:avin:g'tile source file theo .. afterc:reating it the edito·:r.8Cteen.looksHke, :

'!!Ii FUle !E<ft Searoh Run ,Compile· Debug P.liOject O,ptions,W1ndow Help

- [ .. I ----.- --- _ ... - - - - _n-----'tRY;.f)1?P --- 1::0 -11 ~ .

Il~;r;itlt <iI,rnee.~<lig-e

t ir,':l::la.e <:li.o st ream .. h> llhead,e :r:f.i]lee, (.1i.lse of Pl:e:P[!'}Of:.5!!'()1: dl:r;e,c:t;i ve'~ ti.~clllde"Cotl:HI .. itt> /!tu;:a:d-e,r fU,1:!

vcdd rnal.nO

j

·~lz:.s·c:r; '0; Ilful1'ction to 'C][,ClBlI: ti'l.e, ~Cir\C;Cf!l

'CO!)E <<:"'~~'JLc!Qlr!e t·Q the"lQ·~]:d 'O,t C+ * p!!:·og.t".:urtIl\i.ng:\n.''';

L..:::====== 1 :'1 ••

IF'1 Hoip Fa Saw FaOpen Alt·F9' Com.~kI ,F9 tr.1oka FlO Menu

1 .. 6. COMIPllLllNG AN'D iLINKING,

Itdepe~ upon t'M operating 8LYste·m, being umLIn, Turbo OK 'We com.pHe the ptogramunder theoptioll Camp'ik,. as shown below:

Copyriphted materia'

B

COMPUTER :SCIENOE-Xilll

The fdmow~ng steps are fioHo/W",OO. whne ,oomil!~Hng the seuree ende H~.e :

1 .. TM 'SQurce code ,is' compiled and tlu~ trolfslafed ctJthl (thel romp,fief d(}£.S if) iskMwn, es ,objed c,tHk. ,1« 80me ,errors (Ire #lere.~ deb'ug th:em a:nd .C()~pne again. Anyp.rogrom havffig stVnti;l:.t (gMmmoticai) ·errors ettn n.otbe compiled .Imc~!J!Jfu.lly.

2;, The, Q,bfi:cl,~e is IfnLtl w#h ,o.ther librory ,code wh,Jch are ,~I!ded for exec,r.u'iQn~f t,1:te program., The: tes.uit,b .. , rode .[$ known as ,~c,ut(lbkcode., If' $ome: C'i'TCIr(s) QCCur ,d,llI.r.i'ng l'inking. tkbug,th.em and' compile the program 'Clgtlm.

~,~ae' ~d~ 6e.a;rot1ll IRun Compile Debugl Poo.iacl o,ptiOOsWindbw IHeip

I, ~.~:. ] ========::::;;;;:;::;;;:

I IJF~int ill rr:€ls:m,gic

===1:[ I~: II

]:~fQr,mati.otL .. ,Rerr.Qive rr:es':;!.a,gie

C{)!'!lpjLle Make 1i~1

31l i l.rj a]lI!.

.Al~tiF9 F9'

l:blllc.ll,[!Q;e<::':A;ls,tir:<:l,al1!l. h> ,t'.ine.! ud,e<:cciliil' i.o • h:>

I I

C.IL~SlC[ {}. / JfU.tlc:tl,·Oft to C:b!;hH' it',iI'le' ~;Cli:'EHi!11

Clout «"'i'ic 1,(lIClltCt 0 t: hCW~H~,l,d (llf C +-+pt.O~P" O!m"!'lirHJ\~."".

I'I I

II

.~====~I '~1- -.

F11 IlHeI~p 'II CQm.P]le theme in theediY,Q Edijwimidow

WewiH get a file TRY.OBl aiter !!u~fi1.ll oompHation and. Bftc.rtbe~inmg'TRY.OBJ wiH. be '~m~ed and 0 me I'Uiiimed M TRY.EU will be ,crea.ted.(w~icb ism executalhill.e fIle).

1.:1~, RUNINIIN.G TIlE: PRQ'GRAM

Now se1erlthe R.tm op,t.to:n Cm igettLng theree.l,11t ae ~hQwnbel~w:

!!!Nle' Ed~ Seartil IAun CQmpilie lDet:lug Project. Optio~a: W~ndQw H(lt))

====~==1=n - .11 etd+F1.:J, .1

~t~lt···~·-::ll'

F'4 .

F'1

Fa I

'Go to c'~,r;5or T."aoe i~t:~, Step ov'er

Ei rgum.elil tiS", '" ,.

~'i~~l1..l·di~'; :i .. ()~t, I:'~~m '" h> I, t!'iC 1 u·de",o~:m:Lo. h>

clf8~·.'U i /It:'i.:!not£.o~ to ·cl~.,:t' th~ sr.;::~~QU c.ciU!t<.<:::"'!iie,lco~e tc .. he lI.Iorld, ,of c:++ p:t'm;rcami!'liling\n"';

'-===== 1 : 1=--_ - _".

1F'1 Help I Make aMI ~UU1! thE!CUJfl3l1t program

After executian of the progm.m tile o:utput goes: to the user s:e.~'n. Selec,t <A1t+:F5,. to' see the user' ~nj' wh~c:h displfiys the fo-nowing ri-1J;~~!lage ~

Copyriphted materia'

Welcome to the·woddof C++ programming

Now PnlSS any.k.ey to return to ElWbil:t screen .. For' exit~ng ft·o:m it press. <Al.t+.l(.>. 'Tho fal~(J~g ~ a.refoUowed while eEec:u.ti:ng the executable tile:

L .E,xiec'ute tile ~ /i/ealld 1':eSlJiU ,is 'Obl'lti.Md (if nQ ,ermrn pnuntJI.

2. 1:lu!' p.rogram tsaebugged,n, rose there is SOIM error.

3. In case! af ,errors the campi/alia", step is: repeated 'a;ftlln, and then ;fne ~uUQ\Il, $t~.

MenulBar

Al.t+SpElOOb!1T AI;l4C

AlH.D

Alt+.E

,AU+F

!a.h.+.H

AU""O

Ait+P

Alt+R

}·.It+S

Mt+W

Mt+X

"~. you. totbc ill (Srlltem)' :m.en.U Taliles: YOlO to ~be Compne menlll. Tab!!!, yw. to~he 'D~bug :w,eg:1.lj Takes; you. to th~ .Ed!~t; menu. Ta'kes you. 'lQ, tile F:i~emenUi

T::okes ~ .: t.Q ·Uw .H~JpmeQ~ T;a:ites )!OU to the OptionB menu T,akg you to thePnj:ed menu. Takes yQll to tfte .Run meJliu Take!! ytlu.t;Q the Search. :mell,u T~CI!i)!l9Ilto tlbeWind~menu Ents Trnrbo C++

General II DE (Integrated Devaloprnent Envlronmtm,t:)

Fl F2 F'd F4i F-5,

F6 F1 FH F9 FlO

Displays a. belp seJleen.

Sav·es th.e file th8lti'1! in. the acti.v'e edit wi:ndow. .Brbtp up' a .dia]og hmI .. 8<0 1'ou ,can .open. a file.

Runs ycIilirptogtam. bti the Hn.e whem'l' the tt:I.filOr fa .pDfiritiMed. Zooms t.hea«i ... ~ w:indDw.

Cydeli tkrDugb an o:pen.wiodow.

Runs your ~mStmn~n debug mode, tradngint:O functiQrlIi.

Runs your ~ifOgmm ~n th!:biiigmode,. s'ieppifl:g,gvei:' fimCitlon calls. Invo:lrosthe, .Proj~t. Ma:IlIa.ge:J" to make an.EXE,fi],f,l,

'1'~ yw.to themmu ba.t ..

.Hot.Key

Cwl+D~ C'kl.+lns Sihift+Dol ShH\,+I~ :A]iHlJbpc

Rern~ !!I!l~tex.t from. WimiiOw ;. dOf!!!n,'t. put, it in Clipblilard. Copil!3. !eJccted text to mipboard

Place8!setoot8d .~ In CL~pbo9:ri. deletet Mlecction" pgte8 ilint from Clipboardinoo tihe 8Ctive 'wi~dow ~m~ Wxit; in w;t1ve,wjndioww !previous state

Copyriphted materia'

Window Managllm,.n,t

llol~

Alt+O Clr.l+F4 Shift+F5 Al.u.F,5 Shrift.+F5

llispl,aya til window, when! '" is t.ho(!IJJI'(l.ber' Qr~ windl}wYQu wa'nt to\liew

m....,;I .. .-- Q, U!I!t; of'i"ri'IoPil, wim!:OWli

~,r,"~1I! -,I:"''I.r.-

moses me 8divewinoow

Til.eaiaJ1 open. window!:!,

Opens ,Bfi lfi8ped.m:' window :ffi8pla,y.!.t User Sere.en Zoom!lilUQ~~m!l the, active window S'l1ritc~, the amvnwinduw

C~IU .!li:R Oor' ];108iti(lJ!,.of arti.V(! ·~w

F1 FIFll

Shift+F'l Alitc+Fl

Ctrlij,i''l .~

0peW! a C(I!::l!te~~tiv,ei-l]elp .sc;re,en.

Id. Upl Help, on Hetp •. (J'wd, ~sF£ wb.w:!. you.'trc! '!lh~,eadJ in the ~,el;p system,,)

,Bring;!! up' Heip index ,Dilpiayap.revi.tfltl He:lp ~n

GH:ll:!!, UplllllSUifllp.speti6c help, in. the acti'l!'eed.iil;; ~

DebugllnglRunnlnl1 ,1lDl.Xq

Al,tTF16 Al,l+F7 k1HFS Alt-t-F9 CiUhF2 Cti~+F5 F5 Ct.rl+F9' F'l

F8

F9

Opens, 8 IW!iPed"m' wm{fuw Tates ym mpr:ev'iiouonor ~~g,.t9~~ rCo~piil~ to .0.00'

,Reoot5 numilDi ptlogmm Adds a wateh. ez:prealon

,Sets or cte8it'B ~O<fifi!i:t.:ionaJ ibreakpoint RUM ptQJl!lt!1

EI:e(uu:t.8t.racingtl.l,t;o £unctiool ,Ex.~tes "ipp~ng function CII]]!! Mm", ((lO~'1M)~tl':sIlinb) P1'(l,gra.m

Copyriphted materia'

11

IBlock Commands

~------------------~------~--------~---------------,

I

~

Ctrh'Q:B, Ctrl+QK Gtrl"'!r;,{B, Clrl+.i::'K Ctrl+KD Ctrlii;.KH Ctrl",!l\,:L Ctrl+",K, ,p Ctrl+.K'F' Ctrl+KY Ctr14KO Ctd+XV Ctrl+ID~' ShHt,,,,De:~ Ctrl+oet Ctrl+KI Shift+lns Otrl"K.:R Otrl+K.'(J

Ctd+KW

M,oveto begim::dng (If b]oo!!;, Muve m eod ofModk

Set ~M~ng ofb]oo'k;

Set end. (lFbloc'k

Entto, U:U~fi,U ba:r Hidl!iSbow bloek. MukliM

:Prlnt :~I.eded,l)l«k Ma:rkword

:f)e~ete 111m C'opybloc'k,

Move block

Copy to cmpboo:rd Cut, to (~lipbooar:il! Dc~et6btook.

hUJftDlt b~ook P~·fuM C~~pboard ftie;ltd b~oclc: from diek UnlndeOlt, bl(ICk. Write blook W disk

'1 .. ,9 .. TYPES OF ERRORS AND DEBIJGGIING

Errommay he m.:ade d,W"'ing pmgr-am:. cr~,o;ffi.(}D even by ezperienced. PMgra:mm.I!l~ :aho. Sucb type of' er.mrs a:Mdetected by 't.'h.e Compiler.1kb~ing' meo1Mf'tlOOUing the e,l1'On.

The Cl"f(Jte:are ct'I!,t.egorized in :Loot t~8 ~ (i) ,S,y,ntm:: erroNJ

[if) ,LinAi7lg e~7"S

(iii) ~uti(l~·timeerror$ l(Rulll·time err()rsj (iv) LogimJ (!rrorB.

(liJI Syntax Errors,

These ,an!' fk~eted ba the c,ompiler., IIf imy gramilma:t,ical el'1'Ot .ismade' int:.hepit'Ogram~

then daring 'cQmpiWa:~on. swpt'be CQ.mpile-f wm di8pla,y 00 e'~J'm_eeg,~. FW'E!mm"l~'1 II r r~i" rot a r.t~ 5,~",g;e

U~lclr~de(ios·tre .. m. h> llbeOlJd:erfil'e (Y!<se glf" p:t:~p!::Q(;~'S;~ltK;; diit"ective'} h !;l1(:'1 jjld.'e'<co:n 10 .. h> I ;:beader:f.H.e

'>I'g idma1.ii:'i! ( ~I

c,]!" ~~C:f.'" () II ,f~t1ct :1to:n to ~l,ea ~ t iIf;,e ;5 c,reen: cou~<:<"Wel~jcilin.€: to t::.h'~ !ijlo.::llid. o,r Ctd' p.[CcgJ:11'Im:Ji!'I!i.<lg\r!"~

Copyriphted materia'

12

It, will not.compUe 6u.cce$:CUUy 8$ ; is missingp,ft;er the fl:!f~ttil).n clrsm). Socbanre it to

cl:r8cfO;and tnen. oompUe mild.nm it again fvr getting Ute Gut;P~lt on.tiheSC'1'een. .

11.10 Llnkln,91 IE~rors

LTbeee~y occur d.uri_1'I!gthelinking ].'It~a •. For example, if)'lou: dontlt. i.ILC:~I!!I.de the he9id!er fl~e i~~b. Of any ot'be:r 8u£&.fietbepoogram lcompiles. aUOOElaMul1y hult fWJs during .U:nk .. mg .. An. errol' mellSBgewiiUbe diiqtl~yedinthe li!n1king ·window. Co~t~t and then. go oo.ead.

(Iii' Executlon4ime ErroR

Sua:ellfilltUy mmpilingand~inlBIQgap'l'og:mm. dQnot moon that you wiu get. the d~ t'esult. The: ~ts may be wrong d~tl to ~r{s) in. logic ,O,r' d!lvlsl.on b:J zero, :sta:c~. overll.orw" In finding the .sqwu-e root ·ora negative!dwoJbel' etc.

(Iv) L..oglcal Errors

Logical eml'8 .aFe'IDQlilt dilfficu1tf.:o debug~ wbleh give wm~gre8ult (I.D. 3CC01lID.t of mistabs: made b:ytbe progrsimmer m logic.

You .. should cneclK lout for ati.dt errors fr.o:m ytiUf teaCiher or oofiSiUltth.e,use:r ma.nUid fer debugging ..

Th~ ,!!Malle!!,t mdii.ridualuni,b in. a .PN~ are @Jloo,wktIDs.C++ bas the f6lJowiiDgtoke!l'ls; L~rd.9·

2.1CJ.entifiers

3. C'onria:nts 1(i~t:emI8}

4. PUncl.!:4lto!'" (:f!kJ1t1.1YJ'()~_)

5. Ope!'Oto~.

AU the c++ programs are writte-n USIDg these· to~e:DS, whlteapaces and. the ;ayutax. (g:ra:mm.er)oCthe 1ao81'lflge ... Most of the CHwkem; are simUarto,tho.se afC tokrens enept.ftl,w additiioQ' and. mInm- changa ..

CH .~~ .W!'I'M ,puiGlt.,UM. ,ucn (II ~mMHI witku:t u.siIW wldfl.!·r~+ F(l'l'~' pk~ .rdum;fO') j: .htre tu .white .space' mr:iiY be· wed 0." omitted ,~ the' ope~·panll':ltheau.

1 .. KeywortIaJ ..... rvad Words·

These words m:e·resened to do s.peetfi'iilmsiks 8J1dmustQot be used ,as .normal identifier namu.

". I ~ ~ I

pri.V8te

U'

:h:d'i.ne

tor

3utob:toaklUl:ge ,wed chill"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

oon!lt 'OOD!Q:UI)I..:1l:8! defAult delete

.int

.new

Copyriphled rnmeria'

REVIEW OF Gt+

13

Some impleme-l'lita:tJ_.I')nJSand. s,tanda:ni Hbr,me8 ¢(')ntai~8i dOl:!!ble ~de::~co,reL.J ill!:!l: Q l&served. wom.md so it shou.!ld. 00 8.void.od ..

,z~ ilden,tlflen,

'The~e 8.!re' the fundamenm!l. buiding bl.oob of a pro,gn:m ood. M'e.llSt':!d. to give~amel!to

variable:s~ flil,ncti(iAs.,,~ys, objects, classes, 43iOO~

An, i.dent~fiertn C~~must fO:low therulea given below : 1m It is a ,ooileelion af U!.ttern. digits cma undei"8C'OreS.

(t~)'I'lre .first chaf'Mter I'nustbe a letter (und'ietsconu:ouMB ·as· 0, ie,iUr) (iii~ UpFnm~ ·On4 Lo.wlr.I'CQ:s:ef.eUe:I'"I .Q~' di/leN!.nt

Uv) All.the ,chg~1'$ ~ 'Significant

(u) Re$~~d ~ WllJlOt be uBed a'S nameJ ()f.tde1ltJfiers/~bles.,

Variables:

These ~e flDld8lDl!l:AtaIi, 'to MY ~m~. ,All C++vmiablea lr.l,wrt be dedar,edbof'Qte their~, Va]!.~~ can ooassigDro to variab!es"w,hich can 00 ,cJh.anged, d:urin:g prognun. execu. .. tiOfi. Vanabkoome-8 sho:.uldbe user friendly. For exam,pl~,.ifth:e sum o:ftwon,umool'8ia to be calculated, •. the v.arl.ab:te can be nwned M',&u:m' rnMleI' than 'ZMgum~or some otnercryptic:

(difBeultto undemtandl name. . -

Thev;liilue 8:8s:igned Oil' inf1!lUed to a. variable is swred. in.thememol'Y allocated. toil We cam cn:!8ite vBrililitea using the k:~ywonl cbar~ .ia:t andDoat et.c~

Concept cd 1D81a 1YPn;

The· kind of ootod variable ma, hold m ,0 p.rogrom,ming' .lanpage .:1 oo:lWl it's data

type. Th.~ two basic re8SiQn8 fllr' difenm&i!!!ting between da!.ta~ypea, are :

'(0:) U8~' Qfprop~:" 'n~rnal re:pt:euntafi;on by tie oomPllet~

'(b) Use. ofp·roper operators for each ;t,P£' by ihe Pl'OSmmmer,.

The fol!ilorwingfigm-e ahOW8 thavnriolili ,categories, of tho data types :.

UMr-defined TVPe St~ure Urilon

CleM

DOOvedT)'P8 _ Any' IF~'

II PQfnlef;

~_..;..IFI....;€I.;.;·~;.;..'~';';-';';IOO;..;.:_' _.

BUIlt-In liYPe (fUnd8~)

FiI·l.3

Copyr·ghted rneleria'

.l.hQV(lid. dlll,ta t,ype has tWQ iMpQ:r~a:tl!t,pu_rtoses ~ w) Indicate!!! t.\a:ta ,":!iu':!,dion, tioos notr-e:t:ur'n 'Iltralue. ell) ,lkslates ,agenettC' polnte't.

No 0bject; ,of tn>e 'void, nUllY be dec~:Bir,e;d,.

ll),atab'p.e, repf1ese~~tion i5l~Mhine dependent, :r~ Ott, TbeftlUQwin;gmble sh~ws, tbe diamtypce Md,their~~rlC!j' a 16-bi~ 'W'Q1'(i II)(l:mpu:~r.

J!:27

1

127

:1

'liIJl5i!,ped ,cbur

:1

'IlT:lsigned Int

'unsigned. lOllI!:' LO,t

~ ~,;294.96'll,;2.'9~

II

1.7 e-308

16

F,'o:r ~.p'~e~

cl,u,!;;r dh. II r:;/iJ :i~ .~ chO!r~c:t e ~va:d."bl,e

iHt ~1[lm; IlsfJWJ, iSI Ol!:" .!iT!t:~ge.r 'VoIilt'i.able

U'Oolllt salary :; lI:sa,lary Ls a. flofl,t,ing po1.f1t variabl~

The d!~b ~ dl!JubJe :isalSQ used frOl'hanrlllin,s: :n.oa.tirIDgprn.l'Jiil'.r.!umhe:rs~ 'wl.th. mur:h lBl'gerran,ge amid pRdsiion (sigWficaDltdigits after decima~ point~. [t~s used when. the number 19 too ,8IJ:I.:Iill or ~llW!fficl:eGltly precise.

A :lI,lJria:We'ooI1Mirul ugMiMltJe oolue if n(l' ttOZue j8.QiS8'igned Or' i~Pldt'ed t~, jt. Integer VI~lables ,

There Gn' u~ed ,to 8lt~re ;whok'nl1mbe'1"8. ~efonowing pNgr.am iUUBtr.H!ltes tJJe' use ,of inept" 'VBriQbl~.s;

'':~eI'JOflst:!7ati::$ the '!J:l@ of ii'llt-=<;ler vari,a!:lLt!::l

'lrlt:::lud~";:1o:::!l;:ri!!<'lm.h> ! I fti)~ COtll~

.1 includ'e-::l:!IlI1.io. h> ""f;!.i.d m.a il'! U

int a, b, ~; cl!J:scr(t •

a .. 10;

/ 'd~Urn;!s: ·t.hNa iflt.eqer vOIl:'iabl@'lS ! 'cle di:"", t n e !l cree.n

11.,),s.siClf~o ttL!!! \/.:ilUflJ 1m to n

Copyrightocl materia

A~VIIE:W OF Ot+

15

b - ~D. "" - 3.0,.

catltS'''''''<II "'«d«!ilntllj "~nt:il mgVe5 '~'Ii:niC1i' t:e nlllKt" line CgUt:. ,,( "ib="':<b'<~e;.otjl;

OQut«Rcco"""c..s<:'i!!'Idli

The outpuct o:f pnl,Sirant ].a, wU1. 'be ;, a=ll0

b':;;;.20

c~30

! .Far dJ8.p'i(J1ltl,' fh'e ~uc ,(;Jf,fj ;uariable ,U ~hll.uJd lWit 1m l!llcl~~~ith:!./!!i ,~llI,blili qtl~~, FlaatVia~iables

These an used to' store flootin{J' poinJ n;um,be'rs. For uampl~

l .. fdl!!rlIOflIlE!t'aCE!I!:I 't:J~.!l< tli!H!! of floca't v;;!l!':'i;;'lJbl~$ If i.l'l e hl(t <: i-os t" rl?31!'! • h~

"'i nc..1 !Jd~cofl1.p. h:>

\10'1 d If!1SJ in 0

j

tloat :Ii. y. H llciefine!f th1'ep.: float Viltliilt11Q3 .:ll:"scr I i ~

!t." 100.5.

'i 20£1.0.

~ .. l.GD .0.

C!OIJ!'t<:<}'~""<~«'" '1_i"<~y<~(O ~"""<''':;'$!'''<err61,r

Th,e '1)iI1tp~.t of pm,gra,m 1.4 wm be :

X= 1J))01.5,Y = 200 z = SOO ICharacielr'VailrllalJles

T~e:segR u:!Iied ~D starn 1~It(l:r,KterCOI'!t8tant\!l ,like'S-. ,jat, , ~

'The l~st o~lom:p:II'~8eGits a Mamk character., T.1bese :IlU,~ lei'!:c:~oeed.wi~hi~.et~g],e qu.ck~. Fbr exllim.pl~·1

I tdEllllc:nst.rrates the· USIa: or l!rl1af'>1IC:l:aC!:' 'll'arlabr~D 'in~lud~~lOBtrcd~,h~

Unrclllcl: <l:Ci'onii1.Ja~

\loid ~;;'f1 j I

f

I!:!ha~ clllf c)!2; I )"c:ho!l:~,!;ete~ "I"<i Iril!!lb l.e'!: tll?c'li3J~E'd cl:r!'lr;:-z;-O;

ehl ch2

's' ;

ceuu <<; ,. eh l""'O:;<ch 1 «eJidl ,. co~t«"ch2!"""..;:ch2«en.d1.

The ~~p~t of prog1"a!m 1,5, wiillb!l1l: ,ch,l~ S

ch2 = 81,

Strings

Fw' .8t>!)n1~ mQ~' t'h(111(J chGmC'ter~, a /Jlrin, i8 ~d. ,For e!X:lmI,pJe\l ,char Dame [Ill ; will aJi1oute, 11, byoosw,bold B :stnng" lm cas@ ,o:fa!ltri~g the last byt.e is always used W S'OONI the s:tring te:~in:8!~i!.' (''\0'), known as. NULL character.

3 .. , Gonstant. (1IIit.nll.,

'TheR areth(t dam item.s l'hat~ver c'haneetMiI' ~a:l,~e duril1g ,the p'N)gram ~cutiQn..

In,C ++there m,e 8!!1v;eral types o.f lilw:tak:

m i~teger consJan:t

(iO floatingeom;wnl

(iii) cMrocl'e'I!' ,oo~tllnt (s,tTing CO'W]rant) (I~ lint •• CtI:n&ta!n1

'T'L., ~ -. ~ ,c .C'.~ :--~ c J'-, -,-",.-~-J:.~n:---!'h(jC't ,lI:~-' ,Ii;."" .. .ti - ~-, c,r --£1"" ~'!" rf'UOw~"'~, .... I .. s ....... fo~~ow~d1

.& nEl'.6e are WrlO~:.e q .. m~. _ Wn .. __ ~_ __ ny I~ ~ __ Qr:Jial'p,j~, '" _""' .. :t..I!!, • ""''''_ ..,. "'_ _ ___. __

f-o-J:' ,cona;t;n!IIeti:nginte~r oonst8ill,b

(ill) IlmuB! ,nave ,at least one dW~~ (ob)lt'Dlu:s!'I.nat cantail!: 'II &c\i,mQJpoin~' (c) It met' either halve .., (IT' ,_ $.~g.ll

Cd) Whern 1W tlign is pusent ;it is ,ossmnm .to be positive (e) C'om~ (lind blow Or:(!: RO~ per,mitred in ,it

[~ 'C++thom, can be,thN(ltypes.(lfin:~,g~r ~o:neta!fIta· : (Al' DecDPal (l~!lise m:r-am 10)

(8), Octal O'I.'il_Or radiix 8)

(e), .He~:adooiimaJ. (ba.se or radix 16)

De<linu!il iuklel' (lO,IllStaD •• 'These oonsist, (Ira :s~quenoe of d~g:i:tlllln~eB:8 ilt begiins: with (!I (digit:lier'O) .. F'or .e.g. ; 1024,. a3 UI~ +2,76~ "". Jl:2 ,ete ..

Ibu:tPi of mMg8l' COD8,tanh. "Tho' ~pu;term.e~ory ~]st9, of .iiI. l'!i!J!~tll'~'~' 'o[ ~ni!l ca~ld W()fld!!i. Thenil!mlfbe:r ·ofbi:t1!J :in. a word is MIDled .• ord.le:n,gt'h. {leneral!!ly the PC'ii' ha.ve word. lengtJh of 16Mts (oom.emayhave 3,2 otmor,e),. Le&U8 take :D. utb.eWG\td. length .. The in·tegerra:n:ge if! ,gi,y,en by ~ 21l!-~oo :2",-1 - 1.

[f n = Uii,thel~n1leger range is .... 2:~E-:~m! 216-1 .~ l! i.e.~ - 82,'768 t~ 12,"1,6'7

lfyolll will! giiv-e ~. mk~:r ~v,pJu:e f¢!!I~tJlum - 32:76:8· an underflow e~rr wmo~c~ Wld if IilioVMueof M'J.~nt:ege.r ill!llllore ~an 327678111. Me:rtI.0W e:l:'1'01" wm occur ..

CopyJlighled materia

REVIEW OF c++

O<!taI m_~r QODllbmtLThese are preceded, by 0 (digit, zero) ..

F'OI' e;mmp~e. doclmBl,in~ 14 wiiUbe writteu as, 010 as octal mkger. (u L4u:I '= lL6,e)., Heu.diecbna1in~iI!' eonatant.. Tb.ese are preceded by Ox. or 'Ox. FlI!l'example, iJoo~~

mal integer 14 will be written as IQ}m u hexadecima1! iin,teg~l'(as,14:UI ~ .BIII).

Tbe suftx,m or 4 '~. or Ufo:tees any 'OOIl!8tant, 'to be .fep:re!1llmted :M loog .. d URlligned! respectively.·

(n) :FfoaUng Constant ~Real Constant)

These ,hove {ract.ionalparts., T'besemay be wri.tmn. in. eitherftacth.maItbnn ,Or:sxpoD.ent :foil'm, 'Tbe following rules are {oUow,ed for constructing re.al eonBmn~ in, fnu;:tionalfol'1!l ;

(0) A floo,U!lIJC'OIl$tant in, ,fractional form, mUM l«ure ,a' least on.e digit befon ,0;00 a/l!er

the decimalpobu.

(b) It may either Mite +or - 'iBn,

fe) When J'U) sign isp.A!.tnt U is ,assumW to be pcnitive. Cd.) OQmma:.~' and' blanh ,arre' ntJI,pemtitted; in, it.

FOIl' example, 12.5~ ·"",·18.3.""",,, 0.0026

Thoe.q!one.tlJ!tfOrm consists of two pa:rt.s .~ man:tiMa :aDd! ~eld. The~l1re ~S',m::!:lly u~d w.hen tie constant ~, ,eithiJ.r too8JfUllII or ,'00 b.fg~ B,ut there is, no NSmctiOJ! (oriu.!l ·to use· e;"lilpo~en.tial foro} (If repre!!eJ'!;~,tiq.n for Qthe:r flQa.t~ng (lQ~tan'~"

In. exponent (ann. thep9rtbefQl'e';e~]8 called, manti8i.Saa.nd theparl ,B'tfte:r ~e' is, call.edi.

@XPO[l:@Dt,. Tlte r~UQwingmlH an CoHo,wed fgr WM!tructtng real,oorul,tant8,ifi Bl:pQlle:nt wrm : (4) The numt~8a ~nde!t'pa.fl!l!!nt are se~mted.b)t ":e'.

(b} The mantissa must ~, erther an. .mteger or aprnpu real oonswnt'. ~(:) The ltJ,(Ult(q8Q J'JUU" ~ f!JI'MI"i"01' ~ 8icn.

(d)'W:lJen, .i\!iQ8'ign,~'$. p!'l!'sen.t U .ts a:.sstu:rJ.ed ,taOO:pc:ulifiVt!.

(e) The e,t;'p(u:umt m~t be'a:tl~ on!;? a:igU il1:~gl' (either po.ritiive ()1' n.egati've)'. l'Ie.fOult sign is +.

For egmp'le~ 2,4 H>- 3,5 Hi - 0,6 e + 5

Rana:e ofPloatin:c Con:atanUl'.'[he range of re8t.ICons.bm.b!· in exponent£tlrm. Q~B 16 bit PC :ie: 3.4 e-38 to S..4,e38. It ~pi!es 4 b.fb;le tQfmem.CI'fY.

(ill) Character Canstan1 (string Consianl)

TJu~l! r:eprresema &eq.~. of zema." more. cha.mclers.. If ,en,e}osed by ,slnglequotee~ tl1ese ore called C'hB~ oorurumt8 and if elu:ilosed. by double quotes. these lite· CPiUed string:. lite.rats.

Fbr CDlw.plc,

~,a'i8 a valid. c&arac:Let 'OClHStalfit Md. l)(;Cll.piee 1 byk ofmemo:t:y,. 'WbefeBlS" IiJBD" will oocu,py4lb'-ytes becauseit,wiU be addoo with a special cbaxactet,j,\Ijl! .• which. :n:I.8iksthe end of ,B. string. Note tlJiat, "\0Ii8, a :8~ngle chamcter .. 1"hua:"JB~ wiin ,8ctOOUy ber~Te8elited WI, "':JB.I)\O'" iumemory i.e .• the tenni:natof' charadeI:' i!l !I,mTed. inthel ,ead.

T:'h.e lI'n.dtidu:u.~ctc'1' chl"'QCkr ool1:l!ikll:ll~ a:~ trmkd ~jllk:B~r6 (lype ,J,,") ,a~ the ~!'lIl: IW'tiwst, cOMtanu ckperuh upon tlre im:plen:u:ntaiio".,(t\ie,tI.Jon).W:~~ ai1 'il:7'l'QY o(c.IIVT il UHdrClIWn

!ltn~ JitfJmh.An ~y :i8 ,eI coIlectWlJ ,of ~/liI!'QW eunu!!mJi whlch i.8:11tatwj• th~ win be .mare ,clear in 1M topw ~8.

Copyriphted materia'

4. Punctuators (liapara,tor,s,

The foUowi1lg chMacte~ are~oo M ~p~at.'0:r,~ in .01"+ ; o (} [] . : i '* .". = ,

Pareltrtheses ( ) These are used for functi.on ca11:s and. function pla[,am.e.i:ers .. These are P11.PI eqme,aSiOfiS Jl1lid ,separ8,te, cQnltitional statements:,

Bl\"aCeB ~. I The~ are usOO, for bl,eeking of cod\(!; having :ElUn.p~9 'Or ccwpolmd {morell-han one} ,ex.ecubilile statemeot(s).

,Brackets ( ] These om 'used, foJ' enclosing wmcri.p.ts in, ,case of single an.dmul~idim.e'n. sional amya.,

Comma ~ It is,ue;ed, fot ;!lepa.t'aiting ,a1:'~ment (parameter) list in, a £ull«ion" :SemJc!O.lon ; It isusOO aa a stat:eme:nt terminal:o:r in the ease olao ex,OCUitable staJte..

:me:n,t,

Colon; It ill 'Used inilbe t':ruJe of H, :~nbened ,etoJ~ment"

Aaterillk. :.' It is used for a pointer declm1ltioncrr~ 88 amu1,tipHcationoperato'r,. ,ElDpsllli ... These are 'used iinthe formalplU'ameter lJiste. offu:nction deela:r:ation (pNf.t)-

~yp.e) wba:v,e a, wnable number ,ofparameiOOts (.ugumen.t:s}"

:Eqm 10 ... = It is used [or aasipment and initialization o:£variahles.. POUlIdllllp t It is uaed. fQ:rp:repmce~ directi.ves.

5. Ope,rators

These' dr:e toiel1S that do 80me oon1puJalio,nwnen. appJied tOll'ariables a,nd o"her objec.ls 1ft an e;tpRB411'JJl..

~~~~~~~~~ PaEcW:E.NC1!! 0,11' OP.B8AroD 1Ni 'Tumlo 'C++, ....... ~~~~~~~~

The ~ C++ opemtorsare:d!iridedinto tha follo,wing 16 categoriea:: 'l"beso a:-re (lrdaM from the,~st preeedeJl!ceto tb~ll(l\V"t ~denc:e' .. 1'b,e, (lpel',(IJ,wrG within each. ca~~y &av,e equal p!recedetl)Jce ..

Th~ 'UnM'Y, OO'ndi,tionallUld. ~3.ignment O,peraool16 OO6(lc:ialte'ri.igh~t;o..left a:n:dJaU ~t.ber opeta:ku's aa:aociate lei'lH.rri;ght,

,II

2. Unary

() [1

'" size, of

new de~ete

.Functlon. caU .Array :sQbIKript

Ow Ind~rect, .romponent ,elector ew ;l!cope Mce88I';re:!o~l:lt]nn en direct: CQ:Rl])Qntnt 5e~cclm'

Logical.!:leptiQA fNOT) Bioose (lj.Bi) oomplement UnaryAJU!

Umrrym~n.us

Preincrement crr poatil1CflCH:Um!t Predec:rement OJ" pos.tdecremell,t Addre!1.6

In~tio!1

tretums eim of opernndi •. ~D. byws} ,(dyn:!lmh;.a!lly :allocates 0++ :smrage) '(dynam~t.8!Uy dea:llocatell Cu :8to:r~)

Copyr'ghted materia'

REVIEW OF CH

19

M.w.Uply

l1~viCte ~noo!"(m!ldu1li!B;)

4. M~mber access

,.*

.",;>,.

'Cf;¥ dereference D++ dereference

+

Bimtl')' pl\!!!!! Binacry minu'i

<;< »

ShUl left Sb:~n righit

<

ussitJrum

LHs ~mm or e<i.lI:al tlo Gtrtlatm'thlUi Gl'eatert:h81'1, <If ,equm to

8, .Equality

EqUlilto Not: equal to

9.

&

10.

11.

I •

Ii I

It", ,I\..~

Ii ..

<<;~ >~

S~mple Uf!~gmnt!nt Auign product Assign, quotient,

Assign remainder '(mod:ulu8) Assign .IJW!:'!

ABB:ign d~ffe::ren,ee

Asaign: biLwi:!!1eAND

Assign bItwise :XOR

As~ign bitwli~ OR ASs:ign[en a:hift

Assignl ng\b~ .sh~n,

Wehailii'El not used, ,some onhc operatoJl.! tmownabov'e 6(1, far. These opemto'rn Me dis"' CiU8fied w.ilth auitable emmples in. the du~eQlIiree.

'I'1u!. order ofp'1't!Celkncl1. CGU1 be ar.re1'1'itJ.thn: ,ttY. ,uging palle:nlmUlf!$"

1. Tile e.:tpDMnltatic:U'z ope1'tJJM ,is: mimng in C++ lang~"

2', The eqtteilm (~ ~} o})'emtm'" i'l' noI' .fl'(;IJ\nI! as t1l.e (1I1rfB'ltmet.!t qperoJfJ'l'" r",,).

Copyriphted materia'

20

N'ow l:eit .~ cx;pili'litli ,some rof the (IIp.0mtoJ'sin di81ti\lU ~ Modulu Operaklr' (%)

The ,moo'ulo G,Pllrnt'ar' WCli'"kB amy an integeI'8"

It Iiet'tlln1!,S tbe re1l18rinierwhen.@ne mtegeris di.viid~dby~th.e~ .. Uthe raivii~,l' i:s ~arer tli:rum tb~ dividend,t~e I'\ll!',.lt, is 'lII~dfvl,dei!1dl i~eU. Fi()T e\\::QMp(c.

I !demtnls;trat.ee ,",he UlI'P. of modulus ()perl!l.~cn; inc1.u:ll!:!<io£lr.rerutL.h'li

'include~conio.b~

"'Q.t.d !r1ll1in U

int n~ml, nuro~. n~~. cl F,si:."t' ~ J ;

fluml = 2'~: I r<lss:l'!Jint.lf111t of 'l."alue ill to nUlI'll nlll~2 = .Eo

nUIIII] = J'!I ul'Ill " rnun2 ~

CQI,&!:"; <: ~1" he t'elT'~J. n.j;!f:.' i:5'''' O(.;:n u.rru3-,

r'Fhe out"ut m:pr~ 1.6, wru, be, : 'Them_am.dle:r is 18;

The Un'l., OperaJlDI'S

TM~ OPll!ffl~!! M.ly ,Ol!: ,Of.l;!:!' ,opemM r(conS_iIiIt or'va:liable) .

. ]!nQre.JDeo.1;, rOp~lio!l' ( ...... :) It i:mCI"e8:sesthe vablle of: ,III varia,b~,e by 1., For rfilUUple"

int i, j,~ i .. HI;

'j '" i++~

co,ut«j, «" "'<<: j i

BeTe, tnt!! !l)ubp!!iIt wrowdbe 1 m 10. Fir,fiilti. i:!:1 ,a1ifid~ed to j ~d.then :1 is, inC!1e:menlted II:)" 1 .i"i?; ••. poa:t inCrt!men,t takss,plaoo..

If' we :ha.\',e

:i~t i...j. 1 ... 20i jl'" ++.1:

cOut. 0;:.0:::.i" <,., ''"".:: j~:

The lilutpu,t we,uJdbe::ll2:l. Firat :ii5 tOCi'SmeD'l;€tlby land t;biEifi .!i'!ss~gn:g'j;ent tilike;$p\l~oo i.re .. ~p!J';e mcreme.iIl!t of i,.

IDecremeD.t, ,operator 1(", ... ) It deereRsestheva]Jue ,of ftvariilble by 1. fm eX~!p.~e.

hilt i" .~~

t .. H!i

Copyr ght rna': rit!

.21

j;; .1 =r: ~Qut.<·<:i<·<~ "'::;<:jf

Here the orutp~t winw 9 10. Fi~t iis assigned to ji and then, I is d~ment:E!d by lK,e,~. pru!tdecrementt8ke8p.~8,ee"

If w1'!nave

int i, )1;' i .. , 2.'0;

j =~~i~

·C:O~I.'.<"~.";:';:" "«.J)

Tbe output W',ouldbe :19 19 .. F~rsUI8 decnrnl.eI'J:\ed byn and. then :a_gnment taOCes place, i.e.,.poo d~mDntofit

E:mmple 1!. Find' the O!1~.put, ,of the .foUowi"IJ progro,m ?

1'1. aQ] Iloe'<; iost::-;e<lllli. 1:1;;'loid. maln n

in!;; 1/,; .. 5~. :'1'''''5. :I,;)O<i:it«x+f";' Clout <.';:" ,. ,,. I" CIQ~t '(''::'+'+;11,,' CIOtl,~<,(,~·t r.' "s

Clo~t<;o:::y .. ,·t<.:;"", "<';:",o~'Y'

I

:Soiution. 'Tholllil.tput ofthe pmgram. is

5t 7. 6~6

Eu:m:ple 2. Wnw the o'U~put ofUu!'following program.? f L nc]u;i,oJe,<i.ost ie-earn. h>

void ',loCI. 1.<1 (~

I

i:nt iX:"S, Y1.,1S,; ~OilJ .. t'<1:<x-·-; ~ollit« ". , it; e,olut <.:- -x,' CQIU.t,<;:<u, ~;

cou t< <y --«'''. "«-='~ .•

SQlutlon. The outPiU.t. of the pl'Ogntrn is

51,3,4.,4·

Esamp1e :3 •. Find the ,ou~put af the ,(o.ll.owingp.rogrom ~. li[!iCll1!.lde <iost rea.!!'!. h>

void ·rr.ai.n 0

i.~t. .... "'5, 'Y"~h COilt·« ... *t·;

Copyr'ghted rneleria'

CO'lll!;.< <--.:0:;;

CO'llllt .. <: <". "; cDat«y+~CC~.N<C--yr

SOlll.tion.. The Qutput Q.rt'heprogr~js

6.5.4,,4

Example, 4. Write the (j!l.:tput' oftlu! ,(Dll(JwilWprqgmm ,: 'Ii'.i.nc i udc<.i.G,s:t, r;:!::'am" h>~'

II!'Q,:J;d ;n:a.i~l ~ )

f

(OUTSIDE DELB1,2002)

tnt )1",,5. y=5; coi;it:; «)(--,~ ,eout·<::,,," t ''',,:

IC:OiJJt • .'~·~·(.~ I L".~ I~:

CQut<'<::y--<><"',1 "«--y;

}

Sobdion. 1'h.eoblWl,l progrwn. win not oompi1~811~fuliy due 00 the BCDC()JOn (;)1 present after mam.o. An, er-rormessage "declaration, ~1iD:ilhl,ted!iJtOOl'l"eCtlT' will be dis.. play,oo.oD oo!IlpUatlon.

UDIIlrYmiDUM (=') UnegB,testh,e sign, of an e~res.sion, Fol" exam,ple',

in1t ~esli,ljlt ," i .J r i .. ]0;

jl - 2:5;

~~sult ~ - (i-jl/5,

,(:lout ''::'<'':r@'~:!J :llt , ..... '<::·<resu H::

'The o~tPllt, would be resUlt - 9. T.he unary DltfiWi does6Q, CondlUonal1 OpertUQf (1 :)orlemlTy Operator

'The ,con.di.tiio:rud opemtor ItiD:Jjj51m.s: ,ofbotb the quesUofi lfiilitk a~d the ,colon. 'The 8)"ll~X of' conditlwual opertl,to;r is M fo.]Iows ;

expressiian. 11 ,exp~gi()ln 2: exptesa,1:lJn 3;

eqlfession 1. ia· ev',a1:uated. .Ifit ill true,the, conditionalexpM8siontokes 00 the value of expllession. 2;, and if Colse. eJtP~oB 3 MomS8 the value of t.he oortdit:l(Jj]a~ e~re88Ji(ln.,

'rho following figure i_lUSb:'ate8 this ~

?

false

Fir. 1 ... n1:ustratio:n of oolld:il~onal opemtor

Copyriphted rnmeria'

Fur .emmp~e~

:iUi:l,it ':-1: .,y , mOll!: ;

C.;;)I!(l,'!;; .,;,o:::'"'!l':[]I;te~, two ii.!:lt e9:e,~·s \n!''' ,,' c;,in>;o,x><>y;

ma::o:'" fx>yl' :X, y,~

'Let: us tail;,e, ,an.'Clthel!: examp:!ll·e in't; age.

caut~<~EDter your ag~\~~l (:in>'>age;

(a.qe>''''~ 8,' ?,eo!:l:t < <:."\n'ioUl, :m~~t 'i;I{llte\n/': c:o·l"l.I;.«"'n'lcLJ ,t:;,il!'!.' t;.!;!'ote\r1"'"

Arlthmettlc ,Mslgnmen1 Operators ~

C+--+' $liJpporh. fivlearithmeUc :a~Qignment ()P€';ra.t~ril that com\bjneB88ignment with ea'Ch

of the 8rithm.et~c Qpe:raJroni. :For examp]e~ In:tx, y;

l: =5,5;

y =10';

then

X l' =y;; X-I= Y; x.··~·y;, x.l'=Yi

x = :l!: + Y; nsu.U~~;;:: 65

x = x- y; re~tlt~ ~ = 55 x ~ .x. 'f y:; resu~t.s x ~ 550 x;=: xJy; ;tee·U:l~,x -55

(' '~

.~ }

x ~ ~ y; ( x = x % y; results: x = 5

:U:eretJte tiHlul:t otench ,statemellt. has been. used :iIl, :nextone rotaH the above :five statementB.

logical Operatora

These are uaed to' comb~ne twoo.r mO'retest ~.fflS!!:iaM.

c++ pr,oViJdes the !(NOT)"Ul@ && (AND') and I Ii (lOR) IDgiea~ ope:r.a.tors ..

I openl!oo'r :! is aanary opeI'3!ta!l' as 'ilt takellonly one opernnd. I~; r1W'~r!!es· the: l~gleniJ va]rue' of the ~perand!.

For axamp~e. i:~ift niilmll;

(lO'iIl'E<:< .. ~nt:.·~~ an. It.:r:lt~9'~t'\!'1' .. ,r

~ i 'f'»frlllm.~ ;' 1'1 ~i Ilp~t a val b:e f :'100. key,boa,:~d.

if ~(nlnnl ! = 0) && ~(I'I!WEI1~2:) = = 0)1) repl1eElen.ts .alQgical expremoOCl, than iteM 'be, written u9ing ~ O'pemtoras ifW (fiuml= ."". on && «numl%t2};::;;::; .0).

&&: operamr It combines two, or more logical ,e;x,pmssiona; and ·eva~ua,testotrue if al!1 ·the oondi.t!on:a: are tru.EL lea use nSisbee-n shown a~Qng wi!th!opem.to~above.

I I: O'~ator It tomibi_DJi!stwo Of mote ]Dgiiea~ex'P~ssi,oRll arut. eV81uatesto true if any one of the condition.iis true. F'Ol'e:xa.n:!;pl!e~

f.ldernofl~trl;l1;.e t.n.e. use or :: -;:pe:ca,({l!:' f ~fl.;;:l\J.cteo:;i'o;t'.l:ear.\.'h> ti~cludQ<con~o.h~

Copyriphted materia'

'\I'@ld ll'1'l11In n I

l-flt fJUU1; I::l~9:t!.O ~

COl.! t «";E.~:t.E!i!t tche o1il.!libfl:J;" 100 rur .2 00 \'n" ;.

'C.il'l-»l'Iwn; / 11 f1PlJ1L <I I'lWIll~:l:: flil:il'lll f!:By,tJa;<I'I"G

1£ I (tilU.'l .... ,., HI!!! I I tflol:lm'" 2(10 ~ I

ClI:;n:lt·.::..:"'You ~;!:'e ~ ~o~EI f1:il.llplN't!4'\.n"'; ~1~,~

cO'l;l;tc<""hl'u dioll~t 1;011.17"" D'~hmr5\rcl""~

III theilibove' ptQgt',~m if yOiu '~nte:l" moo 001'200, then I~he ,ctll[lditioill is lmejt.bii!fi chotl! !Jmte~t:. ,oou,tO::;<"YQU aee ai.good. f~Qwe:r\ll;IiJj: illi e~(lUted,

hope IResoluiUOin OpeMuf' (::)

c++ is :1Ii, bJook stmict1l!md lSiDgua:ge. A bWodk. ·~s a pu:p of s,tatemen:ts ,e:l:u:;~os€!d Wlitbh'l br:aoe:s i.e.~ {~.W,e bo,w 'th~ltt~e S:lI!me' v,amble 'nacme ,ca:m,ibeu!*!d.ford~ffereillt :plu'pOSeEi hIJ twCl ot ~ diiffeRiIJi,tb~ock~:. A looalv~rue h_ :!l1~ :Il:~ope.F(]r e~;PJJe~

fl,Oat a '"'1,2 ,. 5,~

1

The two d!eil~amt~.onJSofamtw,o .d;~f[e·re[!)!t blockB ha.v~ ,d~fferen.t!l!aruuesi.e_."th.e values o:f a "eMII"t belii8eCl. hnbe:rohllIl;geahJy.

W,e may also have ne!S~ed b~ocb in C++. Fotex.amlplej

30.7,11 ..

,I ,iriM [!:' ,b lQCc*

I",~~~~~~

f.

:f ID!l/1;; ,iii II'

Here the va:riabte ~8. ded9!mdinsilid!eth:ennDlGf·h~oot. OOiD.'too, Uised.lml li)iu:ter bleck. In C.iii+ w,ehave .~~ caned. tbe!!loope :~lUiti(Jg ~~to~ ~!WlIeoveJ' tile hidi:Jen"l!'ruiab~.e ,(global \l'ari~ abl@)from th~ 10081 v.lll'iDib.le.

,Ii llusLra;Hon of th~ USia of :: ~., ·i:)~e .u~~ollJt ion Q'pe£ator) Uncl Llde<:tost'rEll~[lh hi"

,i n cll!l.dE! <!'ClCl !l!s. 11)-

int l!I""l.OO. IIQlohl- lIi ¥Qid O1'J"j B [ l

'!n~ K",2..00; """ llOlealEo 1l1o'!!illl)

c::.lr~el:~· ~ ~

li. fit; J' '"X j

tnt; .,."'30fl, 1!1. lo'C.u to irma!!:" b1QciI!: eout.("c;" ~~U "'co;! ill UHle't bi 00.1'+\1'1 " ~ cour «"')l ",a«:C'(''"S;ld.:t; C::C'l.lL~<:,j·'t'~'t«y~<~nd1.

~eut:·(.c" ~ : :0:;""«:: X «<!'ndJ s

c)ll:IlJt<~""\rf\\o'U a,Xtr in outer blec:l!:\n"'~' ~ !put «: ... " ..... "".o::.zJt'{. ~dl.;

cOl.rt<.r": ~Jt."'I!r<~.: x<,;(.e.ntil •.

'Tbe ou:~p~.t ofprog,ram I.8,wiiU bei Yon aroe irn.mnerrbfoe:k

x:·~·300

"""2,00

:: x ~ :moo

You are in 0Ute[' hlock x=200

~::x,=UIO

:~: z wife",,, 00 ~lU! gl~t :~!"-i~lfll .e;~!lll'whe11'ei::!'i the ,abaw·progmm.

The 9~pe "'J!I(lh[t~on. ·operlilJOOr (~;) can also ba Uised.to d.efiiIle, 8 funret;ion oiUl;B~tde the boundary ofth.e, claRo 'ioluwiU b!lR!rn mo:m, aoo.u.tit latel' 0111.

S:Iz,eaf O,perator

l~ i.s,a ulUliry oompile.timc opemt,Qr.thatF!t't~ffli!l· thi! ,length ,(Ii" iIi'izlil in ilyte.s of the IMIriable (lr parentM1il~~d: t,ype·'S~dfi·e.r lkrJlUp.mt.e'.d@s,. SOj~t c1in.ool ;used. irn.the,toUomm.s two, rD~8;

.i:lh"Q,f ",',OIr I /t:rhe!!::'e .... ar is .O!,g:'; al re<!idil' decla:t'eC:varia:Cl&·

aflid oi!!.l:1iec!il' ,~dl!;ta t.Y!;le) IIwhe:tOi} data. t:lI'P@'b i!! C~"" d,ai:~ t.ype ,.

Most va:['ilib~es Bod d!lta ·~ypes.rcequ:~re ,diffefient a;Il\Qltf!lt,g Q'f~nte-.f!'u'!l ~t;Qr.age (In ditrenmt CQmpu.ters. So., the alzoo:f opel"a!wrheJps the same progtamto be 'e''''OOU,Wd! .on di1feten'~ypel!. of ,compl,liters .•. Fur exam.p!.e.

i.nc XJ

:f..I.00lit ~mou:t'l,t;

C(:l'UJI:.«"'Ihe si:z.e ,e:£ il:l:t:eg·er ·, ... ad.ab.l.e i!l: ''''''<:!I:il.ze,o:i (K} <·<:e:n:dJLj· Co'~t«"l'he ~hEl 'Q:f tlQilt 'Il!' .. riablei~·; '''«~£~~of (<t!flO!:l~r'!:t) «ona!..

'The aoove stat.ementawil1 pro'Wide tile size of in. andDoat type varla.b~e9 011 a C(HDputer (usu:ming the ,appropriate he3der f~e(jl,l.) have been i.ncluded in theprQgram),

1.~11 .. EXPRESSIONS

An apemndora combinatwn: (Jfl(}pe~ aoo opemWrs that •. !.VR.en ,evaluated~givB" a 8~11&1e ualue ~ .inow.n: Q (In, expreU8Jo.1l1. In C'+ .... the e:r:pressl.oDS ,e:an.ibe of the follo.wing types:

WAritlzmeotk· ~P1'elJ8it:J11

,(if) LogiaJl {or rnlatiorml ,txpres8ionj'

WO Mi:tBa .~pi"'£SSwn (I'll" ,CJlm;PDund e:~re8sio.n)

(I' ArIthmetic Expression

lit c.3inei.ther be an in4ger o!xpfi!!JIl,(O.11 or H. rwillt'l'P,~88ion. The ICQmbtnatolon. ofrea:~ and integer e:xp.rese,ion forms a mixed e1pJ'eSSion.

In.teger elpteiisions are formed. byoon.fi,ectlng integer (ionstau'tilandior integer VittiablesW!~ngintegel' mitrnneMc opet'akl'\$, The "~ult ·of·1m int.e,ggr exPT~iQ]lisalwa:y~ an meager. For e:xa:mpte~

int a-13, b-5~ q~ q;"aJ/b.

Th0 value of q wilil1 be 9 (due w integar divisiion) ..

Real ex~iona are fonned by oonnectingreal ooilliltanb SiDd/or rem.vadable8 using ~al arithmetic opemoo:rs (you mUiSt nGte that '%is not P..~ :oaithmetl£ operator) .. The resoilt of area] exp:rosion is al,wa,y.s fleal.Fnr 'euID.ple:,

flQat :x:"13.{1I, y .. 5 .• 'O. q;

q""K/y.

'J1'hevruu'Ei of q wilbe 2.6 (due 'mfloatiing point number di.vlIsio:rd .

. ,

Cln ILoglcal expression

n .ma_y wntmnju'lIt oJU $f&Md ay umig.rud varia.blelll" a coMtant~ or n .ma:>' ha:ltetwo or .m.o.re variables wi an.d OOMttm.ts) ·or· two t!'!!' m.o.:re' expre$$w,*" Cl)t:I;nect~ 1:zy t)tllid re14tioritU a~lM' lrciool 'D;~ra101'B •. Fw ,exBfitple,

i:n:t .<li.D,C~

Nowlhe vaHd.logica1 'e~.pJ:"eS8ion aft1! !'.i!<:b. .~ ;!!.-CI' >=ll,~,,",

(111" MIMd IEXp ... _~on

.ltis .~.·IM oomb'inaUa!17. .of.mal and .integel"expnssions .. Fbt t't.xampl.e~ in t a:"'13:.t b .. S .r' c:;:

.floatK"'li .. (I~ y.

Copyriphted materia'

27

y"'ohH

.Afterthe exeeutien (If the above ,statemen,l$·o will ha.velva]!Qe5~ and. y wiUoo 1.25 due to UtI1lriedmode variahlJ~5 u_scd.

1.1.2~ TYPE CO:NViERSION

.It lS the p:roces'SofC(J'Hvening, ,onll'pred:e{tnea (ype into anotner; In C++. rWD types of con\liW'· s:~onta~e pblice ::

(I) •• plllr::l;t type c:onvenilon

It i:~ petfbrmtYi by Ihe compit'er auttlma:Uoolly. It is gtYeIle-rally app~i.ed when am~x.oo. m.ode (!x:p:re:as]on is evahmted. WtfiR,t. tjh@te is no lose of infuiI'JIlaJtion. TiI1.@ C++ compiler' con· v>C!rtaaU~pel'a.nds: up·tQ the t;ype ofthe lru:gest. oper.and, also known, as; .type pmtD!ot~O:D. 'Tl:r~B: ha£! been shown i~ the ,ca. OrO whHe dijS(!Uss,fng· expt<!;gS~on8.

('Iii) IExpllclt ty,pe GORyersloR

It i.e pet1~ ,~y th~ ~urdltpem1~ngupon the need. The l!X__pressio!.'!! is ooDl.ve,ried.totbe spec~fic rype. Thiis is also kttown aslypecutilig.ln. C·B type casting is d~ne M shown be]~,w:

(type) eocpresrdon.

Her~ .typl't 1.9 .~ vallid Ct'~ dab\ type to w'hieh ooD:ver:ai(l!!l is reql:l!ired .. F~r ex8i~p~e~

<l'i1~" ~ n.oIJi~:) s·ul!1i.ln:.t

H-e:rc~ the variable SQm ia usedM 8, float variable anJ!lthe VM.UQ (lfft8'~"wihl befloot, type, ~QU mU8t nefe that type casting does not chanf~ the value' of the ,1}'Ql'ia.bte' orexpnSBIOq pe;rn'ftlnently, but it ~ C(.I~lterted W ~. specifir; .type (')nl:y at ,the; p~ (J1.ty~ OO$t',

1 .. 13 .. SYMBOLIC 'CONSTANTS

In C++ tbereafe tWG w.ays recreate symbolic oonstan~ i (i)uring ,co:m:t

(ii) using ,eau_

(OU.si:ng COO8,.I. The value d.ecJa:red Wllng roast canno:tbechangfiJ. 'by ih.e pro:pa:n:L We canU8B eonst in an e~pre!!s.illn. FtH"exampte~

oonst 'int :6~ze "" 50j ehar art· ~.!ib;eJl;.

Wllith the help of censljwn Ican. aeate typed constant.

I!deflne Cl'earC8 ,OOn:sl.lirtU with: no t,ype .information.

The aoo"le dec~at1ation can be done .~n the following way also

COrl:S't :;;.:tz'e!· >=1 1) 0.; char arc [si.ej;

I~ C++ a constant should be iniUaHzed .. Ifno,t, 10 is assiiped. to it. by defDiult ... Fol' p:oo,vj)d~ng·aIl. ex.temal Unk t.o a c(in~tva!~.e:;it must·~ ex.pU~t1y d:e:finOO, as· an ~tern. 'F'ar exam.~ple.

ex.wrll ,eonst size = 50 ~

Copyr"ghtcd rnaleria'

28

COM PUlER SCilE:NCE-cXil~

(U] U8fllig eUlmI. Enume!'J'a,wddJata ~ype means '''Thl! ·aF(/eNd set 'O,f difjtil't~t·C:()l!~~'u:.:n~ uwues rhfi;ned ,~ a data t,':IlM hI a /J'!'Ogrnm'".

It plrovides II way tD attach names to iltURlibers a.nill goincre'sses th:eeom,p:rehe:nslbH.U:yof t.he oodie"

The i~wge:rC()fi~t:.a_n~,caH be d£llflJld b,~ using eu:-um. ,For e'~m.p!e.

e. nUim .~ ". B,C D~ c

-_'-- - rJ;~ ~I_·~I ,,~ ,

Here A, :a" c. D ,1m'! takt'!'Dn in!teger oona:tnnts with values .0, 1, .~l,and 3r'Cepeotlvely..

Thea.boVE! d!(i-finttio~ is :Siatm~, M ;

eonst .A,= 0 ~

ol):net B= 1;

coll'sl (l = 2 ;

!OO:ost D ;::;; 3 ;

Values to A,B, C and D can be d.efirn.ed expt~clt~YM ,show,n. be~.ow; DDUinl fA .. :200, B ., 50, .c ., 100. D .., 500~ t

N~wA! B~ C and. 0 "11m h!lveva]UfS 200" 601,100 and 500r;es~~ve]y.,

Mum .0000.,y[JeS ~n!'lOl be usftdby Cln" Cl)llt" pt., pqt.t dc, .Q t/ulir ,in~nd ~1(dl.l.ie' CtlMot .~ .cha~ i.n ,rhe pfflIJrom .

• REVIEW QUESTIONS AND EXERCISES.

I.Wb:t: i;!lltb(!p:u;~e~fl\headerfi~e' ina pr<lgmm? (CBSEIU~'l:LB~~ ~oog) 2. 'Wll[y dlu' we fieOO the p.t'epr,o(:eB.80r d:i~'i¥e tlilldl!lJdix:]()fltrea~,:h> ? ~C-BSE 'Q~U.esUOARanll:)

a. Wh.R.t dOYlJlu. 'Ltiio.k ~s th@ mainadi¥wtR:ge ,oHhe 'wmmentJI in 0+-+, a!!l oompared W the 1[I!d .r-", C type Comment ?'

4. Elq)lailn! &e ~ar iP~ of 8~ CH iPl'O~· 'Witb the' bel,p ,af a Iluitableprogram '~pJe.. ,,\ (H

.Eltplwn tJne :!Ib;"iIcturc II 01'+ program.

5.. WriUlIii C~+ptQgl'aI!Il.t:o diIIplaythll f:QUowiiing output u~ng 8. Il;in,gie oout gW,tement.

Compute!:' .. 98 Mathematics .f::. '75 Practical Ii; 80

8. Write a. p:r:ogr.am in. C'++to, di.aplay tib,(! me-&8i1!ile ~PROGRAMMING IN C++, by .FIREWALL MEDEA",

'1. Write fl. 8~un't note On theevoLut]llifi of eM as. 111. pmgrammi:ng ta:ngua.g,e.

B. D:iffe:retlltia:ooliultweentbe f(lI~Q,wing ~ m cin. :air.id (lO'u:t

W) ,c<;llnd )t!> ,GpemtoT.8

9. W;ri,k (Ij li'JhQN :note ,on tho creation and! (!iJ!~llti!ll"J! (if a C+'i' p'I"(Igfllim On Turbo, G++:intcgra,Uld

d:evelopment envirollimeflt ..

Iii). Dcscrlillll1 the rutes Q[naming va:.riables: in C++.

U. W.M,t,Ll"t!thtlapplfen.tiun! o:fwi,d dtda. type in 0++ ? i(CBSE 'Qu.estionBanII;) 12. A 'vari!l.ble can be cteda:rcd. anywhore io s, CH program .. ~.pJ8Jin th~, a:ignifican.oe (lftbi~ fe:ail;~.

Copyriphted materia'

REVIEW Of 0+-+.

13. \Vhat· ~s th~ .B.ppHIlIiI.ti:<Jn ofth.e i800pe re5()i:ution .opera:l:o:r: ::: in 0++ 1

1l4.. Wi'i:i:te 8. ahott n:Otie ·Il\nva:ri:ous datatJ1)e'S in C++ w~th the hel:p of.exa:m:ples. 1.5. W.r:i~e C"1i'+ ·el.l;p~~Ql!!!!l fi)t th~· rgnQW~!lS;

en Volume ora. aphero gii ... ~n, ~t:'!I!Irodiw; (i m'.~)1

(i:O (a2! t-b2) X *" OJ

J:._y

(~Ut) ~(I, + b + c) ....

16. What ~s the differonoo in I!iwWrnlNlt ++cx; and l:++-;.

U... Whlltwill il:u~ the output. ·of &!! fQUow~!'lgp:rogr!lm~gmllnt : :Lnt .i~h.O.~

H+;

i·tu'M11,~

18. li.1e:acribc the ruhi" if .an.y. that s.ltGuld! be follewed while declarin:ga f.J.+*,i:'C[ilnlifietr.

19. Wh~ ~tw D.eC!e5IJi!:ty to itl(!~u.de he'ad~r mes in a CH progt;ll.m ?

20. A:n:aJ1ge i.n ,@nie:r to prooedenee iliighe8t.firsU the. following kinds ef operator.s; arit.mn.ctic" unary. logical} as81gnm,en.tj• ooMn~oHal~ rel:aiti.o:nal.

21. Defi!1ei the~nn;i! ~k~D. ~ord, Ment[fi~rilind"ar:idle,

22. Diff'enm,tJilite iJet'Weel1tbe, fonuw1ng : a:~ ide-nHfie!l' and Jre,yworo

(m oonl!.ml1l;tD.[ld.vmi,M~o"

:23. Li_st- feur built. in «:ala t~pes ,avaihmJ., inCH.

2f.What i:I!J' t~ep~O'!!I~Qf w;mweDb and!mde!l!~tion in .. ll!. pro.~.

25. Using onecout I!~ale,men,t atJ:1,y, prfut fol~lJ;wil1g me~ !

Rema&n:de:r can. be :ltJllnd Ilsiing I and."'!Ii"~n"t!l:G.peciru symobol.

:28. What wm be the outPUt of the {QnOw~ng codi.eo ? J~sti:f'y : (i) jilt Il:~O. ~7. c;

~CDSE SamplaPaipt!I'.) ~CBSEStQJ1pla .Papn)

~'~~/l;1; c::I.-i;l!/b;

G·o.![lt·<.::cor:.·<' , '·«d ..• ·

(i:~) .c:ho:r y.'.'A,'.;

iEdt z;''Z,''''' s' ·+'Y' C.(;liUi!:;·< <:z.

2:'7. WhymlpiinO fun(lti~niJ!!: ~~ial. G.ivc two. :~m!1,.

28. Write two ad.vnnta,gea of usin;g inClude· compiiller di~?ve. 29.Wb~~ :~Ij. thl1:!:UO'~~~~~~ betw~~n ':A~ ~d! "A," in OH '}.

(CBSE DIU;W, 1999) (Otm3IDE .DE!I1·HI", 1998)

90. Find. t:he outpilit oft-he foUow~nr pnlJgra'mJ : tinGl~de~LQ5Cre.ro.~~

'l1~:D,d rnainU

int !jl=20'i i.t;~t. t!=7Ci'j

/Cont «1'1«' , .' «~ ;.B«''''\n'' ~

31.. What 1S (!h,e plllfPOs.t: ,of !81zeo:( ~pe,r,a:tor ? 32. Wha't i8 the :pufiP0&6 of type ~o~vetCSi(jn ?

38" Differei:'iti8,te· ltJetween9. ru!;l,4ime emit' a.ndl 9ynmx: elCOOi:'. Gi'Ve a.ne enufiple, of' .eaeh.

((lISE :Slun:plle Paper) 3~. Wh9.t do ,·(lU und~l'S,tn.nd by tYr;Kl'CMi~ing ? Gi:V(l 11;flltllpl~a: of exp~ldtand. impiici.t type ca:sting.

1 .. ;14 ... CONTROl.. STRUCTUlRES

Ma:t'j_yfU_l'!dio:ns :Il!f11! UlWd to· plEl.es:mesoo,~8andm:!l!n~putatethe d.Bi!m stor.ed In. elhjec~8~i!n C+,*"", AfuRCtl/IJ.fi is n set, of sta:teme,H,t/st;a,teme:rl!,tsoo. pe:r:form a speci:ifte task. For SiOlvin,g a spedfi,c task,. we ma;y have m,[lifiy aJgorj.thm:8i,. sOfiile :sim.pJeand. (j,thers c:o:mp~ex;. 'The p'Ng,ram should be wri.twn iin.au~ friendly wa-y :GO t~a;t it maybe modified la,te:-r on, by anyone wlJQ WIshes 'to c;naIlgllit, if the· need aiLaes, lrebugging and ~maL~ntenance w~u1:d be ea_syif the program, i9 coded wi.to. a proper fbnnat. ,Forntllatillingtbe f)bjec.tive ,of a good,progrurm, we use· on.e' or!lny oomh~nauo:n ~rtbe~l~owing tb:ree oo[}rn:i~ .stn!:C~UreJi =

,I. ,SeqUll'!'nce ccn,trd8lruct~

2~, ,$I):lec.tron. coilUrol B~ruclure fbrnnc.hing)'

3, 'Lt:JapiflJI oo~trol S-f.l'uctur:(t fre~,tjtii.m, (Iii' i,t~rotwnj

:1. ThesequeDec, oonkoiekucturema.y consist iIl'C a si:n.gle lrtaWmen,£; 0f'8. scqru.e.!Ilct! (It Btamm1l!l1tswith. ;8 iEringle ent,ty and s,ing,l!e ,e-xit .. FO.r' e~ample~ on<l8signmcnta;tatcment or tUfi,ctffion caU. 'The !:!eq-uence: 5trucb,'_rtl hi shown below ~

Statement 1

Exit

Copyriphted materia'

2, Selection controlstnJctu:re perl'o:rms one o\,l,t.oftwl) or lll!!lil'el state.e:tlts dl.epe,~.dl:n,g UPQIl the ctl!lldi:tc.JOIR. The oolooUon oo:ntrol ,60tnlctillre (IF.iTHEN~ELS:E) iG ~bQwnbe~ow ~

.F:ig.l.5 (b)

ease is an important'Ver:eiGt\ ·of gemeet~oD (more: than. a sin.gle ye~or.no dec~sto.n). 'The fl(lUovring fipre n~~stratesl:!ariation QROOledJon z ,'he. ~ control $Jr~.t'UNl.

~

1

F:iI"., 1.5 (c)

3. :Looping !!IItmctU'reh~s, one condiitiDn (sim.ple OF ,eompo'und)a:nd a sequence s,troetu:r~ whh:h IS execu.tcda number oftim.es depending on the oond~,u~.n. (logtc.a] 'eJ!:!pre.s~ocn~ belng t:1'U13 0.1' ftt:ise. The loo'piDig structure is shawn helow;

Copyr"ghtcd rnaleria'

32

1'1,.. ),.6

We ean code an'yp.r~gram us.mgtbese three struct1JI'f68" Any poogmmcoderl, usin.gthe: above· ,shown conho,~stnlic:tur'ee,is ealled(l sikuchlr'ed.program Bndthe nppfi'lachis knorwn as atructured, progrWDmiing •. The :ma.lin o~ject.i,Y'es 0:f structured programm,i:ng ,IU'e f"tadabH·, 'itYMd da:nlty 'o€programJ nl>llint.enante a:llidl reduced. testing pn:.blenul," Stru~tured program·, m~ng' eHminatesthe: use of ~~ (oo,oom:lit,}onru oonkol).

C++imptements, dle aoo:vettu,'ee' oontr,ol stro.et~res end, uses the fQUowing 'language «m_strucb :;

1(. Tlll· uilt l" M . ,,~. t,nh1'nJ'$ ~ha- ~tjU'jm:entstutement mput and outp. u~ state·

;1:10, lim~l!o' .. L. "'.(J.El., ~ ;S: .. ~, •.. - -' t.· " .

ments"

(~.~"IP,. ·t~ . __ n U'1 (.oM ron, I;;m~8

(iii) ~i'if.:e,lse {two lVa':f bm,ne!ing) and switch (multiple: bmnehJng) sta:te,m.e.nts' (iu) while. for (en~r:y controllo(}p$) .and ,do--while (e:tii oo'ntrolloopj'

([I) a ,N!:Strietea. use' 'o.f,goto statement.

So~ you may get aD, id.eatha.t C++ ,oomhiines the ri?a,t~,s of 19b'uctu:red p,ro,gra'm'm,ing with .001" features.

Statemenls

The,", o:.ret~ ~nstJ"!'l;ClwJ1$ given .to the compu.ter $'0' p€,rfol'm SQ~ (retia-Ii and form, the $,l1wJlest exe,culgble unit within 0:. C'+¥ progrom;, A 8e.m~cQIQn(:) te~mi~at:.e$ ft ~~a:t;.e:tnent., Ttl!;!! empty or lUdl~tate.ment ,is wriW:!n ns

; I la. :n:U!lle;tO!te[[le~!t

It is useful in the :sltuatio!mlwher.c tho syntax oflan:guage DJoodis til1epreeenoo ofa :!!,'(.,arleQlentbut the prmgrnm lagic ..toes not .. It wilD be used in loops and th.@irbodies .

.A ,Bi:mp:le state:m.en!t is a :smgle s,utlooment wtmin,O,ted,by ;O.som:iceJ:o.n .

. A compound. 8tate:ment~6fo'rmerl! by t,W0 .or more statemsH.reeD.ciLoe;ed by a pair of braces; €I } )~a1GoknGwn W;l 9 block.

Copyriphted materia'

33

CondllUional Statements

jf statement. Itte5t1il 8. condition. 'The .trml!>e,ment(s} 8(i9()C~ated with. it is (are) executed when the co:nd!it~ofi. is true. otnerwis.e the ,st;ateme:r.rt{s)i:s (ate) :not @x~ted! .. The syntax ant sta,teme:tlti~ as ~h.own, below ~

if (~co(!ldiUon)

statement;

It ean 00 shown. wah theihelp offtlm!' clIart given In .. F'i8. 1.7 ~

I n8lli1 sta1ememl~

I ,~if any}

.Fig.I .. "7

F(l1re~mp~e~ i~t*. ,/,

cout·<:: <"Il.[l~i@:r the '1:.'1010· irltl:.I1g!~l::'.!l \ ri.'" J ei:n>:;'>'!1"'>:n

if \x>:!i''i,

C{H;!~'::<"'.~

If fhe specified c(mdiU~ is Il!ot. ·t:nle Uten sta:tOHt.en.t is,nolt @,z;ec:uted and. comtro:1 is trangferred WI the ne·x.t. :!!tate:m.ent,. T.he statement ihelfmaybe a oo:n.di,tion!d.eta.tement..

If:..else .,tatemen.t (ileledor:). It tests a. conmtion .. 'Th~, iljt:8!wment· 1 :ta ~OOUI\ied wh~n the 'Cj)ndit'~Q~ ~S!~e othet'Wj~ statement 2:~Se:lCeciUted" The state:menltil umy tJh,e.msel'\l'9be COl'tlpi,lund. $ta;~m¢nta.

'Fhe 6;y.n.tm!: (If :if;'~ise stote:me:ntis as given bei.oiW: it (eol'!rd!iti!O:n~,

~tO)jtem,efit. 1 ~

·els~

.at.<litemel'lt. 2 .•

n c~n 00 fihown 'Wi.th the be1pof n~w chart;give~. in Fig. 1.8 ~

Copyr'ghtcd rnaleria'

34

I~e~ ,statem.ent (if any)

:F'Ol" I!l~!.lmp]~'j int 1:11.!m;

oout'':<:'''En1:.~'r I::IU:l, T.lum~It\[I"!j ci;~,> :O'lJ,Um'i

if [nun'll; 2~'=!) )

!;:out.; .;rn,l:!iJ;O::':;" .i a eve]'J,mLlJlmtce r\;~";

elsE!

cout«a'luml'(o('" is odJeI, numb~r\n"";' P,f:\Qgra:!li!i)! 1,9 mWii~at;,e~ 'Me 'Qfi:f-e'~oo ~~rt:e~ent,

! !leap yeilz cnocJdi1g 1I'ln.C'llldl;!.:iQ!Ir;rE!Olom.ft~ f,Lrl'C:li.uh;:"cQI'I~C '. h. .. 'loin ma I n (I

~

11:'1 t YOGa, r ,~ cl.r:.;~~~ ~ ~

COQ~cc~Ent~~ ~ne yea~\n~~ c: iII;.,. yea I ';

CPU!: <1:';:' '" ill'" t

it [ ~ (JI'-e~<:riii4 O~ H (Yf;!'arUIlOI-D) ~ II rYe!!lE"!I'W(l--Qn

e:l!luto«(,Yoiiin:<><." .l.S: i!! le,",? ye.tr\"'''~

C!'l!1i!'

lWli:JlJ't';:<1iP.iiLlk~.(''' i:i f1Iu1;; Q l~i!lp yeOl,:I:\n";

35

The outpu.t 'lii:f pro,SlaJID, 1.:9' wUlbe ; Ent.er the: year

201]5

200S is[!;o~a. ·Iea.p Y'ie.J.fl!r Enoo:r the year

20M

200Sis :9i.leap,year. ,Nes.ted·U Statemenis

One at' mare ifSM!eme,~t($J .embedded within the jif£tate'~n~ ~n! ,c,all:ed' .uted~! The fo:noIWrungif~b;e! ~tate~e'~t is ane5ted.i:fsbtem~nt ne~kd. t;Q lev,e~ thl!ee .

.i f (~Or'ld.i t1 O.n U

I

.:I. .. f ,~col1d.lit !'Oi'I~) {

if (corlditi'on3) !!lit; a t:;!(l!men1:,.] i else !l.t;at·ement,;2 •

3t.atemen~Qi,

Prognun l.mO iUustratesu6e (Ifru:::steod.l1'statementsi.

flR.ootS' !;If i'I !;I31adlLilti~ (H;!.llnion U.,ftlCltldi@<:lol1 :L'1!!!:am.h>· -i.U!lcl~d~<cuio .. jip·

lnt:'hu;ie<;rn.;ltl'l.l'I> (I [Or s~rt:. ~ ~ fUl'lotiOf! '",old !MLf! t ~

{

lflo!!!!: a. b,.c" dl~·t".IIt.olr l'It:2, i. .. n.gl. imild'2, dnl::r() f

,C'Ol.lt CC~·B.f1t:D!: th~ I!:!I:li1fli!:l;t ~n't:.~\tI"' .. cin!>:-a!-"'b~""r::.

Ilm:l1m h~ d.aIE.OI

CO\lt<:"""\n.a.="<<:;alli~'i' b="<:<t:I.o;<"-' C""''''~<c.o;.<\n~·.· it (""="'O •. O~

[

ilb=Q. Ol

C.Otlt <.t .. \ ~ Equa:t.l GI r~ I. ~ d-=qe ne t'8 t e \ n" ; else

CQ1JYr ght rna erla

36

COMPUTER 8C~EN'CE-XII

!;:l;Iut<!.€.·'\nUIlB~= ~il;:lt.iIJ!l he.:; :!IilllJle ~Ogt \n", c:am:';;O;:"'nRoct R"'<x:-1ji

;L~ rdhc)Q.tll I

cout <:~"'[fRed.l tOi'lild. dlst:ihct :r-bcl.!> \~ri. XJ(.1~ I ~b-+sqrt ~~.i::i~) ~! n~. O~~) ~ I'lrZ-I-b,-sijrt. I di3C~ J I rz, il ~Q Ii cIlILlt...:::.(""\n\rlFil"'St. 1'oot-" O(<'Krl ~ I:OL'l.1:«"[I)"1lI8reee,f1I:i .r;oot-""';I'C;.r-::l.

if '~dh!;:~ .. I}. 0) •

CCllJt«"\'!'I~'e~.! anlll I!3qtd~l root:s-\n,ij" xrl .. -l;! 12. 0'" i1~ ~

Ml"=H:d •

c:.'Gt.U;,< ..... , n ~ n.i'".l t"c5t;. rCClt"=ij!! .. <~rl, 'OOUl: .:;(')'\1'1 \niSe 1:011 Ii!! zooe .. ,,<,~it:::2;

11:101.1[, <::<-" \ n..ComplL''K, eClI'I~I.ii":l'~' .. e- ~o_ati:il \n"; :Kl!;l -b/l2.,O~t:1);

~mgl·:1<ln ~ -ej:i,.!lC::: II r~ . o~ i! I, i

I J~qUd't'a !l:'ClOt Qf <l fI~~ltti'i1,e !;L1Ji~e:I" J It:'ali'll' 't:, til' t:om~lIJ1r:,ed (H"I th~ :!:fati!ffi.

i~2=-1-fQIfII1 ~

,;:eut.c<"', nl'lr5t. 'f'O c t. 1~:, \1'1'" t COtlt.,:::';:"'~R.eaJ. pa..n .. -"O:::<!ld I eout<<;,"\rtIrr.g. JlJ;IJ:",~ .. n'::<i:.mgl~ c:elll:~ <.::" '" n \'ilB~ c~nd_ _j:'fifJ't:. i:!l ~ \fi'l"~ il:!C-hl.t.';:oC"n(te1il pe..n:. "«K::r2. ~c~~~<M\pl~9. pa~~-~~<!~~.

3.]

The outPI;li!t of program. LllOw:i!ill be: .Enter thecoeffi.cie:n~s

4: 1 "1

a. ::::::40:::: 1e; ;:::: '7 Comp~e:l: ·Qot\iu:ga:te roots Fit';St root is:

Re,al part ::: - ([1.:125, .Img .. pari. :~. 1.31695,7 ~fidroot is:

Reldl part ~ ~ (lI.:l25 Img., part :~ .~ :1.3:16957

,Enter the coeffici~[l18 l~5 6 a"=lb=~6 ~=6 Real aad distinct :~ts First :root =3 ~ndroot=2,

En.te:rthe ·coefficients 14 4

t1."".l b.., 4.:: :~.4i Relll~nd eq.II,J;a:iI. 'rQQ~ F'jr;st root. =- 2 Seoond r,oot =, .~. 2

The H-etse-U [Ladder

1JJ(l cc!llditioU!PJ Bod the sm,tementB ,ca~beassooi8if.edin the fbUoMBgFurm.: .i f ~:cQ'fI~it !Q:J:1"U

5 t.1;!t.e'l'fU:::l:lit -1;

Qls@ ~f{aDnd1eion2}

3tat e:rnent·-:2:~·

el~(!! if (oondition3l $ t~t(l;m,tl,!i I; ~ -.3 .•

The diffe~!!!I.t C(I·nditi<tmJ are ev'aI.ustedhm the i!lto.riand Whe:Il a. con.ditloR is evalu.a.t~das tn!J.e'~ the, f(dl~g .sm:wm.ent(s) are, execuited and the :ree:t of the stareme:nt(is) are .skipped .. The above shO'Wlt 'L\IlMtruct is known. all il~if ladder •.

Copyriphted materia'

Ic:hoel~ ~hti 1:'Ii'fH! Cit a trianqlc ,]n~ dnd t1m . .t:I~I3Ol ..I.Rcludf:< LtHIt."t"~ilm.Il.·

·ln~lude<c~~!o.h~

Hft"'liI.U:l"::~m..ii1:~. J .... IItQ.. :l{fr;t 0 fl.lrlt:t. ren ll' 0.1 d m.lu .. n 0

t;Ciut«"'Ent;.er t.:he tJ"u;ee 31!i:1-e:o ~r t ne o;.,I;i,<!.~le: \n"; cln!->li>"Dl»e~

IleooQ .. he d.a'tiil

!. r t la---b) U ,a==~)

CQ'"Ut.< ~ '" \nEqul..!. <lL.f!.ril J. ;;; rl.a-l',ig 1 e '\ VI n .'

eUe if~ lo!l--b) tb-~~ I H,~'--i!:il)

Crlttlt <;;<:!" \lll sgscele,'1I .. iamrl "\WI";

else

[;QlJt<.<n \I'IScalene t;C-~<lifl\jl!:!\n"; 5'" (;!!, ~1;I+c: In.. o~

-il1:''lfa-sq~t ~s~ (s-aJ .• ~s-b F (-z-c}); I::!Otl~<';"\Il:iW;"e'a"'·'~«ilre~«"'s-q .• Un"[_ ..... ;

(!l~e

eoul;:.:<c" \Jllilrial'llglle- nat" p05si.ble: \n".~

'Fhe ,output ofpmpanl l.ll w.:illf>D : .En:terthe three 8~dea ofUu~ td80g'le : 3 45

a =: lib =41, Ii: = 5 Saa]ieMtriangle Area. = 6 ,sq,ll~jts

39

Enter ~hetln'eil ~6~des (I.fthe triangle : 4: (I 5

a .. 4! b;:::;4 c~5 1900Ge~e9 triongl~

M'ai = '7.,8.06248 IKJ. ~mi~5.

E~tlJJterVhe three sid,es of the triangle : 666

a "'" ·6,b :;: ,6; c .::::6 Equn.!;li.w:ral.tnar:!gl~

Ama = ]5.58846,7 aq. 'units

Entc:l" the three sidesef fhctrlWIgle; 102 5

:a= lO b=:2 e=!;i Triangle net possible

The S:wiltCil1l StatemaRI,(MulUselector)

It tests a con t.roi ,Il!Xp1itl!Sg,[OR. (condititlJ!.j.TM c,ontrol i8' tmn~'T"redtQ one a/se!!.le'J'!:iJ attern:atiPl!,s. The :value of the :Il'xp,res8ion may be of type intor clutr but nol af type float aT Mubl~', It is:gen.e:l"sUy u:sedf(l!t' :m.e'J'!:1IJI dri'!;!'enoptions,.

The :syntax o.fswi.tch sliaitemellilt is a.:sf.o:uows :

Si~ a:teme:n~ {~) ; :bri;lal<,,~

~Itat;,eme:n.t un h:Le,aXi

b1l"e,~k, t;t~t'~'ro~!T~ h,·} ,r b~e<l!~~

Copyriphted materia'

40

True

True

Fig. 1.9'. WDiI"k~mg 'Of:SWltclil I!Itatell'Jien:L

YQutihould alway."! .PJ.l~ a brm" :l!~IMm' ~(I'er Jhekut caK $I4Wm.!lll ina: ",udlttka, yo~~)' , oow I.Cj add atwtkel" eGa B'tatl!.m.e,I1:1 at the f,nd ()ltJreIUD~teh. .A irwitc#l!tatem.e.nt can anly wo,rk (01' eqtulli~y a)'mpcuilOn&. The· ch.a.rncUr ~Umt.8 when ~. in swUc:b.~Ilrt! a:tuO'n:wtm~y oonvert~dh;l ,their ~j[!r$ (i..,II •• eq'wtJakm ASC.Jl rocks).

PNgr.am 1..12 iUuBtrat:es the eonoop,t of s,wl,tCh statemen.t that ox:ecutes thes3!me 8Ita:te.· men.t fOil" more than one case ,oonstants.

Ilprl.nt; 1TIJ~j[ oi. diiJir:ll Ua. ~ 1ll1;l;r!1:h

~ l'Lalude<:ie:s' re""'IT'! .. h> ,fffilJ! "!;:.ir;J. iIoliild C::CUt fUflCtiOftfl lnclucle(ao~l!e. 1"1::> i I ro~ e1~~l!Ir ~ ~ r'd.I'LCr::LfH'I

l,I'qid main ~)

~

int mOl1th, aIrs!!!:' I' ~

.r:::Cllt.~o(''' \nEn'l:lal' tlle mContn I\'IIMn:be'fO (l-12l =" i 1i:;1r;1»J\l,On~hJ

sW';i.~~~!qI;l!':ltb'

C~Sie l~
case 3:
~!lS1!!! 5 :
to_ase 7 :
i:a.!oiE:: 6 i
COlse 10:
ij.~$e 12~ 111;.1119 5t.0!.temel'11: wU.l be e~{!o:urelj .joel' ;!ill th'e ",bove ~rI~es COUt-<l:ot;" \ I'Ill d",y.s ..I.n the- rncmLh";

l:1l::i:HI.]o:;

!;;Q'Llt.c-o:.O:"fl2.S ~l" '29 d.a.Y5 in t.tre m~m'li:n.·· ~ hr:e<lk;

GEL~'= 2 ;
IlO).ISI e '\~
i;;i!.S~ 6~
~!l~i:! 5:
t:'ase lU I ;t:F.i=- ~t;illi:.nl'l't;Cm: T,oI'"lll b~ oiJ:KI!II::IlIt;'.:!rl for aJ..l t.h·~ 4hu:~\I'!:: lCa~~~ I:lWlI:lt. ~~ .. \ iiL3 C d"" ys 1.[1 t:..h e mQl:n1'- h'fl :;

!:n:mlJ.i:;

d@'t:i:!ult: ~ C!out<.:" \.nwrong ehCl:l ~~\.t1I". la1E'l'a iI;:,;' 115wlt~h ~~~r~e~t ~nrl~ h~r~

q:BI:;-t::.h (); II f:r~e:::E til'" 'mcri~ ~r

1

'1'he (Jijlt:put ,~~gwlit!:l'l. 1.12 will be ; Enoo,1" the Mf.lI[!t!h number U-l2[) :5 :::1:1 da,.yiSin the: mon.to

Enteif' the fIl()!llti! ~umber O-l2;) : 9 310 day,s in the ~O[f,!tI:lI

Enter the mon.tlliil.f!llllmber (1-1.2) :U5 Wmug ,choice

.lJ], (;(IS!! of n'!l'.!iIt~dlilwi:tJcb •• e;g;!j!o 8t'ai'o!l'meJlJt~ tJte CHse ca1li:g:~'mt'S of tJile' outer and l'h~ ,In.eer s.witc:h •• c8:t!e Cail ,&ilIU\f same ~a1tt;cs.

42

break Statemen.1

JtcaU8esan ,lttitfTam the8'wi:tch body. C'oIltrtllpes to the fimt sta.llement foHoWiing the Eill!d! (If the s'Wiwb. .flwwment. Iftne break atateml!(fit IS not used, tbe control pa ss@sto, the next (ll;Ule ,.,.mabint.. andt..hE!! rem~ining S·tatIHTl3l'nts in th:esw.ikh. cl)ns:tc~c:t.wi:rlalsl)oo eilCJeCu~ed.

defauh Keyword

lThe dem.uU keywoil"d (if pre!len.t)~n tbe switefru. ,eonBtructgivesa.w8ytotak:e nctian. if the, 'Vllilue 'I:tftbe switch. ()x.--~fii,():n doof.! not matchwtth. any 'o:f the oose constants.

LoopllnlStatementa

O'H provides three: sta.teme:n:ts fOifrepe\!JJed!ly el(ecuHng :!:!! flecjue.n.ee of :'3tarem,en;t\ts)' These aN while,. de-while 8n.d.for:.

whl!leLQ~p

Th.€ e;y~ta~orthe while is'tateme'ot~;s as ltlUow\Si; ~ild l~ (cOF!d:ll tcion)

)--.,. 1i1Ie.xt siate!"!1lElrri: ~~MY)

True

iF.I«. 1.1,0,

The 11()giical ~pre.ijSi<}nOf c:ondil:iionmesfio.ne and the same. and the ~y of loop may h.~.vea si;mpille: ,or com.pou_nd statement,

ThsfoHcr-w:in.g pohttsshwJd bere~e:mbe'.red. while iIil:Bing tbewhUe loop; (n.lt moynot' be I!cucuWl toon ()1'lCl! ift'h!r! co.nd'itz·oll1 fs false.· .~nUially.

(iO ll: u·~cutii:dml the cand:it.ion· ro·ma;ins .tl'u:ea:nd the co,~tro.t carnes out of the loop when. th conditu.nbeoomes frdse.

(iiO Theil'll mU$f be ·80\I1iUl' loop termin(It~ng rondUoon fnswe t.he body of ,the l,oop to (lu.otd infinite loop~ng ..

Let us take an t'!)(smpb:;,fo:r finding the area (lif drele,The P'N')~ shouhl ~nninate wben 0 (ze'I'(lI)'89 radjusis ent:er.ed .. Pro.pam 1 .. 13 iUu:stmte:!!, the use of thewhUc oo:nstmd.

Copyriphted materia'

IICalc:ula't,lEl ~he .u:ea 0f tlil,e cl'rcle U!l~il r"HiiU,!!i ~1fI'te'l:!;;!i:i H '1 n el.J,jI d~ ';;:.lo!J't .I!:',~.;m'I. •. il,)-

inc.l.lllde<(lQflio.TtI>

• Lt!I~J.11dl'!<mt1tllI. tIL>' 1/ for:. paw 0 t'UtJetlcl\ 'define PI .'30. 14139

'l"aid nte. i n ~ ~

{

t.lt:lOit. rae! tl,H;:, ~l"ea, (;1 ~~ct' (j, ,

cout.:: .. ··\nii:;nr;er t.be :c:adil,l.:;' (t.0I t;.!3IwmitlOl,te enter 0 l : "~ C1rp'>1:'<ldlU$J

~h!12fr:.adius! C.~l

~

i f ~ ,i['<u::li ~lt3'l> (I ., 0 t

} OI]';e'a.~~I ~ PG'W (,~atii.\I'$. 21"

~out.«"'\flJ!il!l.ea J,~ ; "'-C.4:a~a·l:;'c" <lie( .1:l:n~tiIJ \.r.t";:

else

~Q'l.u::'::""\t1i.A.&:~a i.s not. ]lc!l.s;!;blp\ll"'; gE!t.en 0 i / !!:J::ee~e t.he .rn:Qr.lit.cr il:l!:C:l;IC~(J.

1:tOliJ~«" \nE:'ri'["~t'I::'t'le I:'.l'\ltlil~ Ir:'C l:emini1DC!< ~'n't:'ar 0) ~"t ~! r'i,:»rartJj.i'u:l9;

The output ,of program l.ma wiD be :

En.ter the ~r~.m,us '('til te:l'tlli!rilla:te r9n'~r Or) ; 5, Area iSi '78.5391749 ,sq. units:

Enter t'be ra:di:u.s, (to te(f",m;~ate enter. 0) :0

kample.Fhu:l :the' syn:t~ ,e'rriilr(s)i" if a;~y~ in the fbUcwingprogMm

(OUTSmE Il!EI';~tlH9J

inc ludiC} <ios t.r.eam. ., h> voidrn~in

:!'~t R~ w~9'I(I; 'OiihlleW>6Q

:~"'io'l-5~. ~lX1it;.ch ~'oIi'J I.

:2 (] cOIlit.<;·":Lo!lri!c~ :~an'91e ,. «enGl •

. 30 c,outHHidrdle ~a!'lge"«~~dlJ

40 C:O'l1It(<:H.i.91fie.:r FI:,~ng'r!"';;:<@ni:!1L,~

Copyr ghred ma'mial

,Solutlo:a. The syntax errors a£ie:

.1. The ,ayn1bo.~ ,-tlm m]a!!,ing before mclude in the first ~rnG. 2'. Tb@ ( ~ .,e mls;stng after maln in theeecorld. Une.

3., A. comma (,,), Wllmt be there in the smtement

lIlit 'R: ;, W [; 90; ,after Rib~il; :r!O't asemiCt.l,lon ( ; )

4. 'The sttl.tementwMle W .,. 6Os:houldbe WritroHtiS

whUe(W>60)'

6. The tiree ~tements mside swiii,tch al'eneithe[" having' case :n'Qr" a:f'ter C&ut .• thee Bh9~.d. be wrWten al3 :

ca:se 20 : cOIl!t«'"l..ower Raog!eii<:<endl;

case ,3:(); lOO,ut«"Midd1ElRilingl;li~ <;"<endl;

case40; (lout<."';'"Higne:r Rarnge';«endl;

dO •• wh lie loop

Th.e 5.,yntaX oft~e d~w'hi~eBtatemen't lS ·86 (01l0W:8 ~ el:e.

)lib! U.f: {e'G ~:'Ul t ,i ,;HI} .f

Itca:nbe Bhow.n. wifb. tbe!help o:f the thrw ~hartgiven in. Fig,. 1..£1:

False

next s1ammen1 (jfiU\Y,~

Fig. 1.11

'The foUowiing(l(li:nb3i sh:QIiJ.d be rememoo:redwbile using the d,o.wbHe loop' : (i) It ~, ,~c'UredQtleMt()nce,

'(ii).r# ,18 ,e.wClt'~' till.,}w oolldiUonre~aJM ,t'J'oo'QO\I:d the oontrol coOmes out of the ,l~p WMn, thCC('JnditiOl1: beoome~ {!lEu.

(Ui)! T~"ro must be mrn:e loop terminaUng, condition insrde tke bodl ,()rt~ loo.P'w ,(,!;tj{jjd i:;;ftnJte toopling.

Let ustalse an. e:mmple! for ,e:becking a :Ilililmberfor ar.! armatr{Jog nUntliber. Anum.oo:r is caned~ng ifit equals the sum oi£Mea ~nt8 digWts.

Copyriphted materia'

REVIEW OF C#

Fol[' ex.amphl ; 1, 15a~ a1iO~ an; 407 ,etc ..

Here 153 ,"', 1.3 + 5::l 'oi!" ,33 a;f!!!di. srumi1arly !!iI,the!!'i\i .•

JPooigrs:m 1..14.~1lUl!!trate,S tba use oftbe d~whi~e ~nstruet.

IIChl!!.cl!:. l!i n.ll.mMl: .for. 1I.:rm!l!t.ro.rl9 'iil'u::luaeo::ic!itreBIll. h>'

r in I:! lllJdlll "~wd-o., h,

'mid ~ill.'( ~

I

.1 n't, iI'lllHn, ''I. .:!!Hlm, t', i'l.lr!lll:!'!' t .1:

COtlt.<<''''''II,lElnt:Ill:· t:lio nlJmbeli::'"\n"'~ ciJ"l~'>num;

n_"ll'lr.ml. ! I 11: hr u!!'!ed a<6 aJ: d;rl!Pfi;lJy I.ti!llxiabl,eto !Jt:ore nuiP :9Utlt='.o ,~

do

&"(1\-10; II COCIid b t.\u~ ~G:l1lIiii:Ili::lIiU: iliUm +=I-l""~"r' ~

1"1 1':'10~

wtli iLe'~ f1 ~"'~B ; 1 r ~ ffi.mr"=f1~'

COlilt;·':;<"·illJlrn.;:;.:;_" j.s iI:tIl1!SL r ong". els@

c eIllJ~";< I!I UL'll~<:" 1-9 1I0t a:r~Sit.;nn;1 g:'~" ~ ~e·tl!:t'H l; I/lr:~e'l'i!;j t.he mOr'lH.Ot'

T.he outp~t of program l.1.4wiHoo : :iEliuwr tbill number

tss

153 is n:nn~t:rnng ,Enun·tblli number ao

30 ism(lt a.T.~,~~TQng

tDrlCl~p

Tbe,whUe B!lld. do ... While l00:PiS .p.re, g~n,eraTh]y l1!eedJlIVhe:nthe!f!~be!!" ofii~erntiiJ!DJ!iI Uha:t. is. the ntm'lber of times tbe body !®f t~.e l~p· ~ai 'e:x8cl!Ilood} ],8 not known .. ThetOJ' ,~OO;'I' W Qed' w~n~;u.mhlt.l" 'Qf ite~ioM is known ,~n, ,ad~gnt'e.

Tb.e syfiW. '!lUhe f001' loop is. as f6Uow$, :

Co yr ght rn ria

It can be s&own with, the help ofthefilow drlart ,given, ,in Fig .. 1.1.2 ::

Fl:a:iSe neX!li sta1 en"llMlt

-- - - - '(li'any)

IA"itiaJiile lhevall!Jl:8 m OOJiltrd va1lfabfe

Pi. 1..12

Ilrl:tlalilzatlonE:q:are88ion.lt.ie: e~ted ,onily once when the loop first; .starts. It pro.\lim: the loop variabJs! (oontctOl variab:le}an init~al Vahle.

'Teat Eqmen~on. It. 1nmws ,tew!tlQIlial operawrs. It is: executed every t·im.ethrough th.e' ]oo,p' be:fore tne,body oftih.e loop is eJ;ec~ted. If the test, '~preS$il)l1I i!!t.ru,e, the 'body o:r ~he lo~p is execUltedj and iff:a1sc. the control comes (J,U,t, oftib.e }oop,

m.cnmentiDecremeb.1 (re-bdtiaJ!J:zatian} Es:p1Ieuiorn. U liS alwa.y;s executed a.! t.he e._n:d ohhe loop. 8fterthe 'body onjb.e loop.

~ body of{}ru for loop' ,is ti'n:(l'10Md b] brnC'e6 ,aRd ill, ,not er,eeu/ed ifthesemioolan: II t~rc a/tf!". the ,WI' 8tatemf!!illl'.

Fore:xam,ple, int:: i,~

for (1~~; LC~lDJ 1++J; I,

J

In the a!bovestdement tnebod,Y offal' loop iSiniot e,x,ecuted for tb.e' value of i fril'll1, Ito 10,

Copyriphted materia'

41

Silt if we write: the abavestatement as: in" i.~

for ~i~l~ i<~ID; L++}

then the em" ~QOP w:i.1] be eX.ecutedW Urnes and iislncreB:Sed. illy ] everytime. When. we wriltel ; lilt 1.;

for {i-~D; L~=L, i--}

1

then the {O'f loop wlill be~ec!u.u,d 10 tim.es 8ll.d im dec~silld.by l,ev,e.rytime,

The re·i'wh"luliItion e:tpn!8sion can; be both fm:nment./ aecrtJl'Ulm upru,io~u" (I'nIl)' ,1 b"~!k)r.iW:

Ql~,"!}(l:l~' al$Q. .

lhamp~e I.. What wil'l be the outptlt of the {rumwingp'fOgrom.: '(CDSE nRLBI,2(04)

f:lt,:nclude<io!ltreallilll,.I'I;' V1oid~in~ )

±ifi~ v'lo;;S!v:2'!I,lnr

ro~ lint x~l; x<~2; ~++.

<:;0'111 t, <-<-t, +'vi <.:; " \t. '''; ~;v~ --< <e~:~l p' co;~t<,;;--v:2',,<:: 'I \t ! «v],,++<<:endl.

So~.UtiQD. 'The OIJjI~Put wHl be ;

6 10

8 8 6.

6 8 B

Example 2~ W~t wUl be the oufpm afthe fbllowtl11gprogrom :

(OIUTSIDE DELJU,2(04),

i i.:n.c)J, !!!Q~<: :j,Qst.r:e<l:r::l. t.~ vo~1d ~ai.f1! ~,

{

Ult ya:r:l"5f v·,arZ.O:I.0f

for fi~t ~~l; i~~2; i •• }

c;out;·«'!,i'aJrl.ofc+«' U; ,,«-,-v,a.r:2<<:,Elt':l.dlr c!out<:<'!.i·ar2·~~<:<" \t "«++vO'!:t1.«,e:tI.cU s

Copyriphted materia'

48

:Sol,ut:li.oa. The ou:~p«twm be ~

5 9-

9 "

7 7

.,.' ,f

'9

Example 8., ,FJ;nd theu~ e:rrorlesJ.if aJt$ in t:M fQllf)win:g progrom .~'

(CDSE, DELHlt2002J

'i n~l~de .: iuS! t:t'~a.m. h;:vo,lid ma:£n ( )

I

,10)

,i;nil;. ;I'Ii:_ c,iln«",,;

fo't: (i.nty;;; 01 ~ ydO; :yi+}; CQ~~:;' ;>K.+Y ~

,Sol:uti]on. The syntax el'Ttlrsar,e:

LThe ,cin<-a;!ltDltem.ent shol1dd be

'oin?>x~

2:. Tbe ,Q(lu.t»x+y;, idawmentehowdlbe 'cou't«",,'t~:t.

hampJe L Find the lI)'ntax errorbJ). ifany~ il'! t-hefollmuinc p,rogro,m :

o(CBSE UEI.W. 2002 C)

,1 ~clud@,< ,iOo~,t, r@am,.t!o> vo~t,amaJ"Jl, { ).

;i.tlt X;,r ,ci,l:j,< <:,l!:;

for(lnt r-G; I<X} ,++} l

SolutiQ~,. The syntax er;rQrs: a:r~:

L TIle maitll ( l funcMon powd n~t, beterminatedM!11Ii, ;: 2. TOe cln«x; sta,tem.ent sbowd be

C,].t1>,>"X; i

a., The variable z i6 ded~,oruy for lar~oop so the stateme:nltcou.t<:<z; wiU.nQtWf>'rk Pl"Operly. Tllereforez should be declaredbefDre the tor loop.

,AJsothevariable z jie, ;a:ssig11!ed Q value "l wblc:h. iis Dever used, wiUool:!uit ina Wm'tlltlg during compUaHon.

Copyrighted rnmeria'

&mnple 5 •. IVill the, folZowi.1J.,g ,rogrnm !~~UR:l sur,~'5lfully :~ If·7$ot,i!!mie th~ tM~.(sl. ~CBS:EnEI.ml,201fH)

.'inchl.d€l·'::stdio .. h> ve.id. !fie i!l { )

lnt 51.., :5.2, tl1i.t:m; ~:l!=s2·""Ot

fo~tK.O, ~<11; ~++)

(;1n« ;:U.ffil'

::Iif {r1urn>O~' :ll+"l'l1Jm~ ·11:1S1il s::.\l-/:I:l.l1.rn;·

l

:Solut~!II)B. No'. th~ eibO've pirOg;r,~m.wit~.n0't e~~cu.te ~U:.G(!essfu.Uy. Beoo~.Q!5a:re ~ 1. The header tlUe lSH.ot propedy~ncll!l.d!ed. Jts·h0iJ11d [be

iLli f1cluae<:i 0 51;: ::r:~arn. Ill>

2. Vari:a!b[e xiii! :~(lt d~l~. It ~hO:lJld ·be,d:~la1:'>edas:

iDt l1: befo:re U$ing iillf.j)}j' l~op.

3 •. Also :82: "'/in.U1ll tsbjji,uJ:dbe wn.tten ns 821 ~ .m.!lIm. ;

Let us' 1oVIiroa.p:rognun 00 find lhef'acWrial ~f ~~.um.beir .. !Pr~a:m lJ!.5 iWlust..t'ates the USB offor·100!p!.

II.Fd!:lt:·mr1al 0'1: ii l'II.u.f11bel." lnclu~~«tostream.~~

• Lf'lclLHile<::con!o. I'L!>

voii:ll main (

{

int mmm;

!~ng ta~tQ£la~-lr I:.Lr3'e!' ~ l ~

~llIut>!;·<. '" \!'IJ:E 1'l!1:~ I!"t: he I1.ti:mlH~·!I;:

~J..El;:' >'Il,UITI' i~ HHim...:al

C::Cl!Ul:"<"'\n"<"':;~·IIJrn«·" t.u:;.t.~l:i<l!l I':!o't defined"·; el!i!·e

fpr (ll\~ i-I; l~nl.l:m; 1+"'1' f.l!lt;tQd!!il =H

!;Out",,,;:"·\nFilli:ttl!:' ia.1 cf "";<liiIurn«" is "'«IaC!cor1.1I1 ~

Copyr ghred ma'mial

TheOQltput ,o:fprogram. 1.][5 wil be ; :Enter the Q,umbet. ,8

FacwnilJl !J,r 8 is 40320

Ent~r the number:- '"

- ,4 f8!et(l!rja~n,@t !defined

Nested Loo,ps

Nesting of loops ,means QIIUl' '0\1"" mDI"El ,1000p!l wUlin a ~OQP\ For !@!ltBlIll,plI9,

:l.rit 1., i'!Uli!'i, :sum. t,o:r(lct~ i<=UJ:H+)

{

cou1; <;..: ~ 1" ~' sUm"'Jl;

fO.l:: (ntum ... ,2;: nWII<"i, IiltiJn+~' ~

'co'~t «"'"!f"'«niYm; :l!urn +":I'Ium;

I

1'ryooft~_A th';::Qy.~pu:t Qf~'eabl)ve prog;r~ ~e~eF!lt,

:N~w leiti us wnw aprOgI'RUito generate the patttlm O,bl 'Hue!! gWvwbe:iowlIll8:l!llgn,ested loops:

A ABC ADOElE, A]3,CD.E!FO

nineiEI

P,mgmm 1.ll6 illlLllstmte.sthe,ue of n~d ]oa;ps to s}ml!l'ate the 8ibo\1'!€l shown pt:dite,m:

I tPdn't p,nt.erfl

:lllle:1.udtn <).OSl;; ,riQa,m ,. h> "=' tn.i:lu.rl~<~on to. It). \;toid lIlijlifl ~ ~

[

i~t n.i~j.~p~~~~ chaar celli euacrll ~.

cout.':::<"·!l:nter;t:.he .... a-1:ue of rt<-13 : \n\lj'" j! c:lfl;:'llo,p,;

co Qt:<=-:;'" \n \ flOeS :u:ecipa t. 't:';Mrn t s \ rtI, \ n" ~ fl:l1: 'i ~; i.,;; n;: iHJ

CQU'l <0::" t • c::11"'''.;li.''

.f'O:!I1:' (1-1, l< .. 2:"".l-l. j'l"'l' I f

ctal'l~, -e .. cl'n:el'li+t

'The w,1l.put I)r~,l'l!D1 ll.16 w:iill be:

En,mli'th~ vab,!le ofiIJi, <J=1Il3 : 10

Desiir«l P~,ttem is

A

.ABC ABCDE ABODEFG .ABCDEF'GHI.

AECDEFGHIJK ,ABCD:EFOHI'JKIAI ,ABCDEF,GHIJKLMNO .AmCDEPOHrJKLU:NOPQ ,ABCJJi.EFGHIJKLMN()PQRS

broIl( Statelil'llenl

It tmnsfet:s: co,~twJ au~ ,of Gl, ,loop, bypasslng th~narnml ,loo:!) cond'i~iQn\ tCa'l;, We hav,e already 'use(i], the breusta,mm,E'Hlt :for separa,tirng' ease laibels: in. i!!iwitch. i5,t;aJW.e'nt!!. ''liYhI;!:f!!a, b[lm:m enoom1,U!md!. :ins:~de 8.looip:r.bhe~OQp i.E! 'oowmb'i~tedand the ~oDbl~ PUSi&.ei to tbe iStaro~ Ment foUo,wmnlt.h@M-dy of t:be~oop.F'OT ,e:xRmp,le,

111l1u&tll'atiol'l uf bf,eaili s-tat.emii'f'l't, , i ne lud.e< i 0$ re&fl. h>

U IV;:: I ud~<'Ooni tl1 • n>

ve lll:! ma.:L1J! ~ ,

1.1'1'1:; ailuml. nWl'll:.; ci'Q

~

I:ll!'~c:r t) .~

CDUIt.«/'E'ntc-l:- t,T,o;IiO a~~!!!:u\n". Din~~~urnl~~ftum2;

if 11iI1lim2 0)

breaill..

Copyr ghred ma'mial

COMPUTEIA SCIENCE-XU

else

cout, .... ·~·\liIWe can divide".:;.;:numl<::<;" by " <;><m.Lm2<:<endJ._ cou 1:. <: 0;:" \nE _r'e.s,s a.n~ key 1.:0 co fit. i rHl£! ••••• " \ n" ; qetch{).

while '[nullI.2 ! -0);

In Program L]L7~ ift'heuser ,euteD the .seCGnd.l1Iii!UI1beir as '0 t'lliJie~~ as division :by Ow:iU ha:_l'!!g 'ttle ~Yi!3te:mI' tbe lbf~ak!!!ta,k!mellt causes ~mmed!i:are termination ,oftheWoop. We are not aetu:l.dly perlormiIl,g d~risio[l heroebl.llit if we do so tl1en b:r,eak can r'e.5Cill.e'~5" in cJl:l:!etJn@ ,d[viso~ happens 'to be O.

It can be ,~jl,o,W1} M,th tt.u3 help ,orth.e I.ow'chad glv,en in Fig. 1.13 ;

IN'O,l'mai~ , Imturn

CCl:n~lniIll8 S1atemsl1It

11; frtnr;e;s the ,nm'itl'l'mtmn ,Qfthe ,loop tn ta.'ke' place, skipping ,an;l" .s~ate:rnent(s} fo/Jowing U~e oo.!1:ti,nue s.t(ltl:!l'ment in th,~ body of the loop. In. thewhi1eand d,o .... wh.i~e loops)!. the con~rol peSSie'8 totJte oO:Dd!itiona~ test. In ·the fo,T. l00p"tWle ooniUn!iJe, statemen,'t eauses the ,co~djt~ona] teBt rulod t~e!l'l tche r&ihdUaHzatiJon. part oftne]oop is ,executed.

Ther~no1llriDg iIlowe'hart oEIl1IOW;6 th.e operation o:fthe CDint~:lliil:.lie ;statement;

NQrmaI re~liJIm

True

Copyrighled materia

IItllLi~trill:.i8fi of '~nt~I'I!J'IlI !I'J;,O!t.efll,",nt l;Lnc;L1il!!le.<iollt.r@.t:I[!\.h~

j:litil:::lu'\Je~'OI!:"rdo. h>

v-oid main I.)

I

ll'lti,j; chsc~O;

.,~~ {t .. t; .1.<"]; 1"'·

1GJ:' ~ j-l ~ j c .3 t j-'i ~

li ~U j} r;:oatillitle,

ICc:l.UL.O:::<:" \n"«:i«'" "<:< j,

Tibeol(!J,tput mprf1>,gf.am1.I8 wm be ~. 1 1

2; .2

a; 3,

I:D,th@ aoo/W!!pro,gram when. the,voJ,'Ue of i IS, not eqrual 'tnUl1!it ofj, the emtinue sta.t;.e.. lU.en;t rtl~ee,th.e !output, SblWlDM1it b,-p'8J:!s~dand theoo~j.ulpuses 'to tbe oo.nd"iibG:ili11 test 'of i~ri..el' f~ loop.

eam!ma Operator

Il: aJlows! uS'to ,evaluate twoa.r mondill~ioot ,ap~s;sio.rMtW'eJ'il,£Uer ali'~iWk apressmn is: aUowe,d.Thetesu1:t ia t'he value of theriptm0.8~ operand.

'Tu, oommii opera,tor ifl,usedprimariiy in thefirBt ,ond 1m ex:prcl!8:10HfI 6<£ II fail' tMlp. For uam;ph~,

±i1t 1" j;

:for ~i."O. jl""lO; ]-i.>O; iil-+', j:-'-)

'OQ'ut«" \ !:!F~Q,g:g;;l!~i flgi ~ "'f~!l" ,r

In Ute abo'vGI segmsllt, ofth@ pmp,am l'be arutpultwiU. he appe'a:rin:g' 6 time!!, as, iii. and j ,a:r-e lmtielized be€on:! 'the 'to(a.p, ~s en!temd, .Afte'r each itew8ti<lJl,ilis m&~e'men'OOd adjijs decremeuted. It is eQJ~jva1e:rwt to .~ fO~](lwing' wb~le! loop"

.

:;1.-0" j-:i!.O,

whHe I :)'-1:>0) ~

Copyr ghred ma'mial

54

The ]lOOp' i~terminaood wben bOith .~. 3.iI1Id j are ,equillJ,. tl:la:tis 5. 1le 81:1:10 F.u ne1tonl

It iill m m1:l:~ime library muh:J'letita~ CGU18es (I, p,wllmm 1:0 e,nd, .. ret:ul"mng the ,e€lntr'lll ,'0 .tM opemti""8~s~m. Zem a:rgu.me:!l!t to e:riiOmean:srtbt!!;t pnp.mn.ll!ndiBllJIomudly .N(lD~ro :!.'I!~ .•. m.enU!: indli:.ca:re ab~l ·itermina.tffi.oH.

F:(jl" !I1!N':Ellmlp~e~

/ICom.:pu·t,t- t.1'u~ I!IqlIli eo I:'QCI ~ a:f <il .nl1lilhjl:c' in~l~~~iGa~rearn.h~

lL he 11ildv<-c'J~ it! • h>

hncludt'<~th. ~HI(ot: sGI,r:; n iliiG:ltJd~<prI:lC[l!l3 .hll fer- ~'Jdt ( ,

V'OiH~ rn~ 1]'1 ( ~

float; nIJJI1; ci:r:lc I: II ,i

.1:~l.It(''<''Eimt.'!Y 't.ib.1! .I1I.WMU!.I!:\'Il" ~ t::in"'''fll.!Hn,

lII~~m>-D.Il'

{

~o'ltI::<~Ij'\nSql!la:E'[! rQ~r~ Qf' "~,,:;:~um<t~I' i,EI "<....oi:!'J."!!:"t (.n.~III) ~ e'Xl. t: [CD l j

Tb~ ,Il,pave pfQg,r,B'lll .. tin.d9 the: S·I.jIUlmill root of poo~.tilve :llUm:bern only WJidi. t.c.el~miJtuli;ti)eS ~fi~U¥ wh!!!p, ,emijQil ~8 ~tlJoooot>IIlRd,tn c~.a·~ .J~.gu ,~1].wrnega.tiV!llll'lJi:mlbeil' the rnoHa;ge "Squ~'n~ ro~·t Oi'.8, negatoi've IIlnmbe:r clIi,n"L lHl.Found"]8 .plinLed.

The gotDI Sta.tam:enJt '(~~nconC:lillolllaIIIBrancfilin)g S:1ateMent)

It :hi!; giv,en, hC:No:nl.y &:r thc!l:ate of,cQMiP~ettU~ssJ he(:a;~a'e ~6~ o.f (l got-o make$ apm,gmm difli,cuJt,trO U:~l'S~ ,and debug~ On,ce; Y'ml 'h<lVe some e~perie~~ ~fwritit'!l.g C++pr1@>pa:nl$~ 'yQi!J, wiUfee1 tbattbe\l'e is, IliQIiI.eed to use .ga,to .•• iY'OU win Oh'86rve t_ougho~!ttlhiii!IbMk.

The ,S;;y:nihax fotgotD :lis, ; g,5t:.O Jl atillolf

The laJool is :a! valid! C++ i.de!!ltifier fQ'i~w>~ 1!:ty OI1lJon. You Pl~ 'hav,e .)!' staW:mi!!n:t; :M'tell' ·the liiibe1 in the[o,rm:

label ~ ,st.O!!t efi!'ent~ FOl'e:t:a:Mp~e.

ifl'~ :raul'!1li

:teNt: (l'IlIlm-.=:l; rnl!m<::=2: O.~ DI:!m.H-)

55

i r (f1lLIrn\2 "''''OJ goto t.a.rget f COI.:H:;<<:r!lUm<<" "r t.argel:.·~ ~

P:mble(!ID. 1. Write G C+t progrom, to ,swap' (in:fe,rchpill!d two ,numbet'S witho·u~ uS'ing g: fhir"(i '~'(1riab'le.

Solu;~jJD[II",

nSWIlP!lin!!J! af t.W!:;i nl1mh!!~~ wi.:i:::h~nJt 'I!l~ir.lSJI a t~p'b~l!!I:ry var.ia~l;E

',inc it.lde..t i Oiil t.E'·I!!:,u!l!l • hi> 1,1 nG1.UI;i!e:~C:OjIUQ.~'" "I':o!.d 'II:liI in 0

f

i.nt. a, b. ~l~sc::r ~ ~ ;

!:lcut~'iC"Bn't~ t~1!! twO' nU5l~r~ ~"; i!::il'l"~a~~.~ ~

CeQ ut. ot; <" \ n \ n I n~lJt,'t.e" !'I\liPbe.r S <l re,.; \ Iil " .. eol1t:"< "il!l?'··.:j«.d;

cOl.n~.::<:'1.\ fiB- if ':'::ib,

I J~wapphu",

lEi=a-b; "=Cl-~i

C::Q,u't~("'\n\n~umbers aft;'€!l!:' s'htapph.q :a,~e; \rt"'~ i;:iPIJt:<;<"'A="'>;;.:;a.

cout«" \ rnB="~b:

9e't.oh ~ ,~ I I ue e.:e Chill! nJicmlhl!:

The output. of program l.20wiU be; Eti,~ett!h@two nl:i!.mlbers : 50 .4i~ Inp!u:tted,~lumber.t:i, atlle:

A = 5!!) B :;:;.4.(1

Copyr ghred ma'mial

56,

N'liUILbets .aiJte:r SWIiI,pphl,g are :

A.",,40 iB,=ijO

Pro,'lem 2. Write ,!):!:C'¥+ ,P!"iQgnamto fiNl the ~maJl~:s;t of t1r:ee' nIJ-m,jljer$. :So~pti(),1iI,.

~'i. fie 1, ~dJl:' 0::.1- ~tI t: n: ~~ ,1:1,;:,;< j;l.iilCll;l(ie<c;orl'iQ:.b> ,",old I1UIHIO

t

tnt. 1'1>1 b, C ~ ~lr.5lc::- H.

CCiUt. """if \ I;j E:n1;,eolC tll,e tiu:ee cf.J~el!f~; \n" ; cim-~a:;">~>~c~

If~ll.;I!M

i'f' (<!<:,;::,

COIJ't<::'~·"\.ll,S!1ii!!lJ:lr;:!,~t. rtumb.ar i~ -"'o!:<i!i~' !!l~B

COIlt;<<''' \.,Sma:lle5" Il'l:lnWe:J.:... is ,,""'':;0, I fI U s,l:~temerl'l;: e'p.ds

lalS~'

], U 1,')<'.1:: )

'C ~u.1;<.o:: "',lorn;;! 11 e!'ill: ~ Uii\'&lell; .:i ~ .'~ .::.:::h, ei;!3e

C2Ut~€" !iilj'lalle:9,t. numl.uu:: ~ s _"<..t:c,,:

III e l s I::: 5"1:. oEItem'E'nt. e n~:Io geu:bO;

The ou:t,y;t of program. 1.21 wm '~, :

Enter diI.et1n'leoe; n:1;J;M~ ; 84, i5 23

Sma1!ru,eSit ll'tUlliber' 1& .:i:, 28

Probmem 8.1iV:rit~,a: (J'++pwggFa:m tQ'~!'ler~,t'€ /iFIJJ n,flbonacci rer:m.;5i. Thefibonacci: :ter:ms arn O~ ,1. :1:. J~ a~ i .. 8), lfj~ •... ,. ,.

solw:tiolll.

ii~cLuoe~i~B~~ea~.~) lIine: 1 ude,'<coll i Q _ h.> vaid main ~ ~

irlt: .a,b, nJ'~:.It" 1'L,>C;!llJc:I'q ~l.r~~t::U~

Cotlt-1l"o;!"\nliicl1.r i'Ik'Il:IY flconac:t;;i 'teI:'I::1~ :l"l!:iij'flired ;10"2 AND "-~"7\rI"i' IICtrc'l:k l'lIal'i:.btt !lJ~ }I'~u~ PC for- inte9l'er ~i!~g(!

eilii>;:.n.

a=ll~

'hr .. t j

e~61:::c~"\n'·o::<:.n<:"'~'f.i:h~!ilal:!c! tEIJ:'.nf.!l arE!'! \0"".' I;~&<~a~c' • '.;:..:b;

r;=I;lLllTt-.2i.

,,",h i le~ COI.1Il t" nl i

1]~!olt·a4b.

COil.! Ci <,,(,' • <: ~:m:lXit. ~ !;;QUI1fE +-t •

.. '~!:),

h"'l1~;;:q

1

The 'lJurtPlIi;. ,a:rp:l'IDgram 1..:22 wm be :

HQW m:etny filbO'tn!nocfu,t~nl!l!fl!~eguired :;:'t::;2; AND 0::::24 ? ][0

llOfi.bon~cci te:nns ~re :

o 1 1 2 :s 5 B 1321 :S!j,

Problem, 4. Write a Of-+ IN'~:m, '~Q' 'c'keck a n;~mk"IQtp'nnpl. A ~u,m&e",'p>l ,iq prilml (fi~Jil only dilrhlQr~ arc 1 a:~d ;it~(!l'f.

Solutioo. A numbe~ p'>1 is prime if it 'i8:not ,diivisg~e' by :m:_y nl.Wll!bel" ifl t'e~~1 2 to~n:tegrnl part of:i:tlH, SqUafi'l1 root.

'" 11'1c:l~':I'C!Ii!'<U:I~l:i:ea!iil. tt~ U II G llJdti-<:c I:;)!'fi a. 'b;'

~il1.nc! 1J;df!<~th. I'll> II (or 19q1il: ~ ~ f\Jnl!'t ion void main n

!

tnt. II 1il:nrt " Ill, t ~ clcBCIr (n

CGltu:.c<"'\nEnl:.er t.he I1I1!!JlilbCl[; \n". c;ir.t":>num,~

if(num.c;U

t!ian~<""\n"«!:Il:=rn...::<" !jli ~~t: priwa"t alse

Copyr ghred ma'mial

't sqrt II~ urn l' ~ d-2~

w!i'lil~ fd< .. t ~

if (filiUlliC!- [) I b:t:eaJo;,:;

l:I!+ I

i;f h:i","'t. j

t'u~t"<-=;'"\o"'«m.JjIi1«" ia prl~~"~ e.lsj;!

r.1:'lu,t<o:"\n'''(<:.!'Ium .. <:'' i5 !'IO~ l'I,cl~'~';

'fhe j)ij!.t;put of P'~~gram Ui!3; wi] be ':

E:n:ter' tile ntllmile[" ~ 3:1

31 ISpritl1ll31

EIi:ter the numiber' : 241

24 is :n,otprimel

P,I'oible_ 5.W,.,ft:e a; C++,rocn:un .tto< che~i; ~, n~m:ber ('br p~lioor-o:me. A number ~. paliMrome ,~f J~' m: eq~' t'(ll :ft'S I'{!u,!l!l;',fle :i'I~m·bl!iI'"

SoJlItiOiD.

, 1 n.cl iJ~ e-c los t. JI:'E.1IIlIl .11> Jnel~deccapic.h>

llmlii!3lfle~ l~ng n1.m,.I'l,,;I;.eiOl"I"LUm"'O. l..I"Iit r~

fJ.l,,,,al;;:~ r) 0:

ll:oat<.c".En.t:.e'r tllil nWliber\n\n'''' ll£n:;"~nl1mt

~·numi j I Slt.g-ire nU:[l!< ill i;l I;:h1f[l;lqY v~l!:t:<ibl fI while ~ I'll -l'J}

t

59

1.:e"m.]r:n"'£ev~~n.ufl· Hl-tc~ 1'1I"1.0~

1.1' ~.~'i1!Vllllm""'nlJJrnl

cVrI.'!!;«" \1'\" «'fjlj;_m<:.<'" i~ ~ ~l!:f1d~'l;Jlilii:"'!-li"'; el~f:

eOtl'[-c .... \(I"'«II1P<~N i,iil I)!Oit a paJ,J_nnrc;ne\J1'" ~ ~et.~i'I ( I ;

The ~u·tpu:t ml}imgf.am. 1.24wiU be :

Enl!;er~e n:IJMOOI.' 128454321

:123454:321 ~!i, ,(I pWindrome ,En.wrthe nlDlibex

420

420 ]5001:; apaUUJilrnme

Prob.lem. 6~ Write a /01' 'coop ltJ'1" tl:a:e followi.ng:lle'quenoos' ofsmtell!U!lI:t'Bw.i!hm:.i;~effe:cfing;

tA~' t;li!1,tpi~~~(CDSEQg,~dOO!!! H$nlr) 1nl: liva ll:le= 9 ;,

I;O~t,c<l.v~.llll.@.(';('~~dl ;:

L.tta], tl,e.""L.va Jl ue - 3,.;

c,out( -e [..v,alue+l «,ea d 1« LvOl! ll:!e- J!. -e <e ndl r LVoiiI.l,,!.l!e-LV<lilu,e:-:3 ;

c:,out< <lov,i!I.lue <'<e:ndl ~

SO]lUti.OD. 'The ,qulpu:t· ofile. given ,~,u.eIl1a!' of ·~ta.temen.t i8.~:

9 . . ~.

'1 5 3:

&I tilefio:f:' loop :for these sutemen.tswin be r~riin;t Lv,a!ge=9i: L"~~,\le'>=Si Lvallle-:=:2}

oo'l1.t«Lva]u'€:o:;;(endlj .

. Probls'm !,"What valltCs' :will be aBsiplib), the vonliihli!!'o8i lUI, ~ll~ ~c gn4 fb~l' ,~rthe

,~c~lto~ ()f~' fbUowing pros,ram segment .~ (C.SEQuetdllm, BaDk)

Void mad. n ( ~.

r

lnt .1=0., U!OlJ=Qo.LLb=O,uc=Q~ fOlil."'·O!, wI'lHE:( 1;( ... 05),

,~

60

QCJMPUT1ER SCI'E!OOE-,xI'1

c,i:]S@ 1: .. +'+U!i!,;
ca~;{;! 3:
CiilS@ 4 H1"ll;l;
,:;",,::.e 5 ·t"t~IC; d.e fili'U]:t. :t1i" fai 1; Jl~ioiiitctl

IlwhiJle

court.':: <';lOll. < <e!:ldl ~ c,o~!:.< <:;l;ib«elldl; ·t:i:OiL:lt -e <uc<:::(9Ilidl; co~tc<failC<endl;

~.

Solution. The val~.eaaaJ!igned. tolJa)l!1b~ ue ood. ·f.tillwiU 002 .• 4,5 Ed. :6 respectively. Proble:m 8. Ho~h(lt will bel:. the O!dplrd of flu: following C++ p,rogmm S1Wnwnt z

t:orU!'!It i"'1;i<'=5;'U1'}

PC;I1,lt,«"'\p" i £o~(j-~;j<~i;j++)

:SoIution" 'The oe.tpu;twillbe

:Ie

1. What i:8, a compound .statemellt ? Whelm $eJOOtIDn l3t3temant l' Whieh se~edio,Q !ii8!tements. does C++provi~?

2. Write a program to dleck.whether th~mpu,u'ed.,cbl:U'a.d.er is, anal,phabe·t,. digit OJ' special (!,haractf!r.

3. Ina. ecn~l.structIl1'!e .. ~teh.ClI.Se. explaJDthep~ Gft1siIl\g defawt.

- 4. In .. anye++~toftam,. u.8jng8Wli~ !!tatemeHJt,. ~fallh~!Jl:atements are remofved from all C'i8e8 of !!-witch. :statwnent, howdoellitaffeet tbe,furu:thmnHty afswitch 8i::a.temlInt '1 GiVe '~~e.

·15~ ... WtilteR. program. to pBemt:;e a'8imple ~tot. -

6. Wriltellt. C++ ~ ·to· piW,t·Uw day of a. week. A. nWlilber ltG, '7' is entered asrnput.

1. Wri~ ~.p~1n tofi.nd. th.e uea of'a ci'rt1fl"rt'!Cf.lIngJe or triarJglo depunding upon th.1! 1I:IIe-r~ thoie!!..· .

8. Wha,t .. ueiiter~t.l~. "t.a~nw ?E~j;ntie~tvra,t.iIl'D statemeEl,ts pro~d:ed by C1"T.

9. Wha,t, .~!!i the d!iffer~ncektwleeDD.'W'h:ne and. d.".,while loop,? ][0. What is: t;fie ,eft'e,c~ of" O(lll1tmue: in. IJ: loop?

]1. Write lit program. W PDe-l'.atearmsual)g numben upw n :spec:ific' .limit. 12. Write 9i P!r,r)gt'Mll:. to ~erate 'prime D.u.mibe:n upto a I!IpeWi;.:: limit..

Copyr'ghted rneleria'

REVIEW' OF Cot+

13. W.r:l,teSl. ~io, abta.in. the prime, t~ ,ut. ~ti:v. m~. !~. Wrl:tea ~toprln', mvtti~tWn taibJe illfa Q'WDber.

15. Wriiea. ~to ft1il.dLCMand 'OC:]) ottwonumben..

)6. Write I!I. ~·to ~J:BrU'I·the paUem .. ;l'Hm bflliow :

1

1:2 123 1:234

1'7. Wriw,s '~. to ,pg~te the· f~ .P8~; 1"",11

1+2=3:

1+2+3.,,6

1 '*' 2+3+4 -1:0

i ... 2:+ a ... 4 ... i+ 6",'1 ",&+8-t iQ!I!;·g

];8,. Wri;t8,_ ~m c ... + to, ~pt .~ dJaig. ~1).9) and )R'mt .n plLAibll!: ~binMioM tnm t.hen d;iai" ,(For ,aornple" if &he three .,ta. are 2'. a and I then. an poI8il1Le ,cmnbiDlllmma, an

281,213. 82tij. 3~2. S2.3, lind ID).' . ,

18. Wn.te:lt·~·to ~t; thenwnbof ·of' ~I ,of po!litiwtl' ~lUn~ ntPUve puml:wn'8 aD .• uroelD. iii ,dNa,m ,01" dab.t.erm:inated.by lOme apecific valQe.

Wnt8 .' .. ' .":'. .. ~ ... ~4.. ;'. . ,n;iiiip" ... \,· . . ... ',; ....,,:~:~ ....... : •. , .:b~i' :. . - -: 100' , l'N'>IlI ~ ~" i:)...j:j~""""_

-- -- .. ~"'" U'II_nII11U1 _ '~.J--tS'U1'UD ~~loQm.~.~ -- --. \n. ~-:.J-.",

reaD 'triple:1. il a aet .Qf three· mie,p:rI :5. j. It. .nchQ1at.i.l! ... jj1 =11.:1).

20.

Iii. Wri,H: a CH ~ to, ~lb aum of:limt :nnat.unlnumben,. II. W.rit.e. e:... ~ togenera.tethe foMowiIlgp;YJ'liDlid oJ ~bI!; 1

2:32

IUI,5 01 3

4,1567614

15 67 89·87165 6'18'90. 10,98 7.

18910 it :U,ia; 1'0 98 '1 8:9012S"'G4iS:nOS8 9012S~&6r61632109 0123456~8987654a210

_ Ua ~ toop.,(or JM'.nrwni.d'ge'n.rn:uion.

11., Write. P'f'QII'Ul t,o'lIWD the aeriee r • :1, ~. '" rot .E" , ~ T ZD .•

SL. Wr:l:'t.e an. inftni.te lDop m O++I~ •. wll:idl,lbwI, not '!.1M' a,n,..~Q1' ~t. II. Wil b foUowirq~. eDOOtI!I.~ '1 D:fDIK,. .ltatethereaaon(l}.

, (9~BDR[_IO(IO)

t Inol tl.de·<'iostrl!liam" h> vQoid m;;lJ~ 'i )

1nt: x,. sum-O; cin«.o;

Copyriphted materia'

COMPUTER. SOIENC~XII

fDr(.~LiXClOO.x.~2) j[ f ;l(t:2 ..... 01

S!iJ!rn+=~.~

·c,o~·t»·" :!>umJ"'·"» S'iJim,~

26. Write a Ct+ ~am to print IlOJ\!Unber iH ~. om!er, fl:.g •• 485 I!Jhould be prinred.!t~ 534.

27. Wri.teo:C++ pmgntmWl'ms: loopaand. switch :statement topl"int word 'eq.u:ivaJienrtaf a nltlllber of 1 . ..:3 digt,t8j. e.;g;,365 &bawd be printed as--three hundred. and. sl~5¥e ..

28. Ullie cfsoto statement i8ootlld. be£Vlli.ded. 'M a C~ program, Why? :11:9'. Wdte a 0++ progr.am to check. a, nJu.m~l'ror .peri'ect.n:WIlber.

_I A 11:'" - 1.-1' •. - -. - ~,. #!··l - : ....... l. - ,~1.~., -'-'IM . -f-"II' ~ - -- .. - - ,.. ~:-f --'. -. ,. -.,1. . d-·"·· ~ - -.... -. l·J;~ .. -- -~-~~lH"""" "1 ~ eq_1 H'ReO'!!~-.'-· (I~3 Jl~(U~~II, ~,!I'! •• ~.dl WU-O!1l'en'_U"_'~

- the· number I_It For ,emmp.ltt 28· "". 1 "'.2 ... 4 +7 ... 14.

30'. Wdte a 0++ lu·ogr.l1m to ~mte tltlpattern .. for example, for n.= 4 the ,pa~Ym :aboUild. be. 88 given helQI!W:

4

4134

4:(!Ul.S4

4~ 2:12 a,~4 43:2,3·4

.. ~ ii I

41301 -1

- ..

I". _',

1

121 12:321

J!:2 3· "·,In:i 1234154321

:1. 23,4 6, 6,5 418 2 1

1. '2 3,46 6' '6.:54 3 2: 1

1 2 :3 4:15 6 'l 8· 'HHii 4 :3 2: 1

; .

1:.15. ICONS OLE INp:UT/OIUTPUT OPERATIONS

Tn C'H~ ~t!~ry pr:r.wram accepts lOme' iI4~"p~s$es .~ ·dalir. ar~d g;~Mrote3 tM ·out;put .. So we must :kn.ow about·mpu,tof data aD.dPro.por~ presentation oithe ·res.ut6. We lnlJve ~pi.D. and oovtvrith the >:>ond« operatoR! fQriD~t ilUlid 'QQltputree.pecti·"nlly; alrJo' knOwn,.~tormlltted. IfOoper.Q,tws .. These are not character 1/0 11:tRctions. We can ,oo:ptrol tIl~ output as pe;ro~

rnqu:iremell\~~vari9W1i I/O &uG~io:n8IUld i?pem,ooos available in 0+",.: . . "

. - J" r '! ,-_ _., . _ _ - _ _ _, ~ _ _ _ _ _ _, •

The 110 op.enlitiong. wii,th ~!le '~~00am ,~a II1Oniwil'Bnd. the di&k. files &rehandled

~ ~ .. apd; ake~~ cl.-~ 10 C'+~ .. Obj~t-oriell!ted in.p~t oUtput. 'Qperatinns are· SUp" port.ed.·bY·,Bucam. classes: {to be weCUS81ed, lEi;OOr on).

C+ ... Stream.

A>!lt~Q:m t". a:,~,!'Ir:oo 01" ~ilWtionfor oolwcticm. o.fcMrocte,~. TIul'8t! are o.ftW() tyJ)e!S :

(i) O~p-ut ,trog~ '(ii) I1J:put·,,~r:ea~

(1),Output __ m

It aliaWIJ u.s to .write (H' store chamctera .. (i~) InputStraam

n ,allowB .us· to tea:cl orletch CMtact~t'S.

ITogramscan. u5e,botu. th.S8@: smMfis:. 1iJe following ~ ilI~raas It !

Extmdion

Input [Device (Key bou'di CHi' ather . :smg9d~rca),

I""'=': '-,. - . ~ .., . .-,.~

.(.Sc~1 Of other stouIge dWice}

Fla. 1.11. RepUBl!nta:tion o€.d!a;ta!lltJ:eams

The data. (input Qr Qllltput) iii! undled.mdependent. 'or the 00 devices in C++. The, .~~ ooa:ril8!l'ld. ,aereen are the derault option.~ {'Or· tbe:9~ but the :~8 ·C'$I.befe~to

other devices orfib:!~J' .ifreq~ed.., ,

For !Q(Immooieatii_ngWl,ththe 'ooE1!~lelLni,t, we mm!t include the file i08b:'eam.h 1.U!iiag the prnp1'QC(!J!IJ()'r d·ir'ectjrJe linel,ud«!. It mean!! that tee' ine~etilm. 'wiDbe earned. out m. the prep~ :a~ge ,i-,e,. before the actual com.pilat.ionofthe 1!i0'1ll1:t'! code,.

IAlwrdufUe·tdtmJld,#Jefnclu&d in U CMP~ ani, i!tJte m4C.I"CI80r"~.M}ined in.th.e

Ii« att. needed 'Q'hat.~m.

Ulnformattad Cm'ISOIe 110' Functions

The console, inputlwtpu,t. functions ea:nbe categorised as : m .singte~~cJter Etmc,ti,O:R!!I:, ~,g,,~ pkha:rnt pgt.OOarl) (ii) ,sbing timct.ionl!:. .e,g.~. pt8()tput80

These ;~dJeimed in ·the· hep(ieJ." rue tJtdir! .. h~ wiAiehl!Q!\lJ3t be ~nctu.ded in the ,~+ pro~

,gram. fOlf using these fimcu<mJ!l. . .. .

The cet.char() functi.QQ.reads a cna.roeterftomtihe k~boar,d, U takes no~eters ..

. . .

:FOf examp]e~ dl~r Chi'

t::O!;lt«;"·~p,te,r .;). cha:ract·et\.!'ii" < t:;h''''9:E!Jt~ba.~ (,) ;

. T.be abovaptogram segment helps in. readiing a eharacter' ftom the :uy~a':rd. The puteluu11 function. writesacluu'tlicter on the monitor. For ,example,.

puU::h<!i.r. (' J' ) J

Th.ea:bo·ve ;sta!tement wiU.p:rin.t the charaeie:r J on the nl,oniit.or. E'venavalues:toredinaMU- variable· C3:n. a.~Q()' be di.sp1~ed by putahart1fuJ}~on, For

example~

, .

. put,.C.: ch.,a~. (ch).: I i d~ s:plalf thE!!,:!'O! llle of ch .

• I. .,- " .' ,"

... - . ~.

",

~

.',

• 1'1 ..... ,

Copyriphted materia'

1641

II !!lll!Ft:~dt:.io:naf ~en:,ctI'I!II.:I1 tl !Uld pl1l:oh,ar (J

I t Ch~dlc <l 'O!b~~;t;iI;'~r for Mil alp'hatlQ,t~ diqi't er 8pet::Hll c.hfl];:~ei[,

*~n~ludo~io~~o~m.h~ ,!iipd\Jde)!::CQll'ltlJ..h::o • -f inc lude <:st.d:i.o . l\,.

v~J.d RIa ~n ~ ) I

cl'i~r cb.

cl ~.9: ~.l' r , ;:

cOl.!t..(.c~'Ef1't'el" a t:'~,u;,u::t.e1..-\n-. '!:h"'\J!Iltel'l~'1i:' 0 ,; '/ r enter ~ c-h.;!rJi\l~~r

if ( ~cir""!Iii& ,&10 ~1i,..,..gJ.:n ; j (d'I'::O ... 9,l 1140 c:h.'!"'l,i'l:! J

eMlt"'~"'\nj{cnJ holl'll!!!! Gflte!:e.ti aJ'l ,alph;abet\nll-.n"';' else

1ft eh>-'4. II ch(.oo.5i? )

i;:Gllt<(."\rI!,¥Oll h,evi;! ~mlllerl!d a dig.it\n\n"; eU~

cC1U1l::<<:"\:n.'tOu. have '1l'11~1!,~dl a :!,p!l!ci!l,l l!'t.ia.rdlCtl!r'n\n",

1i!e!o.ut,~t II))f:pmgm,m, 1~2i5 'wjl1 be:

Enm:r a c"ba:mcter A

You haw: &nte'r:,ed:m,ampb~t A

Enrte1"aa~,

7

¥au hav~'e:1!!.~ Iii digit '1

Enmr 8, charaeter ?

You, haw: en~ii't. 19pooiU characller ?

The geICIIOllnd _fill) fUnctlols

lie Re,tohO iIlndpu6chO ,t\l:nctrom ~ and write ,11 chamcter frotn, ,i~y,boo~ ,ana to ~ ~Wd~., We: !ca.nMt,~ diem, :nor 'the lo~ating S)\8tmn. 1'h.egeli1:bO ,function

does :not eeJ:w, the cil1:ua,ct:.er':it re001i,~ :fi'Om,Keyooari.Fnre.choiD,g tb.e: ,c:har-aJrte.rthe getcbe:O fUnetiO:IlI, sbould.beiliilRed.~g he~ps m, v:erffiica:ti.on ,0:£ en'teood d:albil. I.t is: a ,gMdi p'n:J~~ mmg fi!a:tWoE!.

'll1I '.' h(') '. d :tchl)" d 5n d "'1 . ", ~"b h d: m u " .' ~ .. jr'l" 1 '. ~ __ egetlc_'. " ·PJilLPIIII_ _ '\ ,are ·.e~_c 'ilL :!In t, e '~~_I_:,,~ _ er L_e •. '- ,lJmg ge" ~J :mp !I(lel

of getcbO so.metnnes dees not ~ ftI.e use ,of ,g p:u;oo!htO :£0'1' ec:hei!lllg the user ~npu~,

.~ in' lilt! ,need (}fp~H8ifW Bn'er ,lteywnen we: U8e Bekh,O 01" #.hO~nc.ti"n.

The fol10WiDg program. liUlH'UUiWB 'Uw: liJiA: oi'g'GoohO. getcb,e()1 lmdpg!LCmO fiUD~tl(l~s :

j i:rllcluill.!!!i·n.O,ll1: r'AIlDI. h~ ~ i rI c: lqdn ~'.U1i. io . A'

';lllOld ffla!.n 0 f

ch,<ti cin

I:L;:'"!lCX II ;;

eclJtE""''Bnt:'~r <!! cl:i<llractl;.n:\l"I'I. ch-l!J(!tc~O ~ 1'/ e'fllT,;,e.r Ol. cil,iilJ;:a~t.,e.r C:::C::lilt«"\n'fCt~ haVE! erl!rteo;ed\ .... j

;n.l I: c:tli ,[.:,in I, ~ I' d.tsplay tn~ CJh~lra~~ ... 1:'

ge t.cn '0; n l:re,e'Ze- the: rnitlJil i t.~ !imtl. 1 50ltit!l ke yo prt!:llSl!cli

The o~~t or:,~am 1.216, will be ~. En:t;er ~. cbi!U'3cre:r

j

YOll,hav'e ,enmred j

:Intbe aoo,ve pregr8lm.pteheO I'>!!OOl~s: a. (;hoo::a~nd ase.i:gmte, ill todt,wifhootp:reasm,g tbe En:ter kt!" ad. tie e'amcle~ h'i! di11!played on ~:ree,n. The puiChO foo.ction ,dis,p;I~l,lthe ,cib.M"a;citter a,g;ai_D 'or1lm.tHlitor. The getdtO fu<::tllJlill williita fu(r ._y IDey tobepressedi .. On. pr,ess!ng the 'key t'bepm,gJ"aDI.b:l:rmiiljjlate!li but,the eh~' is ~ot li&pla.;¥ed (!I.Dmonioor.

TbeimO _d. :Pg;~O fufu:~tio."

ThIEl: gekiO andpgtlO ~utH;tiOO!!! ~e U!l'edri@f :f\e,!':!!dbg a~d di:!:Ip~ayi_,gg :ltri.tl:g'll:, The le:hO geba :smug :&o:m .sWln i.e.,,, ~eyboa1'ld ,andpumO outpuba :string to stdJout i.e'.,monitor (H. appends a :n.ew Une .c\han.wted. The l!liyliJlJtml: o,fgetsOmllrll.pubO liib:rary :ftrmdJClIlIliS: :(IIl'e. ~

qets ~ arJ::_Jl<Jilae) ;

put.s (,,1:1:: 1;_I:I,<IIl1.'Iel r

For @xam:pie,

eM:!: naime ( :3.1], get:.a ~ n~e) • put.:S (n ",!tie) ~

~s fitring DamB can ~re_i'l'itiimiilItl. :SO volidi,cb.:tiimeter.!I, (>ala a.~trin:g' ~8 aiway.liI,mD!ll!i-, [!.Il!.~d hyPo DIwlmm;a!cii:e:r .~,e' .• ~\O' 'Ik.J'lO<W,I1I, as s:trin.R ~a:oo!t) .• 'The ~\:11 char8,e1;'t,1' is ,lIutoma'tl,oony plaoodat.thee.iIld.of·the :st.J:iDg 0.fi.p:fleSe-leg iearriale r,e~

68

, i nc1.ll.c.fo tlfl ill t, E ~~m • b ~ liflrc.lUlfu<c~nlt' .,:n'"

Unl:!].ud!~Qt.d.io..lf:!. ! I f~~ 9~~:!! tJ a.rrd. ptll:.:!1 (J i d;gfi.l"le' SUE -31

,,,OJ, d main ( )

[

cn2lr- n~ [SUe:h olr-gc~ I J-.

~OUt;« '" ~lH: ~T yout' rro1m~' n \1'iI "';

!Ie ~s (n".m;e'l. 1/ e-nt e'f uhe '!l t '" .l1'Cq' ~~wt(~~'nHello ! ~;

put.::II (name-j i II d,l.~pl<llY Ule' st.dn~J

The, {I\i!.tplg~nf prQg,.l'~ l,2?' will hi!!! ~ EElt~:f Y·wm'Dg_m~

I..POORV'lII. :oU:IT

!Hello!APOORVA D IXI'F

Un-fGl'li11aUed ,S.mIllDI litO Functions 'De p,tO lind IPl:!rtOfgnc~IQnt

0IIa;!3i!J cOsimam mRB, C1U!:ctio:n!li deinedro!l' {lurLp'llt lll:pemtJo[t!!!i" Mned 8trePIQiMeri~C1tn!l., Th'1l! ,op«r~,tot<:< is .ca1!I,ed, tb.ema.erie.t:. cout :is apredefioed obJ,oot aDd, is ifli5:fiOOlated withth.e Mandri IOl!ltput ,dev.ice,thB,t, .]If.Ii~Ule :screen. 'F,. examp.Le,.

iC011l:t;'':O::'''tr!'a.9t.e,:r; ing' 'C'* '+ :P~Ogll:'ii!.·IiIlI!l,\:n".

Here'~theted string to 'I:beri,g)It ohhe ~r wCll[jd be IJt(lr~d, :m,t-h~ s-trea:m obj:ect on the :Ieft. 'fbe ,<;<; (htserte:l')iaooept.a;a S,W!D.g' as an, aq:ume~,t am:! i,t: ean ,aooe:pt,aU fM.dia:m.ef:ltOO d~ta~ei!,aJlil ~~~D.tii. mV'~ilI ,void" ,~ beargmne:fil;; ·kl·~t.

'C]8;9$ .~Bm 'ib8:!lfmiletiOIll!lB de-HiDed fot mput oper8til)M~ ~aHed6tl"eam .e:rtmeUoRs.

The '~~w:r:» :is ·cnDed the~kaem:r. clo iis.ft p~ede(med object. al1!d. i:IJ ~iated.'witb.th.e IJtan,dlmlinput de .... ice~ l:b.a.t. is, tb.e )[~M!9iml. Wem.U8t BOlte th8!tthe emactm' Siklips :h~adi1'llg white ~'J~ce6 .(1 ~, ii\~I~ ~\nwhile e!rlr:8!ctinglmyil'imdaDI;I~:rw.ml diatatypea., For ex:B!mple:,

i.n't :ii, y: .elf1,»x:o,;.'y,'

'Here.tbe impl!IIlt ,dRta hy~e 'm.e'l' :9ho.uJ!d be m;o;tchimgwiith the t~ ·of,the va:riabl:oo. ThiI"l~ readiltg ·of,f/, variable .f'8 t~~ml'l:~d with ill whire ,spaoe: or a: OOl1: .. .m4tcAi. ~h(lril;ctll1r; Tl!emneti!ni!1. pu:t() ~,,~6'e:d ~ ou:tPiI!l·t. :PllliIl!!El Qfll~,t chl:U'8.cbw b:y cha:r,!BI.ete:I',. r.t iii! a me:mo. ber' ,of the elMS oat:l'eam. For e~runpJe,

!!i:;Cllllt!;, "put .. j'" .11,." ) ,r

diep.I.tb:a ic[b:a;m,ebr ,A. onthQf:I~iJ;)"

'The foW1owingsm.tem.ell:t i C'QU;;t .P·1l.1it Ud i'

dispE:Il_J'8 the 'v;ailU€l of '~~'1i,!''a:ri;fl,b:LeAhavi:nga ch!l!meter val~e. If 8i[!jl!unlb@1' is tli&!di. aaan i!i~ent. W' PilltO ,funct~nthl'!'nits ,ABiCll 'Valu . .:! 'win be di:s,,,lH1l!:I:l. F'OiJ:' exam,phi!"

~'01;lt.p'iJlt n:i:5} ~

di.sp]~stb;e lihuoowr A, ,(ABell vaIDlie of,A u:65)\

'rheilioUowingpmSl'HD. ~'gment accepts ;8. line' efted .&mn ~eybo®<ard IUId, ~p.~:aJ'9 :~It

al0.Ilg wUh then.u:mb.'3:rof eh.aracmrs ent:eredi, : :ii:i't C;OU.f1t=O;

cllill,;r: ch.i

couc<cNE~ter tbe.te~t\n~i

,

cifi.get(ch~i II~ead ~ Char~cc~r

wbile(ch !~ "nO)

+ iii"cou.t'il.t ~

C;:O~.~ .• put (c~.) i lldi.l,;plaY.;3l;he cila.t:'r!ctlil:1"Qi:! mcnU:Q;r c:!il'!. get (ell) "

cout.;«"\IrlNumber of. C:harac:i'::ers !!!:'l:I:t,e:re:a ,! "<<:elO'tlrlt.l

Tffile followinC P~Qi~Q) irumUl,!h-a.w!l, the use ~f ge~Oandpu:liO fun~l.\;j()itts ,;:

II LU.ll!l~ I:'d.t.1.ng uae of g'E!'l 0 ... 'I\t! pu;J;, IJ fUr~c:t1DI]!I ~ include l.:o\!l.1;..ream. h>

i 1f1clluir:~i;:Qtlio. til>

"_"c ici iUtLl.:!l 0

f

tnt. CI:!11J:lIC .. (J; ~.t" I:l:!.. <:Lt!l=U.

e.ULC1;.""El1.r.':Il:" 'l:lile "B::!I:~ '~ .. !S .. 1.llil"tE!.t;i ttLy ttl'rtil key : ~n~; e1.1'! • qat ,t cltJ I .{ /f<!lLa a c:b..n"o!Il:l't !!!.~

'o!hl.J.~ tol'l ! ee ''''fI# I

·~(;·bunt~

£it)U~ • PUt" ((it! I ~ 011'1.(lU ~~I'IJ ~

Th;~ '~UJtI;put ofprogr,i'ml l".2Bwill bill! : .En:oo:-r·th.e 'b@Xtterail1B.b!d. by EmERby : .MMWrita:g 0++ PHgr,fm18

,Number M mn1".D!omrs 'BDiU!red. :22

COMPUTER SmENOe;-XIl

Hem" 8iftet t~g tbe tu:t tTIe ENTER, ]le,)" is,p:Mssed t'hen. tilie' ,charactem ,Qre!read ,one ~' 'O!lll.e M.d, t:beva'fWable Ci!l,_t 'isinc~tmlellood. The staOO:m.e:n:t oout.;put('cb)~ dis.plays tDe ~xt,and.~nnirna;tiJion of loo,p' td.~sp~aoo .p. :fetehlml!!: tb,e, newline ~cter.

getllllle() alld 'Wlita() ~undlons

fiu! le:tlinsO ,rmd writeO (UilWtmn8 are used UI',ith iin.e.or,te'lded .inpu~l(JlJtpil.lt., T:he ,ptlLne() rUncli~!1!i. rea&a ]me 0;[ text termiina,ted.byEN'TER key_ ,F'Ore~p1e~

chell: ~ ~r ILU 1..

cil1.'r,;jIli'tHne (iIli::ti:~ 3:U';

Here~ a-rr' iia: _ a'P,Ia,Y ~f :size 31 and can :s,iQ!I"e,80 'cb~oors at~ :~llun[.meliil.thoenewHne, e~s:radeil" ~\ILI.~iis['@ad"it is,mpla.ood,l1y til@ ,!n.nl, clmmi.lte['~nthe: arnay. The temIiiiJU!lUon~es plaoowbenei:the:r ofthetwo!,t_t ill,! ,eiiUte:r .l1!U'ii'FE:Et ~ey' ilB:pmssed 0:[ one ch!ml!de['~e!ls tban tb.e:m~_IDU'!l!:l ~~ of:(IIfl"8!yh:8!v;ebee,n entered.

Su.ppm;e that t!he Fo.U!1Irwmg text IS enter'ed :

India 1:'11 GIIll!l. t. <pCB! a,s .E:!N'I.ER;"

1:1he tnp!Jt IS re~d, p:roperly and s'bo:t'Ied~n ~_y :9Ift'. Now5uppose til.e' Fo~I~'W'Wg k~t is 'eH;kred:

NeuCtiC'Kelt Wodd, el!Jp,~l1 'be, h.eld in 2j)0I'7<ENTE.u~>

Em.thiB C8.!!1e~the In.put ·mlI. betermmQtedBfte['roadi.mg·tilofi~t1,t 30 ~bar.a.ctGrs (i!nclud)~ ing'blaiJ'!k!l)Si~ehown. belolW' :

Ned Oockd W,Qr].d. Cup wUlbe

'The!!~g:e ~. m~(lbe r,e!ildl by 1;IL!lmg::o> operator' •. But» cam!lot .IIlCCePlt m'l1i!1luwelf-d lil~p. The; lJiUU ~barl1i.eter 1:8 ;QUijtoma~lly added i9:lt t~i9' @1l;d of the: ,clla~alCter,s :M'l'tly. FIlM' exam.ple,

a in>:> it r:r: f

The above sta:temen:t c:an reada word. 'l.1I(pto 30 c'hoo:"3Cmra aDdl that; too ,of ,800[1" le,ss c:'~\u,lI';pc:te:rB. T:be rO~]I@~,mgnmll j,Uus;ua:t.e5 the radmg Gif8m~g us~ng s:~tlineorlmcti'mIi:

/t:Ul1!l!;O'tcatil'lg" IChe ~ddLl'lq iCl !!!trinqs l;lSOiTl.'Q' qet-Unto! U .f:LncllJdl!!"'~ o~t !'@ii![!I.hi>

I i-.AC: 111 dQ '::i:.On1a ., 11 ....

ide.ti.ae S.! Z:E J.

void mEibl 0

I

~11.~i:' !l;i"'r [SUlt:j I' !!::1rsl!:;r j;

C:O'Il'l:.<'~"Efi·t~r your lliirn.~ : \n"'~ c. "":;"OU:'I<,

Ggut-=<"-Yvuz n;ua]H ~ "'!'.«.ii!~".;ot;"\DI\n"t c;-au;to( .... ~Tlter )'1lll,l1' !:t.i.end':s no;u'lle i \!1"~ cin.,e~linB(a£~~ SlZEJ; caut,<''':;_'',~.t"i!!!!lQ·.$; n~mt!! ~ "<<-!1l~<""r:I\o\"'~ ~1l~"''Il:''''E!lt_e~ ¥olllr pc.Cl-fessimf :, W"; cia _ ;;net: H.ne {a:r;&:. S !-l:E} ;

ccnu::.;::C,"'Youz ~:rofe5~icn ~ "«a!;rt'«"n\n\".

99

Tils! G'U'tpat ,of'~ 1.29 wiD 'b,e' : :ERrer Y'QM:g,m,e ~

POOJ.A

Y(rl}!!."~' ~POOJA Entem:yolUl' .mend~BnBme :

FrieWlldl.',J DlmIe ~

Enta' your professiol:1! :

ModeliL1rng

YOUprofe.8BWn. ::MOOe:JlIDg

Enter )"(Irnr :paIllJe:

POOJA..MONGlA

YO'l.tf na.me: :rOOJA. lEiIlihrJOW' :rrien(Pa~a:me; Frie:nd~ifli nam,e : MONG:IA EmhH' )l0W' pmf@Mio:mll :

Acting &; :M!ooelling

YOurPmreM~on ~ Acting&: ModeIling

Here'l for' thei~.trunthe fund-im:!. leWneO dnei9 Dlot 8.Elk£tlr t'be Pro_Pit '"ED,ter .}fOUl" friend"s nam~ f.BS t~@; IWwline ~ha:raete:r (1\Di')' ,aFmr POOJA.mma:msm tliB'Wpt ',eue.. So the ~\~jis :tlNld .• ru1 ,eMpty q1.!l!e!JI:e. Forlt:he ~aiNI!:~the w,om '''YONG'IA'' is readl b:y the ledHteo.fim.clioD. wiilto,uttne prompt, ~tet.yotH'mend~8 name ~I!!' I' ultis motmadi, '~me em, sta~t. g,ut the ~t ":Acting _MOOre,Tine 'isproper-b' fe~dQ' the! ~d getUne(~, SIZE); statem~n,t. See. the outpu.t iim.cem.ore&o:tba'riqa. idH ~t toe,WQtmnl ofptJ)iD,e{) fimct~,on.

Tih,QQIiI:w H the, writeO:I:tI!D.etiioll1l, ls giwn below ~ COIUit .~rU:Eda,:n,:" SUl,IU;:

HeR'. ,&.'iT repR!Sientsthe nwne o:[,iI,tring w 'be dmpliBJeU ad!. SIZE tJheR'llmhii:r 'Or,cli8.'1:"~ ftctel'8~(1 be dli~pta,ed. In,this ~~ the chDnleku after :DUU .are also displayed :iif the S[ZE:ii/J greater tihtm, the l_g:th of the Wxt 'olmm.

We am. (lomhm.e: ~,'ID'i;teO 'fun~~er forprmtm:gtwa"strinpm tbsiume line Q shown. belo,w::

Cl<CIllit '. 'w.~itE!! 't,llr.:!;'!., S IZEll ~ • 'W1!:'it~. '~a,~ .!!:2"S!Z,B2)

~e ~lllsJwk!m~ntp;ri.n,w.lttt.'l.. _.d,a;u2 .in,·Mle sam,e! Hiee" 'DI.ElI following p:ngram :il:1D'" trateeihew,ol'kir:l;s>ofwriteOfim>eUon.:

I Ul!i~p1.,.~! .!:tr h.q;a \l.:>llI9 w:dte 0 Hl'lellJd~:l:,CI:!IJr..re.!UIII. :hi'> ,t.i:I1Clll,.l,d,e<!!enin. b)

UIIl,I::i:'l'tLde<st.,d I1i1j!! • h~ I i fc J: a.ttl~n 0 Itli:liJd mon.n { }

i ~'!: J,e'l'Ifl ~ lm12 r slr&sr p ~

C~:!. 0!i:fi:41 J = "!M'o1!diil1:eii;J.OIg,l;; e:~Br G't"J:2n. " c++ PtDll;l::t:dal:S"'; len] • 8~ lenl~r~1)f

J.e.n2 ~t..r Ie fI I aJ:J:.'::li ~ ; Cl(Hl't~<~··!:t..:rin9!1 i;J : 'n'i'; c~>!!t..~lct:i:t,!i:' ~eJ~tlj' lei'll' ~ ~fiut:~<"\n\n,e:r..tiiJ:g :2 .!,~ ~. \;,t" ~ c::0ut. 'II!I.li:i.te ~al:l:'.!~ l.ati2} ~ IlttlnC!aItI!Ai!lt.'l4U!I of :!H;l:i~:!:

eQ1;ll «" \n\DC.o'll~,&t:~n;Jt.~ sit.:rd .. niq _L~ : 'I m." ~ CQ\li~. ",~1;,l::e ~>Ii!~t"l" l'iml) .w;t':.t.~ ~<'I;;.2, leli12 ~ ;'

The O!.ll.tplJ1t:.o:fpro,gr'-aa 1l.30wm be ; ,striIlgl:iJs:

,M!I1lis:te:ring

S:tring2: [i:Bi

Cf;,. Pmgl'8ma

oQ~peate:Qated !String is! :;

:Mast:e:nng C++ ~tu'Il;8

Here~ tbfillet{i!,~~ ~~intheahl!!l'~~Q~ 'Clout .w.r:1te, {"ltrl,r ]l,en.JD .write, l~lIlr!:'2t 1!en2.' ; 'C>IIlIlalsQl1M wriiltbm as giivenbe]!ow; ~Qut.write~arrl.lenll'

C!O\.M: .• w:l:lte, {aJl;",!:'2.,lel'l:i! ~;

Formatted Can.Dla 110' Ope,ratlons '1ih.9p:rlntrO ,and Kanl1n functlOlll'S

The he~det" :file IStdiOl..b prolVido.stnege t'l!V(l' funl@'~j(ln:Gl .~ p~rlOml!img '(j!J~p:u:t m'lldJ :input.. ~'BiQ acc~ptj a :fonnat sp(!rifWati:o~strifl;grulida lis:t; of variahle!!l~,p:~e~r1!', The f(l~a:t s,p<t'!cifiC01l.tio:~. string sp.ooifies ·the data~ype (If ea:cbvaJri:!!!ble to m!p~t OT 'Otii1t,poU.'t.

pnn'lfO fil.:m6tion

.l~ isuO!!ed(o'r Ih~tt'ed ,ou~.put, to :5iGn;d'arn Qu:tput de:ulr:e: that isfoom:reen. Thesyuw of,priutfi() [ull:ct.io.rn. iB given ool'aW' ~

pdtltf ~''''format spee±.ficat,iofl std:n;I;)N., list ,o.f va:dac'les}, m.ere~b~rni;8t ,ufvariahlle5 is .mmaseparii'i.'beiI,and the tlb:rm.a:t 8pecifi,c$:t~on, str.~g COn,~ min!lit foUowed.by ,8. 'coillv.er.siO!ll. chara;c,'i;e:r.F:Ot uample"

:Lilt: x • 510:;

p:r.:L.ntf ~'''''d'''. :!:) ~

T,mslim.ment, wilim 'print 50 ~n tn.e stree:mI.

f

The :rtlUo\ri_Bg ch8f'actera ue a~I9>ii:i! ,~..a : \n- :newli_n,ea:1'!.d !C~m~p:ootU'F,i:'Ii \t - mght, a,paceSi

The. ftlI1Qwi!i],g~Qgram m~tl"8:te!ltbe '~ap ,M pri!i'!i!ti'() for forma:t<t.edl oU!tp:~t offunda.nlIental d:at!l:. t~!ili ~

Illll.i:l'-t:r~'f.:!Jtq U3o!ig:J.! Oif ~diil'lU (I .. or tGp:il:Lt~>e-d Q"-I~

• i.flcll1il::r~ i("J!I'tt3r!.o • b> i ...,Q'1.ude,::ccIiIJ.o. h> >r~!:d Uta iIil D

ihlt;)Ii ~·o.;

flQ~i:. 'f - 1:S. 15. ~f:ta'l"' c:h. = oJ'; !:h~c,"O 1

]'It' .i.n"l". n "li! t't:l.\I1- ~ x) ~

p:arln'l:.f r'~f -1[\1'1"", ~~;

prln't;.f ~ "coO 'c::\o"'. c:b~ t

T1he ·Ol!!Jlil:.pUt. o:fp:oogram, un wm 'be, : x:~. 510

y ~. 15.75:0000 l~h=J

We: 'cm elM pnnta data ~ as' ,1m>Q.thel'l!IiSii:~g the oo(Q''OI'er1!i!~{)n charade!!" •.

Tile. (-OUowingprngram. ,!!iMWI!l it ; .

~!inctl.ldil(.3,tdr:li.D. ]]> !lold [[" .... n n

[

il1t. x: - 9'1;

p.r!t'i~f'{"K - '~~n";'!I!:) ,;

p,d"nt..f ("ehil.lI:ac',"-er '!lquivnltl'l'It ri "lid 11) "b\n", ~,J! ~ ~

'The ollltpU!toiflProg;t'PD. UU!: ''!!riD he; ::t.!"':::9i7

C'lh~ber eq!ili,va1iimt o:f:9\i' ma scan~O IUnctlon

l~ is, used: for f~l"mo.tted .i .. u~ ,(mrn, 8fiQ:rWo:ffl ,inp,rd@PJ'ice thilt i;lli~boord. It p~Qviie~ DlD:7 0:£ the, coiDrve:mi~nre.ll!tW'\eE!, Bvailable fOl'the :prmUUmcti!J.D .. 'TIll,e, tyntax .of the IElcm:fO function, i~ gW.ven.be:Wo,w:

:5 ca ~f (OJ. fQ:rmi<!!t .9pec:i.f;i ca:t.iQfI !;It r ing" ~ ]: i.:st 'of O!!dd]';e<:!Lse:s Q fva.:d a:ble!il,) i' 'Wll,e:re ,Us~of'a;ddreSiSeJj of variab~,es is oomma aepar,!i(:oodand tne format s~cat!on string oontaiiM 1Jl, {bUow·ed, 'by ft, ,oonv,e.rs~on ,ctlm~r'.

Far 9IlI1lp[e,. cha,r ch~ int x~: fl.;').ai:: y;

5caa:lf ,I,""'c:ld,i.f';,o", ,lieb" &':11, ~l') ~

He:re,&{M!1Ipemooi) 8i'~ be£are v~ell!Une~. 'In. ease. of 6trin:gvariftible6,~e vw:isble 'nam.e lSfiGt preeeded!b,the &. 'Tle ,SC(jnf()fH~ WJ'lrtOt accept ,m,uinwoN stNn,g8 i'li8J it ~top8 ~troctUW0e C&~M~fO&wbn ,it £nooW'\tters a,~plUle.

1,,,116 .. MANIIPULAl"O,R8,

'Thele ~' operot'Or:8used with tie, uU:e'.rtiiO.n- @pernt'Or ,<:<f.o ~odif.tIOJ·I'l1~iplulate the o'lltp~t,., Two ~ lll.:IllliPSlWrs 8ftl' gi,venbe~o"w:

The' enell ManfJldlfo,'

l~: iB· a, ~mp~lgt:Q.r toot i~ml'd8i a U~~ed' ,im.., the .o.'lll'ie'llnl. U bas t:h:e SII:Ilfle effect· Bsthe Dew'liinc ,!':ha:mct.er (~d). F,o:r example,

'Colut<::<eJltU ~

The ahve st.at:".emi!D~·~ ·the etmtntllkl' ·fttenext line.

Tha,S8tW'llillli1lpUlator'

It ;!!ita nIa;mipulQ:lor tJi:at ¥eci~' .the width IrJr .th~ ~ori(:Jb~'.9' Mper 0:1.1:1" !!!ho!i~. 'The file ioman~Jlm_t,beindlllied. 1m-tit in. tbeiPro,~- [t uu.sesitil.e :DililDbet ~m" string) to be prin.red with'inthe speci6edwidlth .. For ~~~~

in!:: x.~

c:o:Uit.<<:s'etw'IJO) <<::X:f

'The aOOve stat:e1D~t ~JtIi!'lhe 'Val~ of:x iiD witUi.or]'() :gd.rign.t-justifi.t!sthe value.

Tae foUGwiDgpmgram.:ilJus!tmitfi tms ~.:

Ilnl'lll.~:rIilr.,icf:l ~f fe:mtalt.tiflg wi'ttl se't';<l U I iJ'U:! V.:!.de,<. l'll ~U!f! Elun. l!I>

"t~~l!Jtle<ctl,fi:l.o .h~

l±.l1,;:l11100,dGI:II:<rni .. fi!. b.:> I I h.r :!ict'~ 0

void ma.b, ~ ~

int 1'I,!i'.;I>; (:1 %'ser Il "

JCrout.O:;O;:"Enter <:.hwe Lll'i:eq,i;!I:l(li \I1''';' irr:1:lIlI»:X> "'Y '"~:ll f

"p,.I:'::imtth!l! 'l.l:alul1! wiLt i!l.:'tN~1

.~Ui;l"~<':::" \in \.n'T.:tIC! oll'['oJ!:"Enl inl:..egiOr-sp."i n~!i!~ !,I~i 11g ae't'll!l" U O!.c:e':\~ \:n":

CQl.lt<""s:~t;ol nD) <",")!"<:o::~(!tw t HI ~ «"'y'''","cJ,:'~~w UQ I o;<N'~'·(. .. el:ildl«Bfld!; CQut:«~et.ilil (I D, ';;'<XO::';S!!t.'R (1 D ~,,~<.:::secW' ~ 10) <::'!:.~"

'Thel)~t'll1m. ~f prtlF8iM. 1.:BSwill be:

Ent.ert.hroomtegElW 1024 "SIS SaG

Tlie @nten'!dJmte.g@ifS!print.ed~g s.etwOat'e'

x Y' z

1024: ~aalla. ass

1 .. 1i7'. ,ARRAYS

An (Jl'ffl1 i~ (1. f!,otl€Ctt(JQ of iom:ogen.ooui!I (:l(IIlne' :typeJ elements that ,g~' r~fe,.nd ,by Q CQ'mman name" It :jsalso e:aUm ,a, BU!hK.riptea.varia'le D, t'hemT;~Y e~em:eli:!lbare G!sedi, 'by tne' name of am, arn:ay ud, an ind.ex: or :!!iliIseri~t. An'a:.ys Me' (If'~ 'typea, :

(i) OM·dfmensioMl !urmy

(ill Multi.dime",sio.nal army" a" ,or more)

C.il O.ne-clilllen.lanal Anq

'The syntax o-f,dedanng.8!1in;gle"dime.na~on.al ,a:r:r8lym c++ is, aa gi:ve:nlbeloW":: t:~pe ar!i:"~IlI!1!i'Ue: [a l~e] J

He.f\e type d!eti~.arre8ifue 'bue 'lJpe of the orrq, wmfhis the t)rpel m,esch. e~t.o:fth€i ~tIfy, 'The IPT..;n:pm:11l ~pecifiee!he! an-oy namebywi,th t~e 'lU'm:ywiU be, nrl'e~m:edandi Idze, ,define a ·the :DJlillmhe:r ofeleme:iHllt8 thearr.ay wiln :skiN. F., e:ample'j

int a. [5]!f

In 0+* the :~y fudeJ: .. alwS:YlJ3:be,gio(l wru,th. 0.. &,.al2.lli woud. re£er 'iio tll.e ·thm:i.e.lem.ent in Ute ,Ml'ay9. where 2M the arr,ay md.ex i();f' :subser:iipt. 'The ,e:lliitire .an,a, ~anbe :sll.OIW!]I, a8 ~

, O.'J .ir{ ·.~Ia!ta]1 a(41

I

9i 5:5,

t I 1 ~9 ~

~ameQf -

too !a~ay am¥ inOO~

~il'. 1.11. &he:m.atic re,reaenJttitton 'Ol·IIB .!In'ay ,gIs']

For ~a.diifl!g· the elemeL'1!lb (mpu!t oper.gJti!o:~) il1l.the m:rlilY t!lSe •. ~ CIQUQ!w.ii!Dg· method: 1m al[5,], ,:I.;

for h.O'~ iO::S. ,i'H) c:.in>::>,a [ 1 ] ;

14

F0l'writmlt'l:Ui!,e:~e.elll.ts (o,u.qJultope:r,atiion",Fl"om the ,arrayU!6e the friUow:in,gmeth0<d: :Eor(:11"'(h~ 1<5; H·+)

CIClut:«:a ( 1.J <<:' '~'

A. v(l'.di~rffis all'ull.tbe~ailca] '~e~ used rl;ll:'tbe wUe·~ti(ll:nllirnumbe~ whicb ,!Llre =m;ll()gQiLlI~ ~.e'i' Olllie m:m:Bnsi'llloal mil1llear~ array. SQ. in C++ a y,eii:tot 'can ]:">e:p;re~l'E~:rtt onb' illThtOOs:er.s ~iWml :Roa:ta. l~g' point numhers,

,In me~! .tJ~t! a.~J naime ne.fer&' ,to tIle S;~t1lr1i1~ ll~iiUi"J!i or the. ~d4ro/j~ 'r:J/ afir.: ~n:;"C!. l~hi(h r,~rc8 iiIc¥l~tm' fol" gto~ ')1 ~1i!l'i!?H~illt\\l of!~ ~J"1'~li'.

The fo~lO\mgJ:r:\O~mfinds 'thfil nV'~l',l1!le maleand fe~ale .he[ght 'ua:'lng tI,"ay~ ~

_illLclude "lo;n.:re.:lm.!I> IH[i!clude<;c;eni0 _ h> ~d~fi.n(! SUE. l[j

vOld m&i 11 Il I

UO!!.'" :maltl'h:t rSlZEJ ~ [emalBi!lt. r iIZ~l. t.!il'''!Il;a~ei'lG tt" 1;;1'-, t.et, ii.emalliitn;; Q .Il, tnt. Gexcm::le. rr.G'~tlht.~O; fcc~I:.-'f!,

eJlr~f t) i

CQut<~"\n!of.rg1im~m "~<5!~i:"~'" $ll\!!> a..md t~1.a1s (eillc:hl lD:J::E! allGwllld\.:I!", CQut;<:I:''' \nE,nlJ;tu:: (I HBi :i'OI('Codo fbo!' ·i;:OUl\c1na.Ii;.'10fl\·O~'

(lOIlt;<:;"''' \U'l!£ntrl!~ IOiBl'ID:lPi,t'. ~ 1 ~ fOil: male , OF. 2' t tllll'l' fru:llaie ) ~ "; Cl._n>!>~mtcQd£l :

~bil~~~e~Qode 1= OJ

[

if (~el(C!Dde =.'" 1) I

cO'ut<<:!" \n.&nte~' l\l1ale hE!1 qM; \11". clJII;>;'mOll.!!ht. i m~Q!1.nt: l.

tlOl~Jll;::.l~ht +- naleht llfieQl.lflt.l. rnCOIJII11;.~+'

i r (~tl'I':Cfl!d,'f! 2 I

f

cqut.«'" \~E.I'I",el; f'!JmalEl heiwht \(1". I:;'im~>~!)tal,eb:t r fc'O,u'It 1 ; ottomillaoltt+· feltlCllelnt LfCaui1t.J.~ f'COUr.Lt++~

cl n.el' I ~ ~

ee u't;:<:.;:" \ liEf! t'e'!.: seKCpd.e ct~;:.;;:-sexCcd~J

~i' • •

Copyr ghred ma'mial

15

if (m1:lQlclfln:a~ ;1/ if lI'!al~ c:and.id:ate (s) exiM

'eCM.u::<"of\ n "ve~a!!i" male helght. '" !".;.,<:t.I:I;:]l)tll~l1t.htl.~,gunt " ... en(11 ~ 'B1B~

calJ.~'''''''''\Ill4'a mOlle, ealld±d.at.e\n",

'Ct:lut ..... "'\.nAver.a:qe f:ClIIal.~ hEIgliilt. e.l:ge

~Oul:.<o!"\llNo fem8<1e C>anrlid.a.ta\n"';, "l~t.~1'! f) ~ II ~rl!j M@ t ~ lfio~i t. etc

Tih~: Ou:tJl~lt o:fpNgram 1.84 wiUbe ;'

M:rOOimum llOmailllee: M],d, females (eecb) SiR' alJ]OWM E\flJlter (II Pili iSeJ(J:l(ldef~~el'minatiion

Ei(lfmr Bexcod!e : ][ ( fO'fmale) Gol' :2 (roil' f't1i1i!l!a~e > ,~ t EnU!;t maleheigbt

180

E:!1!~~ iS~:eQde ~ 1,

EitliWf. mru(!,h~i:gbt ]'74

:i1i1,nter IUlX:ctIGie : 2 En;t,er.f'em.ale: height ]lISO

Elwmr :se.xc:ode : 1 Enihllrmal@h@igb.t 1.84

• ,~ '" ... !roo •

. . ~

, r-

E:!!Wte~ t:!e::tCode : 2 Elr'Ji,te!l'r~~.e he~ght ll40

E:rnrE!r' lSextode ; 0

A:¥e:ra:ge maleheigbt =: 1.'79.83332:8 AV.;lrager~mll'le hei,gh't '.., um

TIDe aM-vIe pr'OgI"aJ:,i:s self' e~pla:n!ilklry,

:Stllings

Tihfiwoillt common use of o!Ileh·d~m.enJ;!~oMl,an'ay.!! in CNilS tiIJI,est~g,. n is defined as (I cA(1,rnc~.,. Qrr~y befng t'e'rm.fnfied by g NULL ChfUQicler i.e.. '\0' .. Sothe~radera.n"~8 must be decWa;r;ed 'ODe' ,cba:ru&er 10000gel'tI.;l!Jntbe ,size, ~f the skiiLg yo~ wish to, :store. The lutbyte sW:re.a the str.ing teTllliiJ'k:ahr "0".

.For ,gx.amp~e. :i1f' allil.ar.r.ay name; iB ·~.m .s:~I1[)e a. :2i1 'I!'Rfac~e:r stti.rni11 the ded:a.ratij,~!!l ~ig,6,tbe:

char !"IOlirriE!!,[ 21) ~

1'he:iD!.Ivid!~a~ chamcrers o:fth.e strine ·areaooe:S:Siedi using a ~~~script. Tbe i~liJId of the Istring COOl be chec~.edb.,y e@i!D,p:a:rillg'he clu~f'ac'k::r'W:[th NULL eharac~r.

l~nlljJallizling Strillngs

A .1i1~ri. (:I(In, .be i"#iCfli~;Cidr.»hit~ d(iJ,c;l~l'i'N1 it~ ,~ tJpetlity,i'W' t'ulue affifJme Oir oJl 'Of';~t8' de·men,ts.'Fh~s~spo.ssiiblle ,orJ!~ya~t;side a flJl1ct~on. 'We can. d.ir,ectJyin:iiUaUmthe array:iln .~ W\U(Y'S i

tba:fnamtdSm ~~!A\','" i,O!'i' ·'0'. !If', \iI" ia'."I\O'];

Of:' dla:r 'na:mei[am = r:A(tOO'n'a'~

W"hen in:df;ti~dl\lQ:~' anG)' eleo.i'nC)!nt'll' are u,uJtilea then ~i:te tel":mi.ua~~J'" m,~t ~ .~pUdtly spe,ej. ~d.B,f,j~ i~;th,e !CiQ$~ (J/8trinB ~$~me~til the .~\l':nd~gtQr ifl· "f.!;tiQc~tJea:llJ' attacked.

The f,0,loMngpoogrMII.:irn1:us.tra,tes tbe aOOiVe comrucept:

II ~ lllllU: rEI t, ion D1: srnf i:.n~ 1: 1.o!!J..l :<;at:.i 01'11 duri.:rJ,~' d,ecrln !:,at :!rom

int:ll.!lde<:;iO'$'t.:c'l3~mAt";lo i lllLl'llU ~@'fi'CClJ'i,i CI • h;l!

cha~ bCD~_n<liitie t4 OJ ~"IiMa.li~aSl;IlIj!::"+ !'IrograIlp.l by J •. iL !h.:'I.i_E"'} ~

\l Itl maa 0

l

!'::lEsc:r ~l'}

ee IlJt <0:-'" T hop!" ~,;o\.l 'h iii liP !;til J eyed I ... '!; ... l.UlIOiol:....J'IPlJI'I:! o:::<e.'rr(ft ~

Thel 0iUDpU.t «!f :p!rogrnm 1.35, ·wit be ~

Ilbope .Y<I:ub~ve e!J.lji!)y;ed ~ M~teri~g C!-~. Ptl~pam.!Sby J.D. Dait

.A.r-ru__y booku't!i!.m~ ifi· ~l!J!"£d be,(o.rn ~ fU!1diQ,~ main, O.

us '~lke one more e'xmnplle 10:f' stmg' m8i;}ipullat:i~,n fot', cam,euWatm:g 'ti"ite,fteg;1I.Ient:y of aU e~ara,;teitlli :

i IC41t.·1n~a:..e tbe hequerLc, 0:£ ~!l c-i:h5!:"H:::ters, in. Str-!ij'1Ij ~ln~lY~<lo~~£eam.h~

~incl~de~~o~io.h~

void maLin 11

Il:tliclf l1[lOl, (In.

i nt: i..]I, ccrtl''Il~, oeCt! f' ~d, l .. an~ (I., @,'I::a1 .. 'I)i elrscJ:'~ ~ ;

C.OUt.;;<"'\o!::nt:~~ t.J'ilJG !!itre.,.1Il'I 91' cllillt:.;lLCtet'~ 1;eJ;Jltinateti by \~E~1'Eil\""ktly,: \11r" ~ ch='!Jjot:l:lh" I I ;

whU~lchl"~ \,r'1

Co yr ght rn ria

;J, ll~n-++J"'~; efl"'9 e~ CrH~' ,( I •

cou,!: o:!.<'''' \ 11 I nlPutted: !Jt:JJ:ca.m 0;1: ch~~aCit.n:s h ,: \ fI"; fo:.:u=d; Hl.e_f'l~ Ht),

:;lCIlJii'r.':::"'.1i ll.l i

c~u~<c~\n\n'~··~*~·'~·~· OUT~07 ~~~·~·~~~····'nu~

cout.«"'\nlct:-lil cli1!l.l!:'!:I,CI:B!!':!!i 1!:1pll'l:.tl!d : .. "<1 I!!JlI.';

C,",llM;.-;t~"'t,c:b>l!!J:~!l+t~r f'.!I:"-eqJ.!el1~V\I'!' fI".

fol!:' I i.-~J .1<l>t:.m,i.i1 -I-~

oecld're!lf-1} ; c'oul'lt-l,

liehe~k.:!:.:n ~hB a.tl!:'OJ,_.,Y w!'l!l;!ther the c:h_!I._r-ill:li:e~ ~rl.1!I ;!!l1!'@_lIdjl bl!!'lilrl ~oQlntell, Ilfrolrl :I;r:Jif! C'liU!'l:i!f:lt: _pt],:ll;.jrti{l;rnJ 1ii.l!:Jl:flC fi:r,~t in 'l!'ilVcrSl(!' di.!t:!'Cl.It.1Lon f~r~l-1-1Ij~-O,j--'

t

.d ~a [jl """a.!:i.] I

f oceur~d 1. II~t @c!l'uE',.m"-l :i f c'brmractll!r' f-Dun.d, ~~eiiill!i;i

t:ot<ll-"-+;

.' Icount f.,g~ thi!l: elrla.["<'Ict'Bc Ln he fDC'Wd:nl !i1l,irecLJlon

f0f tj""iU. 1'<len" j""t"'~

~

H(lll ~ jJ ~,!I,fln {

COU[l~f ~; 't@t,ijI,l·+J:

"«cd [! J «"

if 1'~oi:.fll.-llinJ I/it. f,lll C'h.a tioilC't:01;,1;! 11'U'iI'IF:1 bn;IHII counted bxeaJ1..

1

l;J~ch" ~ Ii hee~e tl7l~ mon.t-t:ollJO

78

'The ()ll'~pU.t o(pPgraDl U)6 wIn be :

Ente,rtha atrellim. of chara~ termin8!ltedby "E1fiER" key ~ CompuWl'scienC(l 0++

Inputted 8t.,eam of cbaracl:e~ ill ; Comp~te.r science C++

Total c:fia!tamn: iinputwd :: 2G Chanu::/er .Freq:u.enc;y

C 2

0 1
m 1
p 1
II 1
t 1
·e 8:
r 1
2
S 1.
e 2
i 1
i!l 1
-+ 2; 'I.U) TwoDI.n .• ,,"logll Arr~

AtwQ dimenBiGrlal an-ay :i~a grid havingrow.s and. ,cohxrnnlil:in whi.ch e!llcih, ,e~ement Is :apecmed. b:y t1wo s:ubscri.p,ts. [;t .. is thesim:plest of multi-di:mens~onala.J'raY8:' .Ftor· exam,le!

Anatt~y .[m] [wi is an _ !by D. tabJ.cbaving m t(JiWS and.n oolu.m.ns (lonbbliillg m ;<0 eleme:ntiB. !be .!lil!ic, o:fthe onay(tota~Dumber of elements)i8 obtained !by ,ooJoo1ating mx n ..

(I

1

- . . . .. -
I I
I I
: I I
, I . aIll] 0]1
I I
I I I I
I
i I I I
, I , I
, , I Os;i::;,m-l os;, Js; t1I, -·1

1

m-2

rn-11

Copyriphted materia'

791

:mX.ill.

Th~ syn:b:li:. ,of ded~a 'tW'o"'di'wH~~s~O:fI!al iai:r.raJ3in C++ is ·QrbUI!IW~:

type vilir.i.,iilbl'!iLJI<lime (!:I,umber of :t:ows,] (rnl!tl'!ber of It;;o,lll!mn.s]:; F0:r exam,plet int.a, [S]I [5]1.

Hem ·,9,· :]5 tile' :n.am,e o:fthe auay ~n1Pe in't o:f l!ize,Srby ,5" T,he array e\lemenm ate : a ~IJ]~QI]. 9~O][1')j, .""". ~ a[4][4l,

T,ibe' f'O)]jnv.rmg examp,le .il~Wiltraites tbe €Io.1iI:CeiPt o:ftwo·dlUnemional 8I1"l"~y ~ l'nctrOfi:Bp&Bo (lifo, matrix. fa .obtaioed! b,mwrcbmrgm:g t!iiUI!roW:B end ,t:(Jl'lJ.i!Biu.

I.lYta:n:!lip~e of a ~t.dx I'·i<nc! 1.!1 de o::iO'$'t r;ei1Ii1. 11> Ii_nel udCJ'<.-colrl:i.e • hi.> ~def.i-WiIo(.' &'1 ZIE 5

l.l'lt: ilJ;l'SUiE:l (.5I:ZEJ; n.ulray i:!Ie~lared voi.:!! ,rr.a.!j, (1

f

void enh~ J; , i fit" -.int1, II il-BiLe'l;. iO'fl9J:;l;:It-C t. j':P!Il -"Did d.:I.:;plLl,Y ~iln: .• :hct'} ;

"",oid 1:: r-ll.l'lsB-QI:!le (1nt:, L!:'II't.) ~

irlt'l:'ow.c().lm;

do

~l.'!;::!!er ~ j •

,cc:n.lt;<:<:.""\nEJlt'l:lr i:,l'Io ~rcl0,t" cf mn'lEr1.K: "'=""<:"<.s.12l:<c ....... ~<l'iI.t~« .. \n ... C:ilO1 > ';. ~ow,_;> C~ llQ~'

while tr.!;I-..r.;..-Q I: f",cilr..,QS1JlE 11 col'm.-:"'O II ~Cllm)oSUE:'1 ~ IIgQ.{_ r-a.I!I:q['d V<lottlill only' CGUt. 00(<: " \nE1'I·t;e:r the mat.riM ;:,1: iU'"d@'.l': !~<- ~IO~"<:" '" .. '«ealrrt("<,~n,d:l; E!ntelt"fItCllw, C:4111'1l1. Ilfuhct'iQn call er!!te::; 0

II,ec.ho the (la'tn.

Il::C ut co::" , I'IG i 'll'etllrn<;l't;!;L'ic 1 s ; \ n \~ .. ;'

d!"1p.l&y h.-ow, ca.i.m) , flf~l'Ict1Dn c~J 1- diaplay (] t::Olit<"·'\rf!l:dIl.spOS@ o[ '~!!It..I"j!!l i.~: \n\ll;

t !r an ~pro8E! ( roroil" co_]!.:fl'I~ r Itfu !<ct;: ii::l fI ~a11I t ~ I"!!5PCd e 0

'3I1!·t.ch- ~,; II h:t: ~'¥i~ t-h~ .I!!l~;O: er1 \! ~t.jjj, ~@ltl.e 'key La, '~z::e'H ed

II.ftua ct..i.on "E! f.i.nl t.,iQIl en t,G r II '""oii;\! £ll1te:r Hne :;aw, tnt. e'llllJiln) r

l:E1lt ,i. j; f 1l~~a1,;,,,,.=.ilil~B cileela:rCla IIIN'l~ 1;;1 se I''I2.I1!:di!],q

for u.=O. ["a:o'W. i"...,.,

fer ,t]ozO, j:<colm.; J>f-f.) e'il'l»a U.l lj 1 ~

8D

IIfl:lF.!ctir)1'I dlE':!!~nr·~.:!an d;j_~.l~y p void. ditolplG)' ~1.nt "ow" int: fZclffi)

Lsrt, i, j; fDr(i~O.i~raw;i++~ t

for ~ j=-0l ~ "\:~l~t j+"') Eou£cca[ill1~(~·'tj~ eflllJJt. "<enal ;

}

II ,f"l.lfl ~t;1, UWl de f i"OJ. t.1.0J1 t. O:.al'l ~P~5e. III v~id tNn,5pO~t;I Ullt rO~. int cOlm,

!lIIt i. j" fD~jj-O~j<.olro~j +t f

:Emir ~ i=·~ i i <-!'or,.,·; i+)

CQU -(<:11 [i J [:j j .(;<' \It:" • i::(ru't.~,(eruU t

The o:U:~P!()t ofp:rogra:m. 1.3 7w:ffiU be ~ ht~rMl,9 or.d!~t ,((fma:ltf:i:x,o(= 5'" .5

3 2:

En:te[' ·tIh.ema~ ,of orner 3: 1\11 ,2 1 2

3 4l

:5 6

Giv,eln ma:trix is ~

1 2
3 41,
5 6 TraflflpDSEl M matrix. :its! ;

1 a; 5,

2 46

In, the aborve ]m}gtruIi. .~ an-ay ~rS[ZE][SIZIH is doolared as, gWobal variable ,!'!o,tha:t i.t ~1lD 'beued hy aUt.hetlHeemnm.o-m. 'Thef~Ili<l:na ,eaMd) _and dHPl~y() iN:'e WIJd, for R:Qld~ itoi ~awri~gthe ma.trix. 'nI.e lii.uu:tmn tl'a_.dlltji'puBe() printBdleb-an:spose arthe entered maW.

Two dlm!egJiODal.OOI!U'a~T' ~Y'l., TM~ t\!l~ $~O~ (111 (;!:J"roy (:Jfs,tring~ wMroin. the r-a',w repRSe1li~ Qtltr-ing ,aM theoolumn represents: a .~ins,le.chamcter in,f,h,e, st.riq. A. two d~maDMunl M"irH¥ ,cam IJ@I iniJt],lilimliin t~e: .sameway ,ostnat <lI'f:1l '01911 dimen:Sio'l1ll11 attny. For ~X1mlple~the foUowm.spro,gr'a:m.mutcJ"'.H!U!stmJ9 ~ncept ::

Co yr ght rn ria

ft t.~.~llHl~<..'io~!:l:'e~m.l'l::> ~ hIli: lmdll:';: ~q"i c:). h> voicir.!,1i in 11

cl'l~JI:' m.OIl'lth:n.arn.e~ 11 Z ~ flO 1 "'t"Jaflc\.iIia1:!("'; "F'~brlJa:ry". ·'Mlu"cli"'. "itIp1t'il":, "'MilLy =, "'J1Jlle'''! "J\ll~", "lI.l:!!~uSI..". ;;"50e-pf.:P1cth~: ... "Oc[,ob~I:'''. "tl:QlI't:_n:jbe'!!:"" "'Oetembe-r" It

int m'JtiLti'l,,' el.r~1:'1 ~;

QCi~t..<~"E~tle',I; t.h~ ~nl;.h I1IlUffibe£' n-U} .~ \I'l!" eif'l~?!lnol'll:;n;

ijf(,IlICllltl:l<;J. • I ffi1lilfltb:",UJ

OPIJt:<:.<·f\r,lHr'iJ~~ mo,nt.h numbnt C!.1tt:~I'adl!,!l"'; gl,se!

eOI;J~ <<"oj \ llMon'(;lIrtllm~ is : "oI1.{r:!Iondu!l~iI r mOi"lt:.h-l ] \(~ ... \ I'l" • 9~tCfJ ,~); II 1t'll!f~;l:f!'t""le lIl!Jnito:l:'

The (I~tp!(ljt (If progr:am. 1,:S8 wiUtlB ; Ente<r the :mO'ntb 1li!Jml.ber U-12)

4

,Me:D'Jtlru. name .i:F;;, : April

Ellte<r the mO:!i!I,tbl:l!!,u:ober «1-12) 113

\VN),ng~(in,tb, ,:numoor enl:etioo

'S:~filreth'e[\e Be. 'tw!e[va Btringa, and dl,e~ength oftbe.lo'll:gest string.l!i!a.me~y Septemibgr~ llfl n~nej,the MTIliyIS declered. aa ;

cha.t' m;IJll'ltnnl!l:me5,[12"]1 (":1 O]t

Hem m.ont:_a:tn""'I[-,i'II]~ ,,,,,,,,~ , ... ~ !llJ' --, - C 'ii.·~:iI! -. ;,d·-I ....... .,-t-1!;--..,c ,-;-;. --'ro!li]H'I~- -.t!-:.-, ~". ~'L.., - ~'L..", .... ",~~,,,,

_" _ __ _ ~....., ,!,II ,I ~'"' I<U u.~ J au ,""",,11. -m:J..atm0ta,_!.i"J! mler5 '''''' II.lI!.e !;;J],,,,,,-,~1OIe.£ -

,o:fthll! :8~g '!!IApri]J~,. -

U.sizedi ,U'nly' igj.tializatiO:M.lf an iIlrra;y is' inJtimuea ,com:pl~t€lJ' at n" ,place l(Jf~·' lam~ion ,then in, C,.,+ :fM $,iZ-e ()ft~ ar:RlJl ,,-an, bero,u;n:ted by the oompJier: .

. For '~a:m:p]et

char lIlI<lm!3 [ B

mtar:r['~

"'Ms: 'iP'O,pU":;

.~' f6. 25 .• 35", ,45,55 };

. '". ~

IO;B,'i;maikfi: [ ] ~ mt val.ue ~ ] [8] -

('70..1)"40,7,,, '9(1.8" ,5,5"91; (5" 10"~16 ~

l~ 2~ 3,

4. 8~12

9~ 6" 2(1 Ii

c'o::!' ',"",",

Witb, the ieWp' ofsu:ch d!ecJaratl~n:8ltihe valu,G=Hstm.a;ybe cblltl,sed lID, oomtti,e.,~ Ie,I1_hEiR or Hbo~n. 'The :r~ ~dvaI!!bge' 'is t'ha:t 'tiJ:telRn'ay ,dimeillsiori.(s.) re:tn!;l!itl U!flIoo;m;ged"

A ~n(:tion gro'U/Ji8 ,IJ number ()fp~grom ,st:atem.ent8Inm as'i~gl~!lnit '9li:dgi~ll'sit a, name. 1'~l:S U!m:t 'CfUJi 00 called, (;i;nvo'kJe~Ufn),m ollierparlcS of the pn;gr,am. "u:uet~QiIlsarelthe biJl;iiJ]d:iIaiS blooi][)EI ~f,Cto-tpm,e:re.ms 'where alll the pnlpam activit, OOC~N.

III.ad 'Dr 81 FuIIilG,Qan,

MonolG'tilli:c p!l:'ogr3iGll, (a ]ar:p'Silng'te HBt ofinstmctiO'ns) becllmll>B difficult touU@l'staf.l,d" F{I<l" tbisreason .fOOd.j,i:)Di9 ,ue used. A" (un(!1,ti.o.n ha:!!i u cl'e(jr-l'Y·d~fined' o~jec,ti!/je (purpose) (100 a ,rJleat;I'Y"defi~ i~~e~~w,~~k '(lillie" ~$!flQ~ l~ ~b ,I}I'i(Ji1ro.m.. A~~p (If functimrus; tQg,~.~ ii'! ~~ a m~till.e.

Beduetic:m in :p'rogm:m size ,iB the another-reason, forusi~ functimu. ,Anyi!:ieqj1!il,encGl of sta:teM.ents,&halt, ismpBatsd, imla,prog;rarrn calli be oombmed togeth.tG, fonn B fiundiJl):n., The imdi@lI!i. oodem iSOOoc-ed!. :ii1!lonly 'o~epla~e in :memory" ,even tho~gh ilt 'mlffy be ~:x:eruted .~ man.y t~mes as ,8, 'Wle~ :nreeds.

'Tbe m~I~~fI\g ~f(ig'f,j!;Miilhl!i~tra't:e!!,t.heUJSe OIf i~ ftiWi~t~on :

IliilSj:1l.i'lY .!J!!meral lTlCc5:saqta lHill.nq a h~t1:C'n Ii inc 1 U c:!f' 0:: i OS,'!: .ream ,. h:>'

iH~lude~~Qn.c.h~

vp!(:I, m'<lin ~)

~

'l'Clid d;ig1ll,1~y:...:~s:,o;;;t!,1e ~ ~; I ;'£U!'Ullt ton pLC~'l:oty~ c.lr:: s C:~ 0 ~ J f C:lea:r::s t:hE sc rr i2:en dJ.~la"lJ.!'i!~~It~e 0; lI!u$lQtJQtI (:e.ll

get.ch ~ l. / I [<reeze t.he' Imnn1'E1[!'"

I Jfwu:J;.ien dlil.fini't iOIr.Jj vo1.dI fJl,l!;pla1i_mi!'!~.':Ia:9ii1l t) r

CQIlt.<,.;:ij~'El.h;OI1iC to t'BIl! ~ij1'Qr id of ~1:'Qqr'dwrd.nIjl\r:L'''';' CGU't~<"P.~D,g,~!H'J L9i flQt::n:in~ h~iI: laq.ic i:m~lelT)BllIt <II[ Ion";

The ou.tp<u:t ofprogl'n:mt 1.39 wiUbe!

W:e~,oom,e' W th0 world elf' ptIOgraMmin,g P-rogrammln,g~s natJh.Ing'b~t 1:0.gli.c:im,plement3:tiion

In Program. 1.S9'., the f'imc'Don ,clIJ'lrii!' ,e ) ,c~e8im tneBCti:'l~en (~t is :o.tibraty run,ction) thent;ib,o :ri.m!ction, dil!lip~aJy:J(!e~!3age( ) ~ i~v.d f~. :m8iin ( ).']:'he oo~twl PPlS5~!ii :00 the [iuilctilon dlsplay_m.euage ( )'. ,ood, the bGd, of t'e' :funCitlOD ~eel!lilled i.e.,tJtetwQ 'OOlItPU:t sklihm.en:ts dispJa_yft.e :m.'es~~ .a:ta. ~boWlii! b~, tbe Qutp~t. '!'be oon~ro,]thenreit'IlUI:Bil:I, maim o and. the :t'IemaitriDg ,codflgebi~cuood!. The function ge~ ( ) waits :ro1"!B!IY .k:eytobepRlssed . .It IS a ribnlry[U:n.ct~oill, :MDOO aoout!; 'Hb;r,aJryfiundiO:M, ~<1Ih:rDn.

In proFam 1.891, th.e fil!l.ndsi<!n:rn:8'~n ( )~ whlc"h. :~nv,l)~e~tiI.e,run~t .. iiol'!l. ,itis,p,laY:.Jmessage ( ), is ca]]ed the calling ftmcti.o:n~ B1ndll!.he f~nctio,n d.i:8I'~a,:.JDeSiBale ( )~s the called :llmctJio,n.

FrunGtiion Prototyplng (Declarationi)

Lih alliY vmia:bte ,in a C++ progr-am ilisn:eces'8aJ"1 to prot~pr.lar ,fkclare a furwtWn befim!. i;t~8 UBe. It iruf'orms the compiler' tha,t,thie :function w,ooild hem:fe.reulledl8it ;Hi, later st,a,ge m tile'prog'l'mtI.

F~' 13'Kample"

In program 1 . .a9~ the smtement

Vioid di.s,p,IaYJnes.sage n f

i!! ~.ful1l!c;tiQn 'pfQkltY!I;I!~O,r a d,eclm:-atiOOl. Herevo~d, spooifiies that thla, fW:tctiGIfi dioe!!!, mot :m~um, an,,. vnlue,Wlidi the 'emptypar'lmIthiItBe5 itnrulc.ate that,-it wesnO' perametem (~e~,mr.

T:he .fimC-#fJ'A p1"lrJ.tatyfH!. £!I aZWaiya: tcr,m i:'lat~ with a scmi'rol·fJ\B.,

Dafiniiln,g 81 Fu nelien

1~ CO",",". a: ,jb;nctLon.must be defined prj(}rto jt1! ,use in the p~fl.m.The functJ.f}ncl.efinJ~ ,ti;o~ C'(il'1t'ain:8' the cOO'slot the ,f.ulu'J~iCl'n. 'Ib,e:functlio!ll defuJitiDu, fOil" d~splay..:meEiS8R11l' ( ) in. Pm· .S'l'BiE :&.39 iI;. g:i,ve~ bel~w'tbe main ~)runcti(ln.

E~imiinatingthB IPirotoltypl'ng

.tfthe oWleel ft,mcJiQ~ ,defigiJ.io,n,a.p'pear:$belf)~ tli:erolliJiW f"nctw~~~deli~iti~ llmn, the called fUnct~on~p.ri(·J'to.type ~yb~ Q;voidetl

Fo:rexa:mp~.e~ProgHlm 1.39 lcan. be modmed! ·to ,Bi,voidfimctiiOIITl P'l"OItotype as g(ill'\e]lbe:~ow ~

Ilciii9play '!1bn~r~l 1[]f!!:s~tIIg'f! IJII.in9 !> flln<l1l't:jo.n

i ffiloludJl!! c !.1I:i'1S t realll.liI> Ur.JellJde"m:olli~ .1:1> l/£uneUoo d~;fi;rd.tibri veid di~p.l"'Y_II!II!U'ilI.g:~ (I

j

CQutc<:"''''''l!'l~rnl!!r:o IChe "'Q,('lel 9I pTogr~l1g'\rl" .. CQutf;<"P't'C!g'ra:mmulI~ is f1D&hinq bllt. l'fi.c;Jl!'e. JLl:lipLl!!f;I~I'1t.'i!I.ti'O:n";

l:l.lr~!:! lr ( ~ J I J ~ 1 e,a t'5 1;'he .5CT£HIlfl dl"Spl,aY:_JI'I~s~age 0; /I 'I'I.unc;HQ!'l ~<'JI1l ge'l:eh ~ I; II JE;I:'eeOB'E!. tbE3' monitor

The O'll'lip~t. 01 pmgr,am. 1.40 wUI bewmeu thai!; ,of 1.39.

The 8:00vle ap,p.[;oach MlnoViel:!~hep:[\oWbPil!!ig'bu,t i.t is :~egsBetible~ MceU:SB .~ou will nav,etowritealJJ th.e fu:ne,t;ions be[l)r~mlll!in( ) Mldi mMiIl: of 'tho ti.im.fJ, you .Iim aot ,sUJ'e ,of the iacl

Ulia;twh~ch fil:nctiO:I:l:,wiWlIbe in,volmd, 'by whlch one:. -

Smoo eXiee~tron o:fttleprogram s;mris, wW,tlill.mam ( }" same p'Ngr,ImU!,'I.'elr.a al!w,I~Jlt11preftil' 00 a;rnl~ge maifill 'e )in :first pl,aCll, .• How :it's: upto ;)HJII!IWI dieeidl1!'wbJch a.pproa'C~. you ~.pmte:r., Perhap8 tiheirat one,! ,a;s inProgr~:m l.89.

84

... ~~------------------~~~~~~~~""",

Th.e syntax of a fml.cDon .definition is sh~ be:tow :: byp'ijl·Ndffi@'_o:II_t[;,[!'_,flUm:tlon{argU!l1'lIl!Dt li:Eit)

'w,heethe type specmes the !type of' thevalue to be r~'tllmed bythefunctruofl, It may 'be ~U'Wj!' va:~wdl CH data tJ!'Pfl.Wlieil}, :no t.ype i5 giiwfi, 'then the mmpUe'[' :retu:ms ,ilI11,tmtegerV";i.llUJe.

N:~e:...~t~.(!:..:ful:1:~t].oID, i:savalld 0++ idQn,ttfil~ (;"0 ft-sm-v·edwl!)'rd alli~Qw@d) a.@DDed b,. 'the ~1iS!e:[' ;and J't 'can be used b, >0the:rftll.'!!cti(ln~ ftu: calUng this fU!~c;ti~n.

A~~e~,t. list liS ,91 cOimimabS€par,~WdI, Ji~t, of variables, of;9:, function throlUJgh wh~chithe funcUQ[l,may ~iivedata, or send. rde~ vih.e:n. caI~ed fTQlll Q~b.e:r ftu,:u::tiQ]'[!,.'\!lJben. ~I) pa;ra!1l~Le:rs, '~e argum,erl:t~jst :m mnp~y!i'l~ you 'ave ,alre'ady :3e(,:[;Ij illprngr,rn:ms, 1.,39 and 1..40 ..

The:foll0w:mgp;rogra:m shoW'S ttl.€! eO.ll!cec,tlo:ra~el!ilit passing to ;;I, ft.ll:ll.cti:o:n ,s.\BfFI..1iN ()

~ . 3 . A . 5 ~ ~

to·- - ,I!' -d' ,. -';L - 'f''''L .' '1- X.X ,x, .x. ;x, - x

:lO:rw1D1 .• mUIi~]e snm o',".r.~e se.neEi ... - ,_ + _. - - + -- - _, + ,." ",. + _',"

. .. . C!. -- . - -- ··2 a. 4 .5 (5 :N

It sum o[ the series

~·ln.clllae<iQg't r~otm .11 .. l4inclw:le,o:coflio. :11;;-

nnctude~th. r~ Ii [OI" POlliO fun(:ticu

U·081l; ~mFIDl' (noe.t. i":, i nt, Wl ~

i1~t Bum 1. t) ,-3ign1li~. tenlll int i,

for~i.2i~<~Npi •• J

sisn '" ... -1:

{:..,;:'t:D'i = 5J,€ln" ~wtl'l.il/i;

E~{"lD [_ M" ICl!1:iul." ; bnt !~I

ClI'J1Jt<M. .. 1::'1[1. t.er x <ill)!U U'm". Ci-Il~)oK>:o~J;

lL~U.! L .SWF'UN tx ~ i1tTJ .,

.:!Qli,tl:"'''' .. \,AAe !"i U 1 -=> ~ «' ~iE'~U It .,. .... -end-l.

ge[ch~ ~. IfflL,e!;!Z~ the ~Ol;'ll eo~

IREVIEW OF C++

85

The ou.tp1.:liJt (If P'I'OfP',1ml 1.41 will be :

Enter 1:, ,and N

I! 4

Reaullt "'" 0.588333

,lftlu function, is called wU,h diltertllt ocluol ,atgum.tnt(s) more than once: ,jraa pnwr:am., the ,WnK Ht~al 'tate1l'lC!l'JI,!;I wilhin: t1:u: !unctJOl1, ~ ~uted;Witlwut .u.sing fum-:ti(l~", tM ~~'~f ()f ,sfatem£r.stB Wf)~ld haw h' becode.d tile requind ,numb.uafti~!! m ,t~font-!i£lii molR (J. 'fwe dff! h.at.t~'a fu;ndion, il~ mo~ ,lit! ute:~ttdwith d~~nt .!'e~' o/,data tU BOOWn: in, 1M cWatteP~ bn it will.be called :once for Ollie eucutiQn"f~

The function, ,synt:a:x ,can be' shown wUh '~,e help o,ffoU.owing ex;a,m,ple;

veid miI,in. n,

{ flo~t x. t:Je~1!,11t,; 1/ loo<!.iL 'l<!i.doml1!!:s

U o~ts!j~ij~ (nOo1l!t L_, Rt!ittliltn, type

Ilf'UrlcUon c;:a,J[l

I oil'Ct;1;!,"'J!. p~[!,"~"met'e:!;,!iI

Ilfuncl::,ioiHSl!MPUN' {~ d!efirdtioll'l,

r--'~Q sem.i.clolon, bere

:f10!! t. S(lMi"UN '~f]oat: x, int N'}" J I ftlna:t- io.n deal a~at ionl

I

.If aorne tlotue i'8 R,hlmed by ,thf~UlctUm then either it COI"I, be; dim:tlll outputted u.dng ¢aUf ,WlfR ~'tmtJ'Wn ~'~~~!i'oQWQl"# ~~ ~·~",~dl~ W~~ ,!,l~bk (4;t~·~ ~ q$ ~ ~~J:!;~' f!{' tke fflJi~tWh.

,Always rem.ember that aretum ata:t:e:men!t always retums one value. When rettml is uaed. witilO.\lt pareEJthe6ia.no VP}IlJ).e ~s; returnedw' the ~ing program. When no mtum til

specified tb.e.flmcUQnre,tu:r't1!!J: an :integer value. ~

'I'YPes ,of Functions

F'tm,ct~ona are dass:Uied in. two~ypes. IThese are i ((1;) Librory FundWnsIBui.U-m-Ji;unct#:m.$

(b) Use', Defined Func.tioM.

(I) Ubrary Functions .

Mail\yofthe runotlwj.lik.e. fmdingthe square r,oot"ora n.umbet. sm.eV&tie ,oia slumber. clearin.g the Screen etc., ,a.re fmqrl!i$fiUy used. 'by Ute pr~eF,1J in.th~ Pt(l8l'!Un$. Writing Plrogt3.lliS fat flUC'b. fi;mctions. in. the p~that, ooquirethese" :is .an ~58ary ami oom· II'lex Job. OTt- UNary pro,vides s'lm .funotione;, :so,that these CIW be ealled!in, any program. 'These Iilm.ctiODS are· eW1ed Jlbrmy fum:tien1i·.

Copyriphted rnmeria'

88

.Foil:' example'"

We .hs!.veU5ed~r (: ) fum.:t~(ln in Program 1,.39'and Vro,.Mogt ,commonly ua.cd math ]ibra~ functJ,OD_9iare:

I abs (x:)

exp,~) fabsb:)

floor (x)

Re'Um8 the absoWute v;alue Qnu inb!pr ugume:!1it : ih i!!~ 7, a:bs, (x,) is, '1

R.et:un:m,the iUfiiiillteilt WbD~llfiWIibe'l" ~t l8 n~tie$!! tbatn J:i :ifx ~ 83,('. 'ce~~ (jljis 34!

RetI.UTIIl 'the cosim.e i!lfangJ.e 1:. : ~f 11 ~s O.OCQs (d 1:1:1 ][,10 (x in 'l'.awlIi_).

Ret'W'll9 ~whef'el e~· 2.718.28: ifs. is; 1 u:pU.) iill 2~:US28 ~ ~lu,~ va~ue of ilb aTg1III:m,e:nt :~_f II is- l!5, • .tiII, fabs (x), is ,l,S.4S

Returns thllli hll"8eBtwihD~onu.mb!9r that iii! Dot ,~ate'l" than!!: : m:x.m :3',;!"45,,noor b:) i:!!,37.0

R.e,t\!l:m5 the ru'ItunIII! logarithm (It x f~l' 1:. > 0.0: itx is 2.71828" ~Qg(Y) is 1.;0'

RotU!tttS. the hOBO il~hlgarithmafll:tor Z>, 10.0 : If 1: is, lO!, tQg' lOb:) ill 1;0

R.e'~ 'J.', If iii!;. is; ne,pti'll'e" Y wuilit be i1I wbole ni@moor. If X i~, 0.36 ,PiDd y i.f!. O . .5!poW' l'J:j. y) is 0;6

,Ret.tt:m:.!I tbe rune, of ,angle x::if x is Ui7,ms,. sin. (x) ~!I 1.0 (x in ,

radi,!ltul), _

RehIms ·the n,GD.negative 8Il:UDre root ,ofx (Ji) {'Or I <Ji: 0.,O ::ifx is 6~2B •. !8qrt (X)i& 2.5

Jto~tho ~g.mt 'Dr 8:Dglo ..: ; if xis 0.0" ·tan (~)I i!J .oJ) (x In radians)

<:EIlath.h> <DlJl'tb.h>

C:b ... eterF'uneUou:

M.Gst :commonly used!. ebat:ru::ter :fi1n.etiions ate' :

loalniUiDll (eb)

ill ,Retll.m8 nmu:ero if ~Jt8 a:rgumenrt; is a,ha:tter era digit. ~tllnUl! :HRI if~t!J i!U'lUn:umt, la· I1~ an a)ph(l,!l.~etil; :ifohis ''A\ 1$1n'.1!IID (~¢!b) :ia nom.et'O ..

&tu:m;sno~~f:cb~fj; an ahlh,p,bet, Qth~1'WiIie~t·re~um~·zorQ' ;: jf il;;b ·is· ~:A/" ~!I!llph '(!ili~~s ;g,1'I~:!'IerQ.

Retums nenuroat: ca, ia ;Do digliJt (ie.. O-9)o~' otherwiaeretu.nlSi '~rQ :; jfch,~11'5' .• ~~~ (~) ~rnm~"

Rtli!1IllS 11{1~ ~fch ·la· alQWeJ'iCIlSe lette:'r.otherwi&e retll1'R;ll: zero :; itch .il!l'a". iiI~.ower ((!n) ill :urtU.ro..

&~ n,Qru;llrn'1:f,ch~fI an. U!p~ letter. otberwiiBemtume: zero :; ihh 'IS 'N, iauppe:r (.;:'h), il!nQl1zet'O.

Rel~ Ilppe~ eqUlvaltmt Qfchirchi~ a letter .• mherwlse ell. i$ retl.1med UfichllDgOO : if ch .. 'w',. toupper (tili)wiU be' At.,

Ret~f(l,'wemt!W.l equ.i,y.lIi[ent, O'f'ch~iI' ch Mil leUe~~ Othl,'lrwl8(!; ~L,' ...... • __ .:11 .'. h" .-.1 ,''f h, ,''AI '1 ,'. '( h)'''n L_I • t;;,U, .la! relil;lll;_ UmL!IUl~~ ; .. t_. JC'_c' ,.,,' '_._. w_'Ow;er ',!;L " 'l\'l__l __ . ,!If •

Copyr'ghted maleria'

REVIE.W OF (::;i.+

The iIli'WurnelU t:t~ Q/'oll the /Uncdo1t8 fa mt'Q8 intemallYIMMarade~ q!"ll:P~ numm· ca#y {lui,. tMU ASCII ,codu).

Strin;gFunoooa&

M:ost ~I!!I;moruYUHed string wnclii.oml are· :

I

IFun;ef,km

I stnen. (atr)

I st.rcpy I{target, 8nurco~

~ fu(llengtb of thel Dull terminated. dritIg·pW.utod to by ,efr. It does not oou:nt, t!ht! uaiHng NULL :

iIlulen (~DmT') '","wd givel ,6.

C0pie8 .~ Ot'Igtenb of l!iti"ing 'lIMll'OII'Iblti:i mrget. ~ ~~py(~t, "NEW DELf[I")w'Oul!i:lcopy"t.he atringNE,W DELHle:o, atringtarget, pro,v:ided size of tarpt :iI!grgtm' 'than or equal to size ~of sOW'tt!!Jtrlngi.r. .• "NEW' D~ Concatenates ,Il tcpy >of aurce t:o ta:r&etand terminatel'! 'with II. NU,LL., 'Ehe IUlet ergg Mlrutd! be large et!oughw :store both. '~t5 .origiMil,OO!.l\.e'!Jlts -mtd!. iho~ of ~(I • :fIb!l:'a!t (targe~, ".FUN'N"r') wJil:l! oo~te.nate string.Ft1NN'Y ·wi.tb the' striqgUl tarpt.

1000mpareftthe 6t:m,p1lOun:ea:n.d tupt,cilu.nK:te:r ilzy' clJ.ar.. ,ad.eT :ftnd Nt,unw Il:Jlmt _h~!! ~,rOln()W,B

<: <0 :if B1OI.U'l::e c tarr:get

;::; 0 jf ~urce ~. target

> 0 if !lfiun:(! > ~t

'~b) User Ileflnedl Funeians

In C**~ llJ8er can dJetme fl!m.etion:s fOF'specific tasks: to be peri'o:rm:ed. Su.chfullctions an! known. as user-defined functions., You Dave seen, the defin.iit.ion and calIingQfsuch functions in progran:m 1.89 to 1..11 i.e ..• fun.etionsdJsplOY_l]lc'89age ( ) and. SUMFUN f ).

Ad;vlmf;q:et!of'gel:ng PUnctiOi!U!l .bI Ct+

,Some adva:nlta:ges of using fuoc,tians in C++ :ate listed below::

(i)A oo.mpk¢ p~m, ,can. ~. d'ipid«i m~ 8~'l 8ub.tm>u and /Unction, IiUtJpnwmms

= = '_ .

can bew'ri~ten rOT each.

(Ii) These an ~(l8J1 to wrim., unihmruw' (1M deb •. ,

(iii) A lundfun can be· .utilised in mrmypr'Q'grrl1ns 'by separately compiling U and loading them, together.

(itl) C++ las the faoility ofdefinmga fUnction in te'rmS'of.~twlfi.e.~. recu:ai0'D:. Recursion suJts to some proceBSf!S b.tlf naf to all.

(v) .MaRty .fUnctbmtJ tliu:ch as'~ cout, sqI't ere'.ar:e kept m c++ Ubro,rydndthe compiler .of c ... ~ is written fo'1' ,callirw ,anys;uch .function.

Camng 'Flmeth:ms

In CiIi"+ pmerama~ functIDOD5 wi.tb. Sl"gume.nt6 .can be invoked by : (0) Val'ue

(b) Refe.re~ce O~) Call :by Value,

Yon have nkcady seen the call by value method iu program. 1.4:1 .. .1n thts metiwd (he lJalu€a: of t'I~e ,ac-tu(JJptU'Ome.krll (tlP~ in the {uncti!:Jn CQ1lJ an· copialinto tJre l(Jl"nuil

Copyriphted rnmeria'

p~ra"'lete!"'-" (appearing ,in lhe fun,ction defin i~iQn)~ i.I!l! .• the fitnC'lion cnates: its oum ctlpj'1 '~f argu .. "~nl rJalu~:i ,and (JJlfmte~ on t~'m, 'The fon(ilw~flg pfiQlra.~ ml!J!str!!!~~~ this COFJicept:

IICiI!.cul.atiOn elI CQWPOllOt:! lnte,::ost tI~inSl .a ;[I1Jlction 'f i riC: 1 ude <to S't ~eal1l. h>

• i:m:::ll.lde <Ii:Dlli 1:1.11;)-

Hm:lude .... m.il'th.h:. llfo~ pe"", ~ I lunoc;.iofJI voJ,d i!l.'I1.fII 0

r

float. prinlCi~al. r~U!~ U.me; /I:Local \I"·a.riii!ble!5

'IJ'O cI calcu1<t1::oQ (;float. flL;lilt. flA!t~ t I n'u!n.c!!:'l~n 9.c;-o~t:r~e clracrl J ~

(lout O::>f_'" \ 1':L~t.~f tlIefc1.LoI;iI!M v,aLui/:!Ii ~ \11."'; !::CIII,t; <~'~\nPrtn,clp~l.'·;

'I!:.h~>p![.iI'l1;i!Ull ~

1!!~1l!.1:;(".("\nRata !II'!': jl'l~~1I:e,:n:~'''~

~illl."::~'<~lll::e;

~il'l"':H~.ima;

calc:ula't.".;::'(prim:ipal. nte" e lfiliO')' II futI<Ci.i'l:lfl call qetCfl ~) ,;

II fU!1,ct..lo.rI @,f.!..nit..imtn ·~.uCUb"I!:D ~ I

void. CMt::1ILl'l:tl!! ~;['_tl!:lJ~ po, flli!.iI: .:1:", fTcliiit ~)

OO&'t :!..n:t.el!"e~l;..f /llmc!IIl varLolble

il'lta~&t '" pO' ,(Pl!)W ( IH't"'/i OD. 0 ~ ,,'I:I ~ -Pt C;QI!lr.-c<:"\pCampQ>UlIId l,Dt.!IH:'Il!'St. is = "'<O:::!~'1;.,!!~:;t. ~

TiI!e 01l1tpu:t of :p!ro~m 1.42 wiiUbe :

En,~~ tbeli~no1llrilIllgvall!!l.e;s :

Primcipm ;1:009 Ra:t:e M:inte!!:'e!:l,t: 5,

Tim'8 period (m ye>fmI) : :2:

Com,pomtdm~:rest is; ~ 10,2'.5

:m. :P,mgmm, :n.42valu:es: enmred,[o,1' 'the 'Yarialb~e6 pd_geipai!, mte a:nd time;u'le passed to the :fb.l'I!ct!i~'n calculate < ),.Wl!.en the frimlct.i~\!l. oaJeula,w I( ) :lis i~vQk,ed .• the;!i!e vaWu,e's get mpiooinro,the memoryl,Qca:ti.one 'of the :p.1'gUm.ents. p~ r and l IIeBpect.i:!!'eb.

Copyr ghred ma'mial

1iiill8

p

t

r

Fil. iL.18

So~ we oilsen-t,tb1ii:t UleVV,~ll},le,8 m. the ,c:aUingfllillc:tion. ~maJm( Hbe dlifftllI'liln:t fr.Qmt.ib.e va:r.iabte5~n t'e !caUed.f1J:n.(':,ti'o:n I[tamewa.w ( H beellnme,lhey use ,a,iif'ereD'tmem.oQ' loea,noD'B;"

Th~, tlgl~u ,ril/ ~Ili~ '~1:.1gJ' gr~~R.~' :i~f~ ,~l'ir:'"f~~.diQt!; do~~t gl!.f ~~ ,w~n '~'h€ !aTg~· meilllfil ore pmsed 'b;y illail'{4E' eiWR ,fho. the iIllame ,afforcmaJ argwnEilds nt!'il'y ksolflie (lAue are

co~!S',idered t!liDere~t).

A:rg,UIAlernt Datal'Jipe Spec~fh;:al1an

C+.II!- is a ,i!:tro~,fy dllta, ty~ 14filgu~~. Thie C++compilerch~ fur' it'he type (lf~fi!'Jlen.t lis,t a:ndl itsml&um, type. Jfyou make a dat. type mismatcll, hetwe~ii1I, actua1 MId, 'f'o:rmal argu:l't'u~n!ts an, :impilici:t ,data, ,coD.v,~rsion ta1res ,lare if: po~s:~b:le, If tile n~ of ~ments~!S t;).Gt, eq:uol Ot' ,M Im:pUdt. eonv,emi.o;n IB, .notfeasiblerlhm 'l),compUe t1imee:D'Ori~ i!dpaIDled,. The runeMen proto'bpeg;iJV~8thecOmrlPnerfhenflceaery inrOr:m.8ti.~D. srttt..e eo:mpiJIe'tiime. ,~a fUm:ti.on IiltHt be d@cland.bl!ifo:~e cMlu.g.it.

Re1uflllilng 'Va[ue~ltO:ml Fluldo,n

A fun.cM!lI1D. ~.rert~ a single va~ue to ·the ,ea1lHng Functio:n'Y':h.en ~t !OGwplews~ta exft:UJ.~ tion .. When. .Ii £lUIJCDOn retrM-na a. vallu.e:,the data type. ofihe vah:~em1U!t be, !!ipe~med!inthe pro:klt:yJlc.l'.1)l' ,eu'l1l1p.le~ kl the f:QUowin,cproID"8:!m!~the:~e dQ.~e :!is p~d 'befo,[ie the £ooetio~ SUMJFiUN( ,).

n OOIrl!'i'L!'h :3iUi!! of 3e..i:ie.:li N 1" li1~21..3 L t lIl" J/5l + ---- . XAn! (In.-ll 1 U.m,:J.1Hi~<! !J'.:lIt!:'I<lIl!t>l. 1:1:>-

~ i no lil.de-<; ~-Oll.:!. 0 • h."

~'.in~llll:!ll!<'~4t::h"h:> II fc:;;, powO

voId Itlair.q l

~ dQuhle SWf"IJl~ {deuble. In.t~.' II :f\met:iQn p,t"o'totl{pe double X:

lEltn~

cUst:l.':(' I

out..;.::;",Eincer t.lle v<!lll.E! of >: ;;mill!.I'I ~ ... ei<l>:'~>""n~

COl.!!:. (~~ \n\ l'IS'Um of" t.lH:. ~.e["4~5 is "«StiAFm~, x, IiH «enll::ll, 'fE!tdi ( ~; II l' 1:'t"'iE'!!.E' the· J1l(H'Ilur

f t funct.iCII't! detinlti.on Sl!IMFUN ~ 'I

d~u\lle SWFUI'l t~ub N, In't; :ral

90

g!olllbl!el :f a~t.. oS ufn=,X.;

l.n'L :1. j,

fO[t" U>E2,l.C" rio i·· ... ~

finrt"l.

f.rn:, (j""l,'!.::'l'<"'2'~..L -l~ j++' .fact '''.J;

~Yrn +- pDw(x.i)/[dC~;

Th,el cr~,tpui!; of program 1..43 willl,be :

EnEI' tbB vaJJlL!l,eof X and, n.;. 1 .4 Sum, Qf Ule! :seneai!! ].,1'5,];98

Constant: ,ArgIUIII1I8Ili1tS

B;y ruing ,(!Q~MlJf i!'lrg~nt(~ ;thetuMfjem, :t·lVtlJsthese U1"!Umen;t(8) ,0:8 ,OOml1tanfB. 'These. val:pe;s, !iU!nl@l'Il, be'modified! by 'llhefuJnc~i~!n.

Foil" makin,g' the argmnent(.s) !Conetant to a fut'!!ctil)l'iI~we :s!bou1d! !fie: tb@ k:e~ro C(J,D8;t\ as ,g:iven be~ow in the f'imctioll!lpmUltype ;

void max: ('CWlII:t; float .!l,r 'COJ:IIllt flQl!l!t y. (:QlIlil5t, float ~) ,r

He.f<e,j t',e .::l!;u;lIlUiBr coDfit informs the ,compH:er'Ii.1t9i,t~he! ar~~eJlL!il h~riQgooliUlt :l!!1houtd oct 'be.odlrn,ed, b:ythefEl'cti.onimm:O. 'Hl@:se ,H1'@ '!d~rw wi:len ,can b:y referooce :metbod, is, used ro.l'pwss:wg mpmcnb,.

,~tJ) CaJIl by Alfie,renee'

,A, ,N., '(';jllNiI'lCe! ""au,i'des tlInlilliaB.-GI, di,fIj..,~ren:t' n.a:m~,--,ri!)" t,It'" u'",.0a.bl- : .~A - s;~-· -, -';'-~.' ·1.l ,'-

~~ .. r· .- ..... -- '.' -.-.11.< .. '. --- --'-'__ __ .. _~_., .!:l'" e.~. __ e_:a:rne~a:naues

Ml1:l~ can &e1U .. d b'illturo di'"itntl:~lil:om:e-, • the, ciFi.".~- --l ntF- -, ,'--00 th; ; ,[', ,.-. - ,'. ,-'--

_. __ . . . __ . _ _ _ _ . ..,. __ _ ---.llL. _ . _ __ .s.. Q, ~IUia . . me a: .e a w:s ,~an;IIl'.,

In leaU.by :reflM'mce me~oo" a Niemnoo ,to tneactrula~, Q[,gumfm't{S,)'W 'the ,caWling pm-,sr8.im ls passed, (oml:rv,arl:ab~ea). ,So the ClliUed f~CtieiDi, does nOlt nroote it;!;,I)WT!. OOPY (If ori;gtnat vailuds)but w,orika wit1i:!tbe ~rigi~al 'mue·(siJlwiith difft'!re[!),t name . .An)'! (';hB:Hg)e in,thfil o,riginul dataintb__,€li ,caned {'!imctl,o,D, ptsl'€l:BieCiOOdbaekoo t1lle' IcnUmgfunc!;W_on.

It is uB@fW whe:n you wnntto change the 0l'igllrual , ... mli!b'~.es: .tbe ,ca:mr.g' r~etiiij~by the ,milled. mmction.

'DIel:aU.,,'Wing .~. ih:t!stro'tet3 'this oonoopt ;

IIS1oldpp.inq of "'101'0 lIuaibeJ:!I us:ing fUJact-iofl O!!lU by; t'~re'l7'~:nC'e ii.na1 ude .. lo~ [:!;"~1D.1'i >

1 ncludi@lC:col'li,c _ h>

void ffi.P-'U. ( )

(

t:lr:!lal!:' D •

in't; .!'I1!L'I'Il f n1.Ul1~H

REVIEW OIF' C+-+

'91

void ~WBlP (iIrE:>; in!.:U.I Ilfun~tiol'l prototyp~ClU't~O:::'~E.",,'l:6~ t\'lQ fl'QlIlb~~!i ; "~

cln~>r:n.l.m.1.";;, I'II.1ml;

!=.olJt":;:~"\n:8I,Bfgr,p- !:ll"'liIppl "g,: \nllnml: N'.:; T1 m.l, OQut<o:;;eodl <":::"HI.I]:!I:2: "'':<fllltI'J: t

:swap ~ nUll'll. nU,II12 J J '/fil.lOct ;LOn (JaIl

CCIU ~<,')'\r..\nilon;H: swappinq: \nlhUllJ: "«m,utll; ~out~<ennl~<A~um2: ~<~aum2~

'ijeli:.ch II; 11.L.~~e=~ .hI'!' monH o!"

I

II un~Lcn rl n1 v~ d swa~ ~_n~i B,

UtI: L~ til" .:1-10;

b t.~;

The output of program, 1.44 willi. be, :

Enter two numoors :47 2'9' Befo,re s'Ws:p,ping:

Nu'wl:: 4'1 NU'm2: ,29

After ,E;,wap,ph'llS; : ,Nu:ml : 29 Ngm2:47

R'e{enmce' ,argtUMti/!s'aifie' indic:atf!d bYllnampel'&and' ('&) befon "it z void, swa.p' '(int '&8;. :m.t ,&b); IJfUnction prow,type

y ou m1'l(Y (lmi'~ 'the names of the variablee inlt.he prototype on : 'void S;W!FlP (jut &. lot It.) ;

Also W10,t;e tha:t :no ampers~J!,(1 ,liign is requir,ed i:ofo:nctwn CDU :, ,s,'wap' (:rruuml~iIlI.um::n .:

In prog;r3m, 1.4:4, ttle vaJh.!.es, iQ the variahles,Dlun,l. 8lId DU.m2~n. the ,caUing pf.lograM =:If€! i.nte:rnba:oge.dJ. 11lis is shownbelow:

n:umi

o

"u,rn1

I 29' I

nl1m2

a b

(ALfler' e·lfOOliiltLtlf'i of swap ( ) )

Fli. :1 .• 19

C yr ollt rn ]'1 ria

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.