Computer science Practical file

Prangav Singhal XII-d

Roll no.-03

I gratefully acknowledge the valuable contributions of all those who helped me to complete this practical file successfully. First of all, I would like to thank my Computer Science teacher, Mrs. Pooja Gupta, who gave this important topic, her invaluable guidance and made this practical file possible. Special thanks are due to my parents who helped me to coordinate the entire activity and provided necessary assistance.


This is to certify that this PRANGAV SINGHAL of XII-D of MOUNT CARMEL SCHOOL, DWARKA has completed this Practical File under my supervision and guidance. He has completed the Practical File, up to my satisfaction in the academic year 2013-14.

Date : Roll. No :

Mrs. Pooja Gupta (PGT Computer Science) Mount Carmel School


Inheritance: Extending Classes 5. Structures 2. Pointers 7. Classes And Objects 3. File Handling 6. Arrays 8. Constructors And Destructors 4. Linked Lists. Structured Query Language 4|Page . Stacks And Queues 9.INDEX 1.


h> struct Country { char countryname[20]. gets(c[i].percapitainc.h> #include<conio. cout<<”\nEnter Capital Name= “. char capname[20]. cin>>c[i]. cout<<”\nCapital Name is= “<<c[j]. #include<iostream. }. cout<<”\nEnter Country Name= ”.countryname. for (int i=0 . cout<<”\nEnter per capita income= “.j<3. gets(contname). } char contname[20].capname). } 6|Page . Country c[3]. cout<<”\nPer capita income is= “<<c*j+. cout<<”\nEnter country name to search “. gets(c[i].h> #include<string.countryname). for(int j=0. Write a program using a structure Country to store information of three countries comprising its Capital Name and Per Capita Income and display the information according to Country Name entered by the user.percapitainc.capname<<endl.contname)==0) . } } getch().h> #include<stdio.1. i++) . i<3 .j++) { if(strcmpi(c[j]. void main() { clrscr(). long unsigned int percapitainc.

OUTPUT: 7|Page .

minutes.minutes=(t1.seconds.seconds. cout<<endl<<"Enter hours: ".minutes+t2. Write a program in C++ to make a structure Time and to add two and to display them on screen and the total time.hours+t2. cout<<"\nEnter seconds: ". cin>>t.minutes+(t1.seconds)/60)%60. cout<<"\nEnter The First Time: ". cout<<"\nEnter Minutes: ".h> #include<conio.seconds)%60.seconds+t2. cin>>t2. cout<<endl<<"The total time is "<<t3.minutes+(t1. cin>>t1. t3. cin>>t1.seconds+t2.seconds=(t1.hours=t1. cin>>t1.t3. cin>>t2. t3.minutes+t2. time t1. cout<<"\nEnter seconds: ".2. cout<<endl<<"The first time is:"<<t1. cout<<endl<<"The second time is:"<<t2.hours+(t1.hours.minutes.seconds. int seconds.seconds.hours<<":"<<t3. void main() { clrscr().seconds+t2.h> struct time { int hours.minutes<<":"<<t3.hours<<":"<<t2.seconds. cout<<"\nEnter Minutes: ".hours. getch(). } 8|Page . t3.seconds)/60)/60. variables of structure Time #include<iostream.t2. int minutes. cout<<endl<<"Enter the second time: ".hours<<":"<<t1. cout<<endl<<"Enter Hours: ".minutes<<":"<<t1. }.minutes<<":"<<t2.

OUTPUT 9|Page .

3. int unit. Compute the electricity bill such that : Unit Consumption 100 units Next 200 units Next 300 units Beyond 600 units #include<iostream. cout<<"enter the code".2*((e1. amount and no.unit-600)*3.3.bill=0.8*e1. 0.bill=80+240+2*(e1. } 10 | P a g e Bill Amount else if(e1. cout<<"enter the no.unit>100&&e1. else if(e1. char name[45].bill=0. 1. void main() { elect e1. Declare the structure having the following data members.bill. }.8*100+1.unit>600) e1.code.unit<=300) e1.bill=80+240+600+(e1.unit<=100) e1.8 Rs. else if(e1. of units". float bill. bill.unit.unit)-100). cin>>e1.unit. cout<<"this the electricity bill"<<endl.2 Rs.0 . cin>>e1.0 Rs.unit-300) . if(e1.code.unit<=600) e1. of units consumed.h> #include<stdio.h> struct elect { int code. cout<<"enter the name". cout<<e1. gets(e1. 2.



11 | P a g e


12 | P a g e

4. Define a class travelplan in c++ with the following descriptions : Private members:     Public:   a constructor to initialize the value of plan code as 1001, place as”agra”, no of trains=5, no of buses=1. newplan() - allow user to enter plancode, place, no. of trains, no of buses and assign the value to no. of buses as given: no of trains <20 >=20 & <40 >=40  showplan() – to display data members. 3 no of buses 1 2 Plancode Place No of trains No of buses long string int int

#include<iostream.h> #include<string.h>

class travelplan { long plancode; char place[78]; int not; int nob; public: travelplan() { plancode=1001; strcpy(place,"agra"); not=5; nob=1; }

void newplan() { 13 | P a g e cout<<"enter the details";

else if(not>=40) nob=3. t1.cin>>plancode>>place>>not>>nob. } 14 | P a g e . else if(not>=20&&not<40) nob=2.showplan(). } void showplan() { } }.newplan(). if(not<20) nob=1. t1. cout<<plancode<<place<<not<<nob. void main() { travelplan t1.

OUTPUT: 15 | P a g e .

} public: void enterdata() { cout<<"\nEnter target score= ". cin>>extra_time.h> class Cricket { int target_score. void cal_penalty() { if(extra_time<=10) penalty=1. cout<<"\nEnter extra time taken ". overs_bowled. cin>>target_score. Overs_bowled. else if(extra_time>10 && extra_time<=20) penalty=2. Define a class named Cricket in C++ with the following descriptions: Private Members:      Target_score Overs_bowled Extra_time Penalty int int int int Cal_Penalty() . cout<<"\nEnter overs bowled= ".5.a member function to calculate penalty as folllows: Extra time <=10 >10 & <=20 otherwise 2 5 Penalty 1 Public members   Extradata() . cin>>overs_bowled. penalty. Extra_time and call function cal_penalty().h> #include< allow user to enter values for Target_score. else penalty=5. Dispdata() . #include<iostream. 16 | P a g e .to allow user to view the contents of all the data members. extra_time.

Cricket c1. c1. } 17 | P a g e .cal_penalty(). c1. void main() { clrscr(). cout<<endl<<"The penalty is= "<<penalty. } void dispdata() { cout<<endl<<"Target score was= "<<target_score. getch().dispdata(). cout<<endl<<"Overs bowled were= "<<overs_bowled.enterdata(). cout<<endl<<"Extra time taken was= "<<extra_time<<endl. } }.

OUTPUT: 18 | P a g e .

Customer Name and Balance. double balance. char cust_name[20]. gets(cust_name). cin>>acc_no.h> class account { long unsigned int acc_no. } 19 | P a g e .h> #include<conio. 500 to display the account details #include<iostream.6. Include necessary member functions in order to achieve the following tasks:    to deposit the amount to withdraw the amount such that the minimum balance in the account is Rs. Create a class Account that stores Account Number. cout<<"\nEnter amount to save= ". cin>>amnt. cout<<"\nEnter name= ". } void deposit() { double amnt. cout<<"\nEnter amount to be deposited= ". public: void get_info() { cout<<"\nEnter account no= ".h> #include<stdlib.h> #include<stdio. cin>>balance. balance+=amnt.

". int choice. cout<<endl<<"Account no. cout<<endl<<"Account balance= "<<balance. getch(). cin>>amnt. else if(choice==2) a. cout<<endl<<"Enter 1 to deposit"<<endl<<"Enter 2 to withdraw"<<endl. else { cout<<"\nInvalid choice".display().deposit(). else cout<<"\nYour account should have a balance of at least Rs 500. exit(0). account a. withdraw(). if(choice==1) a. } 20 | P a g e . void main() { clrscr().withdraw(). if(balance-amnt>=500) balance-=amnt. } void display() { cout<<endl<<"Name= "<<cust_name. cout<<endl<<"Enter the amount to be withdrawn= ". a.void withdraw() { double amnt.= "<<acc_no. } }.get_info(). } a. cin>>choice.

