Professional Documents
Culture Documents
Computer Science
Computer Science
Computer Science
CHAPTER WISE
C$SE PATTER#
6*6
WHERE IS WHAT&
C&ap 1. Pro5rammin5 in c"" Structure OOP=s Concepts Pointers C&ap *. C'ass and Ob,ects# Constructor 2nd )estructor 2nd In&eritance C&ap . 2rrays /in0ed /ist + Stac0 1ueues C&ap 3 )2T2 4I/E :2 )/I G I C"" C&ap 7 )2T282SE CO CEPTS + S1/ C&ap 9 8OO/E2 2/GE8R2 C&ap ; CO%%! IC2TIO + CO%P!TER ET@OR(S
6.6
636
} 2 S *B!n'ess GC is pressed #pro5ram -i'' print DeEaminationF inCinite'y. 1 .B @i'' t&e Co''o-in5 pro5rams produce same outputA Program ' # include<iostream.h> # include<conio.h> void main() { int ),*"%; i$(()"*)+"#) cout<<)<<, ,<<*; getch(); } Program 2 # include<iostream.h> # include <conio.h> void main() { int ),*"#; i$(()"*"%)""%) cout<<)<<, ,<<*; getch(); } 13B@&at -ou'5Hd be contents oC Co''o-in5 aCter array initia'iIationA int 2J7KLM.#N #?O 2ns 3B 2 . N ? > > 17BSu55est stora5e c'ass Cor Co''o-in5 Pariab'es 1. a norma' Pariab'e. *. Pery &eaPi'y used Pariab'e. .. a Pariab'e t&at s&ou'd retain its Pa'ue aCter Cunction is oPer. 3. a Pariab'e t&at spanes mu'tip'e Ci'es. 7. a Pariab'e 5'oba' in one + not aPai'ab'e in anot&er Ci'e. 2ns 7B 1. *. .. 3. 7. auto re5ister static eEtern static 5'oba' 1 1 *
Q eac&
676
2ns 9B Pointer Pariab'e a'-ays store address oC a Pariab'e -&ic& is a'-ays an inte5er. So pointers a'-ays store inte5ers.
696
getch();
2ns.:
)e'&i%umbai )e'&i%umbai a5pur 1.. 4ind out errors in t&e Co''o-in5 pro5ram:6 class number { int )"%#; $loat *; number(){ )"*"%#;} ublic. number(number t) { )"t.); *"t.*; } 7 (){ cout<<,8b9ect destro*ed ,;} } main() { number a%, a3(a%); } 2ns.: error: int EL1>S RR c'ass member can not be initia'iIed in t&e c'ass. Constructor s&ou'd be dec'ared in pub'ic section oC c'ass. ReCerence operator is missin5 in t&e deCinition oC copy constructor In destructor c'ass name is missin5. Semico'on is missed aCter t&e deCinition oC c'ass. 1.3 @&at is t&e diCCerence bet-een nestin5 or containers&ip and in&eritanceA EEp'ain -it& eEamp'eA 2ns.: Containers&ip or estin5: @&en a c'ass contains ob,ect oC ot&er c'ass type as its data member is 0no-n as containers&ip or nestin5. In&eritance: In&eritance is t&e process oC creatin5 ne- c'ass by reusin5 t&e properties oC an eEistin5 c'ass by accessin5 t&em dependin5 on diCCerent Pisibi'ity mode. T&e ne- c'ass is ca''ed deriPed and eEistin5 c'ass is ca''ed base c'ass. 1.7 @&at -i'' be t&e output oC t&e pro5ramA #include<iostream.h> class base { ublic. void dis la*() { cout<<,:t is a base class ,<<endl; } }; class derived. ublic base { ublic. void dis la*() { cout<<,:t is a derived class ,<<endl;} }; main() {
6;6
derived ob%; ob%.dis la*(); } 2ns:6 T&e output -i'' be: It is a deri/ed class( 1.9 )eCine a c'ass named Tour in C"" -it& Co''o-in5 descriptionA 3 PriPate members: tcode inte5er (Ran5es 9 6 1>) adu'ts# c&i'dren# distance inte5er tota'Care C'oat 2ssi5n4are( ) 2 Cunction -&ic& ca'cu'ates and assi5n t&e Pa'ue to data member tota'Care as Co''o-s:6 0 1or adults 4are )istance Rs. 7>> BL17>> 2nd Care 5et reduced by *7T iC distance is U 17>>. 0 1or Children 4or ePery c&i'd a CiEed Rs. 7> is c&ar5ed as Care. Pub'ic members: 2 constructor -&ic& initia'iIed initia'iIe a'' data members -it& > 4unction EnterTour() to input t&e Pa'ues oC t&e data members tcode# adu'ts# c&i'dren and ca'' to 2ssi5n4are Cunction. 4unction S&o-Tour() to print a'' t&e detai's oC ob,ect oC TraPe' type. Ans( class tour { int tcode,adults,children,distance; $loat total$are; void assign$are() { $loat c$are";#, a$are"%;##; i$(distance<%;##) a$are"a$are<(a$are*3;=%##); total$are"(children*c$are)&(adults*a$are); } ublic. travel() { tcode"adults"children"distance"total$are"#; } void entertour() { do { cout<<,>nter tcode bet'een ?<%# ,; cin>>tcode; i$ (tcode<? @@ tcode>%#) cout<<,:nvalid tcode ,<<endl; }'hile(tcode<? @@ tcode>%#); cout<<,>nter children, adults, distance,; cin>>children>>adults>>distance; assign$are(); } void sho'tour() { cout<<,tcode.,<<tcode<<endl; cout<<,children.,<<children<<endl;
6N6
} }; 1.;. )eCine a c'ass named 2dmission in C"" -it& Co''o-in5 descriptionA 3 Pri/ate mem-ers: admno inte5er (Ran5es 1>617>>) name strin5 oC *> c&aracters c's inte5er Cees C'oat Pu-lic mem-ers: 2 constructor -&ic& initia'iIed admno -it& 1># name -it& D !//F# c's -it& > + Cees -it& > 4unction 5etdata() to read t&e ob,ect oC 2dmission type. 4unction putdata() to print t&e detai's oC ob,ect oC admission type. 4unction dra-Vnos() to 5enerate t&e admission no. random'y to matc& -it& admno and disp'ay t&e detai' oC ob,ect. Ans(2 class admission { int admno; char name/3#0; int cls; $loat $ees; ublic. admission() { admno"%#; strc *(name,,6ABB,); cls"#; $ees"#; } void getdata() { do { cout<<,>nter admno bet'een %#<%;## ,; cin>>admn i$ (admno<%# @@ admno>%;##) cout<<,:nvalid admission no +,<<endl; }'hile(admno<%# @@admno>%;##); cout<<,>nter name ,; gets(name); cout<<,>nter class and $ees ,; cin>>cls>>$ees; } void utdata() { cout<<,Cdmno .,<<admno<<endl; cout<<,6ame .,<<name<<endl; cout<<,Dlass .,<<cls<<endl; cout<<,Eees .,<<$ees<<endl; } void dra'Fnos() { int num; randomi-e();
6?6
} }; 1.N Dlass testmeout { int rollno; ublic. 7testmeout() ==Eunction % { cout<<rollno<<( is Beaving e)amination hall(<<endl; } testmeout() ==Eunction 3 { rollno"%; cout<<rollno<<( is a earing $or e)amination H<<endl; } testmeout(int n, char name/0) ==Eunction 5 { rollno"n; cout<<name<<( is in e)amination hall(<<endl; } testmeout(testmeout 1 t);==$unction 4 void m*'or!() ==Eunction ; { cout<<rollno<<( is attem ting questions H<<endl; } }; i) In ob,ect oriented pro5rammin5# -&at is 4unction 1 reCerred as and -&en does it 5et inPo0edA ii) In ob,ect oriented pro5rammin5# -&at is 4unction * reCerred as and -&en does it 5et inPo0edA iii) In ob,ect oriented pro5rammin5# -&at is 4unction . reCerred as and -&en does it 5et inPo0edA iP) @rite a statement so t&at Cunction . 5ets eEecutedA Comp'ete t&e deCinition oC Cunction 3 P) @&at -i'' be t&e output oC t&e aboPe code iC its main Cunction deCinition is as 5iPen be'o- (assumed t&e deCinition oC 4unction 3 is comp'eted ) : main() Mtestmeout ob1S ob1.my-or0()S O Pi) @&ic& Ceature oC ob,ect oriented pro5rammin5 is demonstrated usin5 4unction *# 4unction . and 4unction 3 in t&e aboPe c'ass testmeoutA Pii) @&at is t&e scope oC data member (ro''no) oC c'ass testmeoutA @&at does t&e scope oC data members depend uponA 2ns:6 i) It is reCerred as destructor. It is automatica''y inPo0ed -&en an ob,ect oC concerned c'ass 5oes out oC scope. ii) It is reCerred as constructor. It is automatica''y inPo0ed -&en an ob,ect oC concerned c'ass is dec'ared R created. iii) It is parameteriIed constructor and 5ets inPo0ed -&en an ob,ect oC concerned c'ass is created R dec'ared -it& t&e matc&ed parameters. iP) testmeout ob1(17# DVic0yF)S testmeout (testmeout + t) M ro''noLt.ro''noSO P) output -i'' be : 1 is appearin5 Cor eEamination
6 1> 6
1 is attemptin5 Wuestions 1 is /eaPin5 eEamination &a'' Pi) It is constructor oPer'oadin5. It s&o-s Po'ymorp&ism Ceature oC t&e OOP. Pii) T&e ro''no member oC ob,ect can on'y be used by t&e concerned ob,ect -&ere t&at ob,ect is dec'ared. Its scope basica''y depends upon t&e concerned ob,ect.
6 11 6
end " mid<%; } return (( os""<%)Q #.%); } 1. . @rite a Cunction in C"" -&ic& accepts an inte5er array and its siIe as ar5uments R assi5n t&e e'ements into a t-o dimensiona' array oC inte5ers in t&e Co''o-in5 Cormat : IC t&e array is 1# *#.#3#7#9 IC t&e array is 1#*#. T&e resu'tant *) array is 5iPen be'o1 * . 3 7 9 1 * . 3 7 > 1 * . 3 > > 1 * . > > > 1 * > > > > 1 > > > > > T&e resu'tant *) array is 5iPen be'o1 * . 1 * > 1 > > parameters and
else
So'ution : void $unc(int arr/0, int si-e) { int a3/3#0/3#0, i, 9; $or (i"#;i<si-e; i&&) { $or (9"#;9<si-e;9&&) { i$ ((i&9) >"si-e) a3/i0/90"#; else a3/i0/90" arr/90; cout<<a3/i0/90<<( H; } Dout<<(Ln(; } } 163 @rite a Cunction in C"" to perCorm a P!S: operations on a dynamica''y a''ocated stac0 containin5 rea' numberA 2ns6 struct 6ode { $loat data; 6ode * ne)t; }; Roid ush (6ode*Mo , $loat num) { 6ode*n tr " ne' 6ode; n tr <> data " num; n tr <> ne)t " 6ABB; i$(Mo "" 6ABB) Mo " n tr; else { n tr <> ne)t " Mo ; Mo " n tr; }
6 1* 6
167 Eac& node oC a ST2C( containin5 t&e Co''o-in5 inCormation# in addition to reWuired pointer Cie'd: Ro'' no. oC t&e student 25e oC t&e student. GPe t&e structure oC node Cor t&e 'in0ed stac0 in Wuestion. TOP is a pointer to t&e topmost node oC t&e ST2C(. @rite t&e Co''o-in5 Cunction: P!S:() $ TO pus& a node in to t&e stac0 -&ic& is a''ocated dynamica''y. POP() $ Te remoPe a node Crom t&e stac0 and to re'ease t&e memory. 2ns6 struct SMCDT { int rollno, age; SMCDT*ne)t; } *to , *n tr, * tr; void o () { i$ (+ o ) { cout << (LnAnder$lo'++( ; e)it(%); else { cout << ULnU << to <> rollno << ULtU << to <> age; tr " to ; to " to <> ne)t; delete tr; } } Roid ush() { n tr " ne' stac!; ==allocate memor* cout << HLn >nter roll number and age to be inserted . H ; cin >> n tr<> rollno >> n tr<>age ; n tr <> ne)t " 6ABB; i$ (+to ) to " n tr; else { tr <> ne)t " to ; to " n tr } }
1.9 @rite a Cunction %2X in C"" -&ic& -i'' return t&e /ar5est number stored in a t-o dimensiona' array oC Inte5ers. 2ns #include <iostream.h> #include <conio.h> const r " %##, c " %##; == Eunction to $ind the largest integer in a t'o<dimensional arra* int 4CV(int a/r0/c0, int m, int n)
6 1. 6
} void main() { clrscr(); int ar/r0/c0; int rr, cc, m) " #; int i, 9; cout << ,>nter no. o$ ro' . ,; cin >> rr; cout << ,>nter no. o$ column . ,; cin >> cc; cout << ,>nter the arra* elements . ,; $or (i"#; i<rr; i&&) $or (9 " #; 9<cc; 9&&) cin >> ar/i0/90; m) " 4CV(ar, rr, cc); cout << ,Bargest element is . , << ma); } 1.; @rite a Cunction in c"" -&ic& accepts a *) array oC inte5ers and its siIe as ar5uments and disp'ays t&e e'ements -&ic& 'ies on dia5ona's. J 2ssumin5 t&e*) array to be a sWuare matriE -it& odd dimensions # i.e .E.# 7E7#;E;# etc K EEamp'e iC t&e array content is 73. 9;N 1*? Output t&rou5& t&e Cunction s&ou'd be )ia5ona' one : 7 ; ? )ia5ona' t-o : . ; 1 . 2ns RR 4unction to disp'ay t&e e'ements -&ic& 'ie on dia5ona's #include <stdio.h> #include <iostream.h> #include <conio.h> const 4 " %#; const 6 " %#; void dis la*Fdiagonals(int 4CMW:V/40/60, int r, int c) {
int ma) " #; $or(int i" #;i<m;i&&) $or(int 9" #;9<n;9&&) { i$ (a/i0/90 >ma)) ma) " a/i0/90; } return ma);
6 13 6
} void main() { int 4CMW:V/40/60; int i, 9; int r, c; cout << ,>nter total no. o$ ro's. ,; cin >> r; cout << ,>nter total no. o$ columns. ,; cin >> c; i$ ((r "" c) 11 ((rJ3""%) 11 (cJ3""%))) { cout << ,:n ut ste s,; cout << ,LnL>nter the element in the arra*Ln,; $or(i"#; i<r; i&&) $or(9"#; 9<c; 9&&) { cin >> 4CMW:V/i0/90; } } else return; dis la*Fdiagonals(4CMW:V, r, c); }
clrscr(); == Einding the diagonal $rom le$t inde) to right cout << ,2iagonal 8ne . ,; $or(int i"#; i<r; i&&) $or(int 9"#; 9<c; 9&&) { cout << 4CMW:V/i0/90 << , ,; i&&; } cout << endl; == Einding the diagonal $rom right inde) to le$t cout << ,2iagonal M'o . ,; $or(i"#; i<"r; i&&) { $or(int 9"c<%; 9>"#; 9<<) { cout << 4CMW:V/i0/90 << , ,; i&&; } } getch();
6 17 6
1.N @rite a Cunction in C"" -&ic& accepts a *) array oC inte5ers and its siIe as ar5uments and disp'ays t&e e'ements oC t&e midd'e ro- and t&e e'ements oC midd'e co'umn. EEamp'e iC t&e array content is .73 ;9? *1N Output t&rou5& t&e Cunction s&ou'd be: %idd'e ro-: ;9? %idd'e co'umn: 7 9 1 Ans == Eunction to dis la* the elements 'hich lie on middle o$ ro' and column #include <stdio.h> #include <iostream.h> #include <conio.h> const 4 " %#; const 6 " %#; void dis la*FWo'Dol(int Crra*/40/60, int r, int c) { int ro' " r = 3; int col " c = 3; == Einding the middle ro' cout << ,4iddle Wo' . ,; $or(int 9"#; 9<c; 9&&) cout << Crra*/ro'0/90 << , ,; cout << endl; == Einding the middle column cout << ,4iddle Dolumn . ,; $or(9"#; 9<c; 9&&) cout << Crra*/90/col0 << , ,; getch(); } void main() { int Crra*/40/60; int i, 9; int r, c; cout << ,>nter total no. o$ ro's. ,; cin >> r; cout << ,>nter total no. o$ columns. ,; cin >> c; i$ ((r "" c) 11 ((rJ3""%) 11 (cJ3""%))) { cout << ,:n ut ste s,; cout << ,LnL>nter the element in the arra*Ln,; $or(i"#; i<r; i&&) $or(9"#; 9<c; 9&&) { cin >> Crra*/i0/90; } } else { cout << ,:n ut ro' and column not valid,;
6 19 6
getch(); return; } dis la*FWo'Dol(Crra*, r, c); } 1. ?. )ec'are a stac0 usin5 array t&at contains int type numbers and deCine pop and pus& Cunction usin5 C"" SyntaE. 2ns #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ct* e.h> #de$ine 4CV %## == Sho's ma)imum arra* length int stac!/4CV0; == 2eclares arra* global variable int to ; == 2eclares integer to == Eunction rotot* es o$ add stac!, delete stac!, and == sho' stac! in arra* im lementation void ush(int stac!/0, int val, int 1to ); == Cdd stac! int o (int stac!/0, int 1to ); == 2elete stac! void sho'FStac!(int stac!/0, int to ); == Sho' stac! void main() { int choice, val; char o t " XYX; == Mo continue the do loo in case to " <%; == :nitiali-ation o$ Zueue clrscr(); do { cout << ,LnLtLt 4ain 4enu,; cout << ,LnLt%. Cddition o$ Stac!,; cout << ,LnLt3. 2eletion $rom Stac!,; cout << ,LnLt5. Mraverse o$ Stac!,; cout << ,LnLt4. >)it $rom 4enu,; cout << ,LnLn>nter *our choice $rom above <> ,; cin >> choice; s'itch (choice) { case %. do { cout << ,>nter the value to be added in the stac! ,; cin >> val; ush(stac!, val, to ); cout <<,Ln2o *ou 'ant to add more elements <Y=6> Q ,; cin >> o t; } 'hile (tou er(o t) "" XYX); brea!; case 3. o t " XYX; == :nitiali-e $or the second loo do { val " o (stac!, to );
6 1; 6
i$ (val +" <%) cout << ,Ralue deleted $rom statc! is , << val; cout <<,Ln2o *ou 'ant to delete more elements<Y=6>Q,; cin >> o t; } 'hile (tou er(o t) "" XYX); brea!; case 5. sho'FStac!(stac!, to ); brea!; case 4. e)it(#);
} 'hile (choice +" 4); } == Eunction bod* $or add stac! 'ith arra* void ush(int stac!/0, int val, int 1to ) { i$ (to "" 4CV < %) { cout << ,Stac! Eull ,; } else { to " to & %; stac!/to 0 " val; } } == Eunction bod* $or delete stac! 'ith arra* int o (int stac!/0, int 1to ) { int value; i$ (to < #) { cout << ,Stac! >m t* ,; value " <%; } else { value " stac!/to 0; to " to < %; } return (value); } == Eunction bod* $or sho' stac! 'ith arra* void sho'FStac!(int stac!/0, int to ) { int i; i$ (to < #) { cout << ,Stac! >m t*,; return; } i " to ; clrscr(); cout << ,Mhe values are ,;
6 1N 6
do {
cout << ,Ln, << stac!/i0; i " i < %; }'hile(i >" #); } 1.1>. )eCine Cunctionstac0pus&( ) to insert nodes and stac0 pops ( ) to de'ete nodes . Cor a 'in0ed 'ist imp'emented stac0 &aPin5 t&e Co''o-in5 structure Cor eac& node struct 6ode { Dhar name / 3# 0 :nt age ; 6ode * lin! ; }; Dlass stuc! { 6ode * to ; Public Stac! ( ) { to " null ;} ; Roid stac! ush ( ); Roid stac! o ( ) ; } 2ns #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ct* e.h> == 2eclares a stac! structure struct node { char name/3#0; int age; node *lin!; }; class stac! { node *to ; ublic . stac!() { to " 6ABB; } void stac! ush(); == Cdd stac! void stac! o (); == 2elete stac! void sho'FStac!(); == Sho' stac! }; == Eunction bod* $or adds stac! elements void stac!..stac! ush() { int val; node *tem ; tem " ne' node; cout << ,>nter name . ,; gets(tem <>name);
6 1? 6
cout << ,>nter age . ,; cin >> tem <>age; tem <>lin! " 6ABB; i$(to ""6ABB) to " tem ; else { tem <>lin! " to ; to " tem ; } } == Eunction bod* $or delete stac! elements void stac!..stac! o () { node *tem ; i$ (to "" 6ABB) { cout << ,Stac! >m t* ,; } else { tem " to ; to " to <>lin!; tem <>lin! " 6ABB; delete tem ; } } == Eunction bod* $or sho' stac! elements void stac! .. sho'FStac!() { node *tem ; tem " to ; clrscr(); cout << ,Mhe values are Ln,; 'hile (tem +" 6ABB) { cout << ,Ln, << tem <>name << ,Lt, << tem <>age; tem " tem <>lin!; } } == 4ain rogramming logic void main() { int choice; stac! SMCDT; char o t " XYX; == Mo continue the do loo clrscr(); do { cout << ,LnLtLt 4ain 4enu,;
in case
6 *> 6
cout << ,LnLt%. Cddition o$ Stac!,; cout << ,LnLt3. 2eletion $rom Stac!,; cout << ,LnLt5. Mraverse o$ Stac!,; cout << ,LnLt4. >)it $rom 4enu,; cout << ,LnLn>nter *our choice $rom above ,; cin >> choice; s'itch (choice) { case %. do { SMCDT.stac! ush(); cout<<,2o *ou 'ant to add more elements<Y=6>Q,; cin >> o t; } 'hile (tou er(o t) "" XYX); brea!; case 3. o t " XYX; == :nitiali-e $or the second loo do { SMCDT.stac! o (); cout<<,2o *ou 'ant to delete more element<Y=6>Q,; cin >> o t; } 'hile (tou er(o t) "" XYX); brea!; case 5. SMCDT.sho'FStac!(); brea!; case 4. e)it(#); }
6 *1 6
6 ** 6
} 1.. @rite a c"" pro5ram #-&ic& initia'iIes a strin5 Pariab'e to t&e content.FTime is a 5rat teac&er but unCortunate'y it 0i''s a'' its pupi's.8er'ioIFand output t&e strin5 one c&aracter at a time to t&e dis0 Ci'e O!T.TXT .You &aPe to inc'ude a'' t&e &eader Ci'es reWuired. 2ns-er: #include<$stream.h> :nt main() { o$stream $out(H8AM.MVM(); Dhar*str"( Mime is a grat teacher but un$ortunatel* it !ills all its u ils.Ierlio-(; :nt i"#; :$(+$out) { cout<<(Eile cannot be o ened H; return #; } Khile (str/i0+"UL#U) { $out<<str/i0; i&&; } $out.close(); } 13. @rite a pro5ram t&at disp'ay t&e siIe oC a Ci'e in bytes. 2ns-er: #include<iostream.h> #include<$stream.h> #include< rocess.h> #include<conio.h> int main() { char $ilename/%50; clrscr(); cout<(>nter Eilename.Ln(; cin.getline($ilename,%5); i$stream in$ile($ilename); i$(+in$ile) { cout>>(sorr* + Dan not o en H<<$ilename <<($ileLn(; >)it(<%); } int noFb*tes"#; char ch; 'hile(cin.get(ch))
6 *. 6
noFb*tes &&; } cout<<(Eile Si-e is(<<noFb*tes<<(b*tesLn(; return #; } 17. @&at -i'' be t&e output produced by t&e Co''o-in5 codeA 2ns-er: #include<iostream.h> #include<$stream.h> #include< rocess.h> #include<conio.h> int main() { clrscr() char $ilename/%50; cout<<(>nter Eilename.(; cin.getline($ilename,%5); i$stream in($ilename); i$(+in) {cout<<(Dannot o en in ut $ile+Ln(; return (#) } Dhar str/3;;0; Khile(in) {in.getline(str,3;;); Dout<<str<<(Ln(; } in.close(); return #; }
6 *3 6
2ns )e5ree: T&e number oC attributes in a re'ation determines t&e de5ree oC a re'ation. 2 re'ation &aPin5 . attributes is said to be a re'ation oC de5ree ..
Cardina'ity: T&e number oC ro-s in a re'ation is 0no-n as Cardina'ity. 1.3. @&at is t&e main Cunction oC )82. 2ns: T&e )82 must be a mana5er# more t&an a tec&nician6see0in5 to meet t&e needs oC peop'e -&o use t&e data. Since many user may s&are t&e same data resource# t&e )82 must be prepared to meet t&e need and ob,ectiPe. 1.7. @rite a Wuery on t&e customers tab'e -&ose output -i'' eEc'ude a'' customers -it& a ratin5 UL1>># un'ess t&ey are 'ocated in S&im'a. 2ns. SE/ECT Z 4RO% customers @:ERE ratin5 B1>> OR city L=S&im'a= S 1.9. 2ns. 1.;. 2ns. 1.N. @rite a Wuery t&at se'ects a'' orders eEcept t&ose Ieros or !//s in t&e amount Cie'd. SE/ECT Z 4RO% Orders @:ERE amt U B> 2 ) (amt IS OT !//) S @rite a Wuery t&at 'ists customers in descendin5 order oC ratin5. Output t&e ratin5 Cie'd Cirst# Co''o-ed by t&e customer=s name and number. SE/ECT ratin5# cust6name# cust6num 4RO% customers OR)ER 8Y ratin5 )ESC S
@rite a command t&at puts t&e Co''o-in5 Pa'ues# in t&eir 5iPen order# into t&e sa'esman tab'e: cust6name6%anis&a# city6%ana'i# comm.6 !//# cust6num61?>1. 2ns. I SERT I TO sa'esman (city# cust6name# comm.#cust6num) V2/!ES([%anis&a=# !//#1?>1) S 1.?. @&at are ))/ and )%/A 2ns:6 T&e ))/ proPides statements Cor t&e creation and de'etion oC tab'es and indeEes. T&e )%/ proPides statements to enter# update# de'ete data and perCorm comp'eE Wueries on t&ese tab'es. 1.1>. 2ns: 1.11. @&at is t&e diCCerence bet-een @&ere and :aPin5 C'ause A T&e :2VI G c'ause p'aces t&e condition on 5roup but @:ERE c'ause p'aces t&e condition on indiPidua' ro-s @&at do you understand by constraints A
6 *7 6
2ns: tab'e.
Constraints are used to enCorce ru'es at tab'e 'ePe' -&en ePer ro- is inserted# updatedRde'eted Crom
Constraints can be deCined to one oC t&e T-o 'ePe'. Co'umn /ePe': ReCerence to a sin5'e co'umn. can be deCined any type oC inte5rity. Tab'e /ePe': ReCerences one or more co'umns and is deCined separate'y Crom deCinition oC t&e co'umns in t&e tab'e. 1.1*. @rite some Ceatures oC S1/A 2ns: RecoPery ad Concurrency:6 Concurrency is concerned -it& t&e manner in -&ic& mu'tip'e user operate upon t&e )atabase. Security: T&e Security can be maintained by Pie- mec&anism. Inte5rity Constraints6B Inte5rity constraints are enCorced by t&e system. 1.1.. @rite Parious database ob,ects aPai'ab'e in S1/A 2ns: Tab'e: 2 Tab'e is used to store )ata
Vie-: 2 Pie- is t&e temporary tab'e created usin5 Ori5ina' tab'e. SeWuence: SeWuences are used to 5enerate Primary 0ey Pa'ue.
IndeE: T&ey are used to improPe Wueries. Synonym: T&ey 5iPe a'ternatiPe names to ob,ects. 1.13. @rite t&e ru'es to name an ob,ectsA 2ns : T&e maEimum 'en5t& must be .> c&aracter 'on5. T&e Ob,ect name s&ou'd not contain Wuotation mar0. T&e name must start -it& 'etter. T&e use oC \ and ] is discoura5ed in t&e ob,ect name. 2 name must not be a reserPed name. 1.17. @&at are 5roup 4unctions 2ns: T&e a55re5ate Cunctions are 5roup Cunctions. T&ey return resu't based on 5roups oC ro-s. T&e 5roup Cunctions are 2VG()# CO! T()# %2X()# %I ()# S!%() 1.19. 2ns: @&at are co'umn a'iasA In many cases &eadin5 tab'e may not be descriptiPe and &ence it diCCicu't to understand. In suc& case -e use co'umns a'ias It -i'' c&an5e co'umn &eadin5 -it& co'umn a'ias.
6 *9 6
1.1;.
Table : Book $ook8id $ook name C>>>1 4ast Coo0 4>>>1 T>>>1 T>>>* 4>>>* Table : issued $ook8Id T999' C999' 1999'
Author8name /ata (apoor @i''iam T&e Tears :op0ins 8rain + %y 4irst c"" 8roo0e C"" 8rain 2.@. -or0s Rossaine T&underbo'ts 2nna Roberts
T4pe !uantit4 Coo0ery 7 4iction TeEt TeEt 4iction *> 1> 17 7>
@rite S1/ Wuery Cor (a) to (C) (a) To s&o- boo0 name# 2ut&or name and price oC boo0s oC 4irst Pub. Pub'is&er (b) To 'ist t&e names Crom boo0s oC teEt type (c) To )isp'ay t&e names and price Crom boo0s in ascendin5 order oC t&eir prices. (d) To increase t&e price oC a'' boo0s oC EP8 pub'is&ers by 7>. (e) To disp'ay t&e 8oo0VId# 8oo0Vname and Wuantity issued Cor a'' boo0s -&ic& &aPe been issued (C) To insert a ne- ro- in t&e tab'e issued &aPin5 t&e Co''o-in5 data. [4>>>.=# 1 (5) GiPe t&e output oC t&e Co''o-in5 i. Se'ect Count(Z) Crom 8oo0s ii. Se'ect %aE(Price) Crom boo0s -&ere Wuantity BL17 iii. Se'ect boo0Vname# aut&orVname Crom boo0s -&ere pub'is&ersL=Cirst pub'.= iP. Se'ect count(distinct pub'is&ers) Crom boo0s -&ere PriceBL3>> Ans2 (a) Se'ect boo0Vname# aut&orVname # price Crom boo0s -&ere pub'is&erL=4irst Pub'= (b) Se'ect boo0Vname Crom boo0s -&ere typeL=TeEt= (c) Se'ect boo0Vname# price Crom boo0s Order by PriceS (d) !pdate boo0s set priceLprice"7> -&ere pub'is&ersL=EP8= (e) Se'ect a.boo0Vid#a.boo0Vname#b.WuantityVissued Crom boo0s a# issued b -&ere a.boo0VidLb.boo0Vid (C) Insert into issued Va'ues ([4>>>.=#1)S
6 *; 6
S(#O
1 * . 3 7 9 ; N ? 1>
#A>E
(2R2 )I@2(2R )IVY2 RE(:2 2R<! S28I 2 <O: RO8ERT R!8I 2 VI(2S
STIPE#%
3>> 37> .>> .7> 7>> 3>> *7> 37> 7>> 3>>
S"$?ECT
P:YSICS CO%P. Sc. C:E%ISTRY P:YSICS %2T:S CE:%ISTRY P:YSICS %2T:S CO%P. Sc. %2T:S
A)ERA6E
9N 9N 9* 9. ;> 77 93 9N 9* 7;
%I)(
I I I I I II I I I II
/ist t&e names oC t&ose students -&o &aPe obtained %I) I sorted by 2%E. )isp'ay a report# 'istin5 2%E# STIPE )# S!8<ECT and amount oC stipend receiPed in a year assumin5 t&at t&e STIPE ) is paid ePery mont&. To count t&e number oC students -&o are eit&er P:YSICS or CO%P!TER SC 5raduates. To insert a ne- ro- in t&e GR2)!2TE tab'e: 11#F(2<O/F# .>># DCO%P. SC.F# ;7# 1 GiPe t&e output oC Co''o-in5 sW' statement based on tab'e GR2)!2TE: (i) Se'ect %I (2VER2GE) Crom GR2)!2TE -&ere S!8<ECTLFP:YSICSFS (ii) Se'ect S!%(STIPE )) Crom GR2)!2TE @:ERE diPL*S (iii) Se'ect 2VG(STIPE )) Crom GR2)!2TE -&ere 2VER2GEBL97S (iP) Se'ect CO! T(distinct S!8)<ECT) Crom GR2)!2TES 2ssume t&at t&ere is one more tab'e G!I)E in t&e database as s&o-n be'o-:
Ta-le2 6"I%E
>AI#AREA
P:YSICS CO%P!TER SC C:E%ISTRY %2T:E%2TICS
A%)ISOR
VI O) 2/O( R2<2 %2:ES:
@&at -i'' be t&e output oC t&e Co''o-in5 Wuery: SE/ECT 2%E# 2)VISOR 4RO% GR2)!2TE#G!I)E @:ERE S!8<ECTL %2I 2RE2S
SE/ECT 2%E 4RO% GR2)!2TE @:ERE )IVL^I^ OR)ER 8Y 2%ES SE/ECT 2%E# STIPE )# S!8<ECT# STIPE )Z1* STIPE )VYE2R 4RO% GR2)!2TES SE/ECT S!8<ECT# CO! T( 2%E) 4RO% GR2)!2TE GRO!P8Y (S!8<ECT) S!8<ECTL^P:YSICS^ OR S!8<ECTL^CO%P. Sc.^S
:2VI G
6 *N 6
(d)
(e)
I SERT I TO GR2)!2TE V2/!ES(11#^(2<O/^#.>>#^CO%P. Sc.^#;7#1)S (i) %I (2VER2GE) 9. (ii) S!%(STIPE )) N>> (iii) 2VG(STIPE )) 3*> (iP) CO! T()ISTI CTS!8<ECT) 3
(C) SE/ECT 2%E# 2)VISOR 4RO% GR2)!2TE# G!I)E @:ERE S!8<ECTL%2I 2RE2S #A>E A%)ISOR
)IVY2 S28I 2 (2R2 RE(:2 <O: R2<2 R2<2 VI O) VI O) VI O)
5astname
(umar @a'tor Tones 2c0erman Sen5upta Samue' Tondon /ee T&ompson
Address
Ra, na5ar Gand&i na5ar .. E'm St. 33> !.S. 11> *34riends street ? 4iCt& Cross S&astri a5ar 1*1 :arrison St. 11 Red Road
Cit4
G_8 G_8 Paris !pton e- )e'&i @as&in5ton G_8 e- Yor0 Paris
Salar4
;7>>> 97>>> N>>>> ;7>>> 7>>>> 37>>> 3>>>> .*>>> *N>>>
$eneAits
17>>> 17>>> *7>>> 1*7>> 1*>>> 1>>>> 1>>>> ;7>> ;7>>
%esignation
%ana5er %ana5er )irector %ana5er C'er0 C'er0 C'er0 Sa'esman sa'esman
@rite t&e S1/ commands Cor t&e Co''o-in5 : (i) To s&o- Cirstname#'astname#address and city oC a'' emp'oyees 'iPin5 in paris (ii) To disp'ay t&e content oC Emp'oyees tab'e in descendin5 order oC 4irstname. (iii) To disp'ay t&e Cirstname#'astname and tota' sa'ary oC a'' mana5ers Crom t&e tab'es Emp'oyee and empsa'ary # -&ere tota' sa'ary is ca'cu'ated as sa'ary"beneCits. (iP) To disp'ay t&e maEimum sa'ary amon5 mana5ers and c'er0s Crom t&e tab'e Empsa'ary. (/ 6i/e the Output oA Aollo7ing S!5 commands2
(i) Se'ect Cirstname#sa'ary Crom emp'oyees #empsa'ary -&ere desi5nation L [Sa'esman= and Emp'oyees.empidLEmpsa'ary.empidS (ii) Se'ect count(distinct desi5nation) Crom empsa'aryS (iii) Se'ect desi5nation# sum(sa'ary) Crom empsa'ary 5roup by desi5nation &aPin5 count(Z) B*S (iP) Se'ect sum(beneCits) Crom empsa'ary -&ere desi5nation L=C'er0=S (i) se'ect Cirstname# 'astname# address# city Crom emp'oyees -&ere cityL^Paris^S (ii) se'ect Z Crom emp'oyees order by Cirstname descS (iii) se'ect emp'oyees.Cirstname# emp'oyees.'astname# empsa'ary.sa'ary " empsa'ary.beneCits tota'Vsa'ary Crom emp'oyees# empsa'ary -&ere emp'oyees.empidLempsa'ary.empid and desi5nationL^%ana5er^S (iP) se'ect maE(sa'ary) Crom empsa'ary -&ere desi5nation in(^%ana5er^#^C'er0^)
2ns:
6 *? 6
(P)
(i) se'ect Cirstname# sa'ary Crom emp'oyees# empsa'ary -&ere desi5nationL^Sa'esman^ and emp'oyees.empidLempsa'ary.empid 4IRST 2%E S2/2RY Rac&e' .*>>> Peter *N>>> (ii)se'ect count(distinct desi5nation) Crom empsa'aryS CO! T()ISTI CT)ESIG 2TIO ) 3 (iii)se'ect desi5nation# sum(sa'ary) Crom empsa'ary 5roup by desi5nation &aPin5 count(Z)B*S )ESIG 2TIO S!%(S2/2RY) C'er0 1.7>>> %ana5er *17>>> (iP)se'ect sum(beneCits) Crom empsa'ary -&ere desi5nationL^C'er0^S S!%(8E E4ITS) .*>>>
$oolean Alge-ra
'( Pro/e that X((X*B CX -4 alge-raic method( So'ution: /.:.S.L X.(X"Y)LX .X " X .Y L X " X .Y L X .(1"Y) LX . 1 L X L R.:.S <( 6i/e duals Aor the Aollo7ing 2 a A* D$ - A$*D$ So'ution: a) 2. (` "8) b) (2 " 8). (` " 8) E( State and /eriA4 In/olution la7( So'ution: InPo'ution /a- states : ` L 2 Trut& Tab'e: 2 > 1 ` 1 > ` > 1
:( State and /eriA4 %ualit4 principle( So'ution: Princip'e oC dua'ity states t&at Crom ePery boo'ean re'ation# anot&er boo'ean rea'ation can be deriPed by (i) c&an5in5 eac& OR si5n(") to an 2 ) si5n(6). (ii) c&an5in5 eac& 2 ) si5n(6) to an OR si5n(") (iii) rep'acin5 eac& 1 by > and eac& > by 1. T&e ne- deriPed re'ation is 0no-n as t&e dua' oC t&e ori5ina' re'ation. )ua' oC 2" `8 -i'' be
6 .> 6
2" `8 L 2. (` "8). ;( State and /eriA4 A-sorption la7 in -oolean alge-ra. So'ution: 2bsorption 'a- states: (i) X " XYL X (ii) X ( X " Y) L X Input X > 1 1 1 Y > 1 > 1 Output X " XY > > 1 1
F( %ra7 logic circuit diagram Aor the Aollo7ing eGpression2 BC A$*$C*CD So'ution: a b Y
2 )
2 )
c
2 )
H( State t&e distributiPe 'a-s oC boo'ean a'5ebra. So'n: )istributiPe 'a-s states: (i) X(Y"_)LXY"X_ (ii) X"Y_L(X"Y)(X"_) =( Reduce the Aollo7ing $oolean eGpression using I0>ap2 4(P#1#R#S)La(>#.#7#9#;#11#1*#17) So'n: R=S= R=S RS RS=
6 .1 6
1 . 1 ; 1 17 1 11 * 1 9 13 1>
T&is is 1 Wuad# *airs + * 'oc0 1uad(m."m;"m17"m11) reduces to RS Pair(m7"m;) reduces to P=1S Pair (m;"m9) reduces to P=1R 8'oc0 m>LP=1=R=S= %1*LP1R=S= &ence t&e Cina' eEpressions is 4LRS " P=1S " P=1R " P1R=S= " P=1=R=S=
@( Reduce the Aollo7ing $oolean eGpression using I0>ap2 4(2#8#C#))Lb(>#1#.#7#9#;#1>#13#17) So'n: > > > > > > > > > Reduced eEpressions are as Co''o-s:
6 .* 6
4or pair 1# (2"8"C) 4or pair *# (2="C=")) 4or 1uad 1# (2")=) 4or 1uad *# (8="C=) :ence Cina' POS eEpression -i'' be Y(2#8#C#))L (2"8"C) (2"C")) (2")) (8"C)
6 .. 6
Ans. 2 5ate-ay operates at t&e upper 'ePe's oC t&e OSI mode' and trans'ates inCormation bet-een t-o comp'ete'y diCCerent net-or0 arc&itectures. Routers a''o- diCCerent net-or0s to communicate -it& eac& ot&er. T&ey Cor-ard pac0ets Crom one net-or0 to anot&er based on net-or0 'ayer inCormation. 2 5ate-ay can interpret and trans'ate t&e diCCerent protoco's t&at are used on t-o distinct net-or0s. !n'i0e routers t&at successCu''y connect net-or0s -it& protoco's t&at are simi'ar# a 5ate-ay perCorm an app'ication 'ayer conPersion oC inCormation Crom one protoco' stac0 to anot&er. !(; @&at is t&e ro'e oC net-or0 administratorA Ans. 8asic tas0s Cor -&ic& a net-or0 administrator may be responsib'e: Settin5 up and conCi5urin5 net-or0 &ard-are and soCt-are. Insta''in5 and conCi5urin5 net-or0 media and connections. Connectin5 user nodes and perip&era's oC a'' 0inds to t&e net-or0. 2ddin5 users to and remoPin5 users Crom t&e net-or0. %ana5in5 user account. Ensurin5 t&e security oC t&e net-or0. ProPide trainin5 to t&e users to uti'iIe t&e net-or0=s resources. !(F @&at is t&e diCCerence bet-een baseband and broadband transmissionA Ans( 8aseband is a bi6directiona' transmission -&i'e broadband is a unidirectiona' transmission. o 4reWuency diPision mu'tip'eEin5 possib'e in base band but possib'e in broadband. S o 8aseband 8roadband 1 Entire band-idt& oC t&e cab'e is consumed broadband transmission# si5na's are sent on mu'tip'e by a si5na' CreWuencies# a''o-in5 mu'tip'e si5na's to be sent simu'taneous'y. * )i5ita' si5na's 2na'o5 si5na's . bi6directiona' transmission unidirectiona' transmission 3 o 4reWuency diPision mu'tip'eEin5 4reWuency diPision mu'tip'eEin5 possib'e possib'e 7 !ses Cor s&ort distance !ses Cor 'on5 distance
!(H @&at are t&e diCCerence bet-een domain and -or05roupA Ans( S o )omain @or05roup 1. One or more computers are serPers 2'' Computers are peers. *. IC you &aPe a user account on t&e domain# you Eac& computer &as a set oC accounts. can 'o5on to any computer on t&e domain. .. T&ere can be 1>>" computers Typica''y not more t&en *>6.> computers 3. T&e computers can be on diCCerent 'oca' net-or0 2'' computers must be on t&e same 'oca' netor0. !(= @&at is t&e diCCerences bet-een POP. and I%2P %ai' SerPerA Ans( I%2P is a standard protoco' Cor accessin5 e6mai' Crom a 'oca' serPer. 2 simp'er e6mai' protoco' is Post OCCice Protoco' . (POP.)# -&ic& do-n'oad mai' to t&e computer and does not maintain t&e mai' on t&e serPer. I%2P# e6mai's are stored on t&e serPer# -&i'e in POP.# t&e messa5es are transCerred to t&e c'ient=s computer -&en t&ey are read. !('9 ame diCCerent 'ayer oC t&e ISO OSI %ode'. Ans( Internationa' Standard Orr5anisation $ Open Systems Interconnection &as sePen 'ayersS P&ysica' /ayer
6 .3 6
)ata /in0 /ayer et-or0 /ayer Transport /ayer Session /ayer Presentation /ayer 2pp'ication /ayer !('' @&at is c'ient serPer arc&itectureA Ans( To desi5nated a particu'ar node -&ic& is -e'' 0no-n and CiEed address# to proPide a serPice to t&e net-or0 as a -&o'e. T&e node proPidin5 t&e serPice is 0no-n as t&e serPer and t&e nodes t&at use t&at serPices are ca''ed c'ients oC t&at serPer. T&is type oC net-or0 is ca''ed C'ient6SerPer 2rc&itecture. !('< @&at is 4)%A GiPe eEamp'e. Ans( 4)%64reWuency )iPision %u'tip'eEin5 is used in ana'o5 transmission. It is oCten used in s&ort distance. It is code transparent and any termina' oC t&e same speed can use t&e same sub6c&anne' aCter t&e sub6c&anne' is estab'is&ed. T&e best eEamp'e iC 4)% is t&e -ay -e receiPe Parious stations in a radio. !('E describe t&e Co''o-in5 in brieC: i) %OS2IC ii) !SE ET iii) @2IS Ans( i) %OS2IC: is t&e pro5ram Cor cruisin5 t&e internet. T&e ationa' centre -rote t&is pro5ram Cor Super Computer app'ication at t&e uniPersity oC I''inois. It &as a simp'e -indo- interCace# -&ic& creates useCu' &yperteEt 'in0s t&at automatica''y perCorm some oC t&e menu bar and button Cunctions. ii) !SE ET: is t&e -ay to meet peop'e and s&are inCormation. !senet ne-s5roup is a specia' 5roup set up by peop'e -&o -ant to s&are common interests ran5in5 Crom current topic to cu'tura' &erita5es. iii) @2IS: is a @I)E 2RE2 I 4OR%2TIO SERVER.
6 .7 6