OUTPUT: 21 | P a g e .

cin>>rno."selected"). cout<<"\nEnter your score: ". void AssignRem() { if(score>=50) strcpy(rem.h> #include<stdio.h> class Candidate { long rno.7. char name[60]. cin>>score. } public : void enter() { cout<<"\nEnter your roll no: "."not selected"). assign remarks as per the score obtained by the candidate Score >=50 <50 Remark selected not selected Public members :   Enter() .to enter the values for Rollno. gets(name). Score and to call AssignRem() to assign remarks Display() : to show all the data members #include<iostream. float score. rem[30]. cout<<"\nEnter your name: ". else strcpy(rem. Define a class Candidate with the following members: Private members :      Roll no Score Name Remarks long float string string AssignRem() .h> #include<string. 22 | P a g e .

AssignRem().enter(). } }. cout<<"\nYour name: ". puts(name). } void display() { cout<<"\nDetails are"<<endl<<"your rollno: "<<rno<<endl. c. c. } 23 | P a g e . void main() { Candidate c. cout<<"REMARKS: "<<rem. cout<<"\nYour score: "<<score<<endl.display().

OUTPUT: 24 | P a g e .

cout<<"\nEnter name ".cin>>cal. gets(name).h> class Nutrition { int display all the details if the foodtype is fruit. Define a class Nutrition having the following members: Private members:         Access no.h> #include<stdlib. Public members: Intake() . Output() . food type. public: void Intake() { cout<<"\nEnter calories ".to input the food name. cin>>cost. calories and the cost. cal. 25 | P a g e . between 10 and 1000.8. float cost. int assignaccess() { } return(10+random(991)). Calories Name of food Foodtype Cost int int char[20] char[30] float Assignaccess() .h> #include<string.h> #include<stdio. #include<iostream. char name[20] .to return a random access no. foodtype[30].cout<<"enter the cost ".

n1. accessno=assignaccess(). puts(name). cout<<"food type: ". } } }.cout<<"\nEnter foodtype ".Intake(). } void Output() { if (strcmpi(foodtype. void main() { Nutrition n1."fruit")==0) { cout<<"\nYour access no: "<<accessno<<endl. cout<<"Name of food: ". puts(foodtype). cout<<"Calories:”<<cal<<endl.Output(). gets(foodtype). } 26 | P a g e . cout<<"Cost:"<<cost<<endl. n1.

OUTPUT: 27 | P a g e .


public : student() { cout<<"\n default constructor ". char name[40]. student s3=s2. strcpy(name.Output(). void main() { student s1.b). strcpy(name. cout<<"\n destroying object". .h> #include<string. char b[]) { cout<<"\n parametrised". admno=1. } student(int a. student s2(3. #include<iostream. } ~student() { } void Output() { } }. } student(student & obj) { admno=obj.h> class student { int admno. s4. admno=a. Illustrate the working of constructors and destructors through a suitable c++ "null"). } 29 | P a g e cout<<"\n"<<admno<<endl<<name.admno. student s4(s3). strcpy(name.9."peter").

OUTPUT: 30 | P a g e .

} }. int student::c. } 31 | P a g e . c=c-1. static int c.h> class student { int admno. student s1.10. #include<iostream.h> #include<string. ~student() { cout<<"\ndestroying object "<<c<<endl. } static void show() { } cout<<"\ntotal objects are: "<<c<<endl. public : student() { c=c+1. char name[40]. cout<<"\nconstructor object "<<c<<endl. student s2. Illustrate the working of constructors and destructors along with the working of static function using a suitable c++ code. void main() { student::show().

OUTPUT: 32 | P a g e .


A obj2.h> class A { public: A() { } ~A() { } }. cout<<"B". cout<<"A".11. class C { public: B obj1. #include<iostream. cout<<"A". class D { public: D() { } ~D() 34 | P a g e cout<<"D". cout<<"C". C() { } ~C() { } }. cout<<"C". Write a program in C++ to show the order of constructor and destructor invocation in a class. cout<<"B". class B { public: B() { } ~B() { } }. .

class E:public C { public: D d. cout<<"Order of Constructor is : ". void main() { cout<<"D". cout<<"E". } 35 | P a g e .{ } }. cout<<"E". E() { } ~E() { } }. B b. cout<<"\nOrder of Destructor is : ". E e.

OUTPUT: 36 | P a g e .

37 | P a g e . class student { char admno[10].12. stream[10]. totmarks. int workload. dept[10]. protected: int attendance.b. Write a program in C++ to show the size of an object of a derived class. public teacher { public: int d. sname[20]. schname[20]. }. public: int a. void main() { school obj. }. cout<<"Size of Object of class School is : "<<sizeof(obj). } char scode[10]. class school: public student. }. tname[8].h> class teacher { char tno[20]. #include<iostream. protected: float salary. public: int c.

OUTPUT: 38 | P a g e .

FILE HANDLING 39 | P a g e .

close().h> #include<string. fout.h> struct student { int admno. #include<stdlib.h> #include<fstream. }while(ch=='y'). cin>>ch. }.char ch. Write a program in c++ to write the name and admission number of students in a file. student s1. fout.13. char name[20].sizeof(s1)).ios::binary).admno. do { cin>>s1.dat". } 40 | P a g e . gets(s1.write((char*) & s1. void main() { ofstream fout("abc.h> #include< cout<<"do u want to write more records : ".

OUTPUT: 41 | P a g e .

ios::binary).eof()) { cout<<s1. fin.h> struct student { int admno. Write a program in c++ to display the name and admission number of students from a file.sizeof(s1)). }.name<<endl.14.h> #include<stdio. ifstream fin("abc. char name[20].read((char *)&s1. while(!fin.sizeof(s1)).close().dat".read((char *)&s1. } 42 | P a g e . #include<fstream. fin.admno<<endl<<s1. void main() { student s1. } fin.

OUTPUT: 43 | P a g e .

int ct=0.h> void main() { ofstream fout("loren. 44 | P a g e .get(ch).txt"). Write a program to read a file character by character and count the number of alphabets in the file. fout<<"my name is loren jade".close(). #include<fstream.close(). cout<<"no of alphabets are: "<<ct. if(!fin) { } char ch. } cout<<"sorry".eof()) { fin.15.h> #include<ctype. while(!fin. } fin. fout.txt").h> #include<stdlib.exit(0). if(isalpha(ch)) ct++. ifstream fin ("loren.

OUTPUT: 45 | P a g e .

close(). ifstream fin("abc. } 46 | P a g e . ofstream fout("abc.eof()) {fin. } while(!fin. of blanks are: "<<ct.h> #include<stdlib. fout<<"Hello.h> void main() { char ch.txt"). welcome to My File 01".close(). exit(0). } cout<<"The total no. if(ch==' ') ct++. Write a program to read a file character by character and count the number of blanks in the file. #include<fstream. fout. int ct=0.get(ch).h> #include<stdio. fin. if(!fin) {cout<<"Sorry".16.txt").

OUTPUT: 47 | P a g e .

int ct=0. while(!fin. ifstream fin ("lorenb. #include<fstream. fout.exit(0). fout<<"the books are on the table".h> #include<string.h> void main() { ofstream fout("lorenb. if(strcmpi(ch.17.txt"). } 48 | P a g e . if(!fin) cout<<"sorry".eof()) { fin>>ch."the")==0) ct++.h> #include<stdlib. char ch[80]. } fin.close(). cout<<"no of 'the' are : "<<ct.close(). Write a program to read a file word by word and count the number of ‘the’ words in the file using a suitable c++ code.txt").

OUTPUT: 49 | P a g e .

my name is abc”.close(). } 50 | P a g e . #include<fstream. } while(!fin. exit(0). ifstream fin(“abc.txt”). cout<<”\nNo.close().txt”). fout<<”Hello.eof()) { fin>>a.h> #include<stdio. if(!fin) { cout<<”Sorry”. } } fin. fout. switch(a[0]) { case ‘a’: case ‘A’: case ‘e’: case ‘E’: case ‘i’: case ‘I’: case ‘o’: case ‘O’: case ‘u’: case ‘U’: ct++.h> #include<stdlib. int ct=0. Write a program to read a file word by word and count the number of words starting with a vowel.h> void main() { char a[20]. Of Words Starting With A Vowel: “<<ct. ofstream fout(“abc.18.

OUTPUT: 51 | P a g e .

getline(ch. ifstream fin ("loren. } 52 | P a g e . fout<<"computer is fun\npracticals are awesome". cout<<" no of lines are : "<<ct.close(). Write a program in c++ to read a file and count the number of lines in the file. #include<stdlib. fout.h> void main() { ofstream fout("loren.} char ch[80]. int ct=0.h> #include<fstream. if(!fin) {cout<<"sorry". } fin.h> #include<iostream.txt").eof()) { fin.exit(0). ct++. cout<<ch<<"\n".close().80).h> #include<string.txt").19. while(!fin.

OUTPUT: 53 | P a g e .

h> #include<stdio. ifstream fin(“abc. if(!fin) { cout<<”Sorry”. Of Lines Starting With ‘I’: “<<ct.\nI live in Jaipur”. } while(!fin.close().txt”). Write a program to read a file line by line and count the number of lines starting with the letter ‘I’.80). exit(0). fout<<”My name is abc. int ct=0.txt”).getLine(a. cout<<”The No.close(). } fin. #include<fstream.eof()) { fin. ofstream fout(“abc. } 54 | P a g e . fout. if(a*0+==’I’) ct++.20.\nI am ninty nine years old.h> #include<stdlib.h> void main() { char a[20].

OUTPUT: 55 | P a g e .

modify\n 5. search. break. void create(). case 3: app().h> #include<stdio. case 5: exit(0). void disp().h> #include<stdlib. append and modify in a file.create\n 2.exit". cin>>ch. break. }.display\n 3. break. while(1) { cout<<"\n 1. case 2: disp(). #include<fstream. case 4: modify(). void main() { int ch.21. switch(ch) { case 1: create(). cout<<"\n enter your choice". break.append\n 4. char name[20]. break. void modify(). display. Write a program in c++ to write. void app(). }//switch ends }//while ends 56 | P a g e .h> struct student { int admno.h> #include<string. default: cout<<"wrong choice".

ios::binary).sizeof(s1)). student s1.admno==a) { strcpy(s1. char s[80]. while(!fin. student s1. gets(s).seekp(pos).s)==0) cout<<s1.tellp(). } pos=f.sizeof(s1)). fin. } void app() { char ch.sizeof(s1)). pos=f. gets(s).sizeof(s1)).admno<<s1.} void modify() { fstream f("stud.sizeof(s1)).read((char*)&s1. } void search() { ifstream fin("student.eof()) { if(s1.close(). int pos. fin. while(!f.tellp().read((char*)&s1. char s[80].eof()) { if(strcmpi(s1.dat".s). f. 57 | P a g e .close(). } fin. cin>>a.dat".ios::binary|ios::in|ios::out).read((char*)&s1. f. }//while ends int a. f.write((char*)&*)&s1.

close().sizeof(s1)). student s1. cout<<"enter name "<<endl. fout.ios::binary) .ios::binary|ios::app) . cout<<"want 2 enter more records". cout<<"sorry".sizeof(s1)).ofstream fout("stud. cin>>ch. fout. }while(ch=='y'). gets(s1.dat".admno. } void create() { char ch.ios::binary) . cin>>s1. cout<<"enter name "<<endl.dat".name). fout. cout<<"want 2 enter more records".close(). ofstream fout("stud. cin>>s1.admno. do { cout<<"enter the details"<<endl.write ((char *)& s1. cout<<"enter admno"<<endl. cin>>ch. student s1. cout<<"enter admno"<<endl. gets(s1.write ((char *)& s1.dat". if( !fin) { } else { 58 | P a g e student s1. .name). }while(ch=='y'). do { cout<<"enter the details"<<endl. fout. } void disp() { ifstream fin("stud.

close ().read((char*)&<<endl. while(!fin. } } 59 | P a g e . cout<<"name"<<s1.sizeof(s1)).sizeof(s1)).admno<<endl. fin. }*)& s1.fin.eof()) { cout<<"admno"<<s1.

OUTPUT: 60 | P a g e .

61 | P a g e .

POINTERS 62 | P a g e .

h> #include<string. *p=*d. *d=t. cout<<"Enter a string: ".h> #include<stdio. WAP to reverse a string (character array) using pointers. d--. while(i<len/2) { char t=*p.i=0. p++. char *d=&p[len-1]. cout<<"\nReversed string is:\n"<<s. } } void main() { char s[80]. print(s). } 63 | P a g e .22. #include<iostream. i++.h> void print(char *p) { int len=strlen(p). gets(s).

OUTPUT: 64 | P a g e .

strcpy(rev. cout<<"Enter a string: ".i=0.h> int palindrome(char *ps) { int len=strlen(ps). else return 0. WAP to find whether a string is a palindrome or not. i++. int chk=palindrome(s).23.h> #include< return 1. } void main() { char s[80]. char *pr=rev. gets(s). *d=t. char rev[80].t.*d=&pr[len-1]. #include<iostream. pr++. d--. if(chk==1) cout<<"\nThe string is a palindrome\n". while(i<len/2) { t=*pr. else cout<<"\nThe string is not a palindrome\n". *pr=*d.h> #include< } if(strcmpi(rev. using pointers. } 65 | P a g e .

OUTPUT: 66 | P a g e .

} 67 | P a g e . p++.h> #include<stdio.len. cout<<"Enter a character: ".i=0.h> void main() { int ct=0. char *p=s. WAP to accept a string and a character and print the number of times the character has occurred in the string. } cout<<"The character has occured "<<ct<<" times in the string".c.24. while(i<len) { if(*p==c) ct++. cout<<"Enter a string: ". len=strlen(p).h> #include<string. char s[80]. gets(s). cin>>c. i++. #include<iostream.

OUTPUT: 68 | P a g e .

ARRAYS 69 | P a g e .

} 70 | P a g e .int c) { for(int i=0.i<3. Display the values divisible by 2 in the 2d array entered by the user.h> void dispnten(int b[3][4].i++) for(int r.3. } void main() { int a[3][4]. for(int i=0.j++) cin>>a[i][j]. dispnten(a.4).25. #include<iostream.j<c.j<4.j++) if(b[i][j]%2==0) cout<<b[i][j].i<r.i++) for(int j=0.

OUTPUT: 71 | P a g e .

j. cout<<endl. cout<<"foll is the array"<<endl.i<s.j++) cout<<b[i][j]<<' '. else b[i][j]=0.s.i++) cin>>a[i].int s) { int i. for(i=0.h> void fun(int a[32].i++) { for(j=0.i<s. cin>>s. for(i=0.j<s. for(int i=0.j<s.s). } 72 | P a g e . Write a function which accepts an integer array and its size as arguments and assigns the elements into a 2d array of integers in the following format: If the array is : 1 2 3 4 5 6 The resultant 2d array is: 123456 123450 123400 123000 120000 100000 #include<iostream.i++) for(j=0. } } void main() { int a[67].j++) if(i+j<=s-1) b[i][j]=a[j].b[34][23]. fun(a.i<s.26.

OUTPUT: 73 | P a g e .

int a[34][56]. a[r-1][j]=temp.temp. fun(a.c. #include<iostream.j++) { temp=a[0][j] c) { int r.j. cout<<endl. cin>>r>>c. } } void main() { int r. for(i=0.j++) cout<<a[i][j]<<' '.r.j<c.i++) for(j=0. a[0][j]=a[r-1][j].j<c. for(int j=0.i<r. } 74 | P a g e . for(i=0. Write a function to swap the rows in a 2d array entered by the user.i<r.i++) { for(j=0. } cout<<"the array after swapping the rows :"<<endl.h> void fun(int a[67][56].c).j<c.i.j++) cin>>a[i][j].

OUTPUT: 75 | P a g e .

i<n.28.h> const int size=50. Write a program in c++ to create. sort.mid. } void search() { //binary search cout<<"Enter no. insert. cin>>n. int a[size].n. void create() { cout<<"\nEnter size". while(beg<=end) { mid=(beg+end)/2.h> #include<stdlib.beg=0.end=n-1. } 76 | P a g e .f=0. search. to search". break. int num. cin>>num. f=1. delete and display a one dimensional integer array.++i) cin>>a[i]. else end=mid-1. for(int i=0. #include<iostream. } if(f==0) cout<<"Not Found". if(a[mid]==num) { cout<<"Found". } else if(a[mid]<num) beg=mid+1.

void sort() //insertion sort { int i,j,temp; for(i=1;i<n;++i) { temp=a[i]; j=i-1; while(temp<a[j]&&j>=0) { a[j+1]=a[j]; j--; } a[j+1]=temp; } }

void insert() { int pos,num; cout<<"\nEnter position where to insert"; cin>>pos; pos--; for(int i=n;i>pos;--i) a[i]=a[i-1]; cout<<"\nEnter number to be inserted"; cin>>num; a[pos]=num; ++n; }

void display() { cout<<"\nArray contents are"; for(int i=0;i<n;++i) cout<<a[i]<<'\t'; }

void del() { int num,pos,f=0,i; cout<<"\nEnter element to be deleted"; cin>>num; for(i=0;i<n;++i) 77 | P a g e


if(a[i]==num) { pos=i; f=1; break; }

} if(f==1) { for(i=pos;i<n-1;++i) a[i]=a[i+1]; n--; } else cout<<"Sorry can't find"; } void main() { int ch; while(500) { cout<<"\n1.Create an array\n2.Search\n3.Sort\n4.Insert\n5.Delete”; cout<<”\n6.Display\n7.Exit"; cout<<"\nEnter your choice\n"; cin>>ch; switch(ch) { case 1: create(); break; case 2: search(); break; case 3: sort(); break; case 4: insert(); break; case 5: del(); break; case 6: display(); break; case 7: exit(0); }//switch ends } 78 | P a g e


79 | P a g e

29.". Write a function in c++ to arrange the same using insertion sort. } desc(e. for(int i=1. #include<iostream.i<3.i++) { temp=a[i]. cout<<"enter name".3).int n) { employee temp. cin>>e[i].eno&&j>=0) { a[j+1]=a[j]. } for( Assume an array e containing elements of structure employee is required to be arranged in descending order of employee<<" "<<a[i]. void desc(employee a[]. float salary.i++) { cout<<"enter employee no. cin>>e[i]. int j. char name[23]. cout<<"enter salary".h> #include<string. } 80 | P a g e .eno.salary<<endl. while(temp. } a[j+1]=temp.eno>a[j]. for(int i=0.eno<<" "<<a[i].h> struct employee { int eno.h> #include<stdio.i<n. }. j=i-1.i<n. } void main() { employee e[3]. j--.i++) cout<<a[i]. cin>>e[i].

OUTPUT: 81 | P a g e .

i++) cin>>a[i]. j++) if(a[j]>a[j+1]) { int temp=a[j].a[20]. for(i=0. i<=n-2. cout<<"\nEnter Array : ". a[j]=a[j+1].Q30. i<n. cout<<"Enter size : ". a[j+1]=temp. } } void main() { int n. for(i=0. } 82 | P a g e . cin>>n.h> void BubbleSort(int a[20]. BubbleSort(a.i. #include <iostream. j<=n-2. Write a program in C++ to sort an array using Bubble Sort Method. The array and its size to be given by the user. int n) { for(int i=0. i<n.i++) for(int j=0. cout<<"\nSorted Array is : ".n). i++) cout<<a[i]<<" ".

OUTPUT: 83 | P a g e .

for(i=0. i<n.31.i++) { small=a[i].j++) if(a[j]<small) { small=a[j]. a[i]=a[pos].j<n. } } void main() { int n.} 84 | P a g e . #include <iostream.small. Write a program in C++ to sort an array using Exchange Selection n) { int i.j. i++) cout<<a[i]<<" ". The array and its size to be given by the user.n).i<n-1. for(j=i+1. cout<<"\nEnter Array : ".pos. ESelection(a. pos=j. cout<<"\nSorted Array is : ".i. cout<<"Enter size : ".h> void ESelection(int a[20]. cin>>n. pos=i. } temp=a[i].temp. for(i=0. a[pos]=temp. for(i=0. i<n. i++) cin>>a[i].a[20].

OUTPUT: 85 | P a g e .


a[top]=n.i--) cout<<a[i]<<endl. void insert() { if(top==4) cout<<"\noverflow".h> int a[5]. 87 | P a g e .h> #include<stdio. } } void del() { if(top==-1) cout<<"underflow". else for(int i=top. else { cout<<"\nElement deleted is "<<a[top].h> #include<process. Write a program in c++ to insert. #include<iostream. top++.32. top--. int top=-1. else { int n. cin>>n. } } void display() { if(top==-1) cout<<"stack empty".i>=0. delete and display in a static queue.

case 2: display().display\n3. } } } 88 | P a g e . cin>>ch.} void main() { int ch.delete\n4. while(1) { cout<<"\n1. cout<<"\nEnter your choice ". case 3: del(). break. break. switch(ch) { case 1: insert().insert\n2.EXIT". case 4: exit(0). break. break.

OUTPUT: 89 | P a g e .

h> const int s=10. cin>>n. int n. else if(r==s-1) r=0. if(f==r) f=r=-1. cout<<"overflow".int & &f. Define function stackpush() to delete and display . q[r]=n. for a static circular queue of array q[10]. #include<iostream. if(r==-1) f=r=0. else { cout<<"the element to be deleted :"<<q[f]<<endl. void stackpush(int q[10]. } } void stackpop(int q[10].33 . else &r) { if(f==-1) cout<<"underflow". } else { cout<<"enter the elements to be inserted"<< &r) { if(((f==0)&&r==(s-1))||(f==r+1)) { cout<<f<<r<<endl. 90 | P a g e .

delete"<<endl.i<=r. } } } void main() { int queue[s]. } } void traversal(int q[10].} else { int i=f. cout<<q[i++]<<"->"<<endl. i=0. cout<<" &f. while(i<=r). cout<<"3. else &r) { if(f==-1) cout<<"empty queue"<<endl.rear=-1. while(i<=s-1) cout<<q[i++]<<"->".insert"<<endl. cin>>choice. char ch='y'. int choice. do { cout<<"1. int front=-1.traversal"<<endl.i++) cout<<q[i]<<"->"<<endl. 91 | P a g e .else if(f==s-1) f=0. else { if(f<=r) { for(int i=f.

case 3:traversal(queue.break.rear).break.front. case 2:stackpop(queue.switch(choice) { case 1: stackpush(queue. }while(ch=='y').rear).break. } 92 | P a g e . cin>>ch.front.front.rear). } cout<<"want to continue".

OUTPUT: 93 | P a g e .

else { temp->link=top. } * top=NULL. delete temp. top=temp. Write a program in c++ to push. void insert() { stud*temp=new stud. char name[20].h> #include<process. gets(temp->name). temp->link=NULL. pop. if(top==NULL) top=temp. top=top->link. } } void display() 94 | P a g e .34. } } void del() { if(top==NULL) cout<<"underflow". delete in a dynamic stack. cin>>temp->admno. else { stud*temp=top. #include<iostream.h> #include<stdio. temp->link=NULL. stud * link.h> struct stud { int admno.

PUSH\ n2. }//switch ends }//while ends } 95 | P a g e . while(temp!=NULL) { cout<<temp->admno<<endl. break. temp=temp->link. switch(ch) { case 1: insert().{ if(top==NULL) cout<<"stack empty". case 3: del(). case 4: exit(0).DELETE\ n4. cin>>ch. case 2: display().EXIT ". else { stud*temp=top. break. cout<<"\nEnter your choice ". break. cout<<temp->name<<endl.POP\ n3. break. while(1) { cout<<"\n1. } } } void main() { int ch.

OUTPUT: 96 | P a g e .

if(f==NULL) r=NULL. temp->link=NULL.h> #include<stdio. f=f->link. void insert() { stud*temp=new stud. delete and display in a dynamic queue.35. else { stud*temp=f. stud * link. temp->link=NULL. 97 | P a g e .h> #include<process.h> struct stud { int admno. * r=NULL. cin>>temp->admno. gets(temp->name). Write a program in c++ to insert. #include<iostream. else { r->link=temp. r=temp. char name[20]. if(f==NULL) f=r=temp. } } void del() { if(f==NULL) cout<<"underflow". } * f=NULL.

cout<<"\nEnter your choice ". while(1) { cout<<"\n1. temp=temp->link.Delete\ n3.delete temp. break. case 2: display(). while(temp!=NULL) { cout<<temp->admno<<endl.Display\ n4. break. } } void display() { if(f==NULL) cout<<"stack empty". cin>>ch. case 4: exit(0). break. cout<<temp->name<<endl. } } } 98 | P a g e . } } } void main() { int ch. case 3: del(). break.EXIT".Insert\n2. switch(ch) { case 1: insert(). else { stud*temp=f.

OUTPUT: 99 | P a g e .


Employees Empid First name 010 George Last name address Smith 83 first street 105 Mary Jones 842 vine ave 152 215 335 Sam Sarah Henry Tones 33 elm st Paris Upton boston Losantiville City Howard Ackerman 440 us 110 Williams 12 moore street 441 300 Peter Robert Thompson 11red read Samuel 9 fifth cross Paris Washington Empsalary empid 010 105 152 215 441 300 335 salary 75000 65000 80000 75000 28000 45000 40000 benefits 15000 15000 25000 12500 7500 10000 10000 designation Manager Manager Director Manager Salesman Clerk clerk 101 | P a g e .

To display empid and first name in descending order of empid from table employees. last name. address and city of all employees living in paris from table employees. city from employees where city=’paris’. 102 | P a g e . First name Sam peter Last name tones Thompson address 33 elm st 11 red road city paris paris Q2. Designation Manager Director Salesman clerk Q4. address.WRITE SQL COMMANDS Q1. Select empid. Empid 441 335 300 215 152 105 010 First name Peter Henry Robert Sarah Sam Mary george Q3. Select first name. last name . To display details of employees whose last name is like_ones. To display distinct designations from table empsalary. To display first name. first name from employees order by empid desc. Select distinct designation from empsalary.

empid=empsalary. Select empid. To display empid.Select * from employees where last name like ‘_ones’. empid 441 300 335 First name Peter Robert Henry Salary 28000 45000 40000 Q6. first name and salary whose salary lies between 25000 and 50000. first name. max(salary) 75000 103 | P a g e .To display maximum salary among manager and clerk from the table empsalary. Select max(salary) from empsalary group by designation having designation in (‘manager’.salary from employees. ‘clerk’). empsalary where employees.empid and salary between 25000 and 50000. empid 105 152 First name Mary sam Last name Jones Tones address 842 vine ave 33 elm st City Losantiville Paris Q5.