Sei sulla pagina 1di 103

Computer science Practical file

Prangav Singhal XII-d

Roll no.-03
1|Page

ACKNOWLEDGEMENT
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.

2|Page

CERTIFICATE
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

3|Page

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

4|Page

STRUCTURES

5|Page

1. 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.
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h>

struct Country { char countryname[20]; char capname[20]; long unsigned int percapitainc; }; void main() { clrscr(); Country c[3]; for (int i=0 ; i<3 ; i++) , cout<<\nEnter Country Name= ; gets(c[i].countryname); cout<<\nEnter Capital Name= ; gets(c[i].capname); cout<<\nEnter per capita income= ; cin>>c[i].percapitainc; } char contname[20]; cout<<\nEnter country name to search ; gets(contname); for(int j=0;j<3;j++) { if(strcmpi(c[j].countryname,contname)==0) , cout<<\nCapital Name is= <<c[j].capname<<endl; cout<<\nPer capita income is= <<c*j+.percapitainc; } } getch(); } 6|Page

OUTPUT:

7|Page

2. Write a program in C++ to make a structure Time and to add two and to display them on screen and the total time.

variables of structure Time

#include<iostream.h> #include<conio.h>

struct time { int hours; int minutes; int seconds; }; void main() { clrscr(); time t1,t2,t3; cout<<"\nEnter The First Time: "; cout<<endl<<"Enter Hours: "; cin>>t1.hours; cout<<"\nEnter Minutes: "; cin>>t1.minutes; cout<<"\nEnter seconds: "; cin>>t1.seconds; cout<<endl<<"The first time is:"<<t1.hours<<":"<<t1.minutes<<":"<<t1.seconds; cout<<endl<<"Enter the second time: "; cout<<endl<<"Enter hours: "; cin>>t2.hours; cout<<"\nEnter Minutes: "; cin>>t2.minutes; cout<<"\nEnter seconds: "; cin>>t.seconds; cout<<endl<<"The second time is:"<<t2.hours<<":"<<t2.minutes<<":"<<t2.seconds; t3.seconds=(t1.seconds+t2.seconds)%60; t3.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/60)%60; t3.hours=t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/60)/60; cout<<endl<<"The total time is "<<t3.hours<<":"<<t3.minutes<<":"<<t3.seconds; getch(); } 8|Page

OUTPUT

9|Page

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

Bill Amount Rs. 0.8 Rs. 1.2 Rs. 2.0 Rs. 3.0

OUTPUT:

....

11 | P a g e

CLASSES AND OBJECTS

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 asagra, 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";

cin>>plancode>>place>>not>>nob; if(not<20) nob=1; else if(not>=20&&not<40) nob=2; else if(not>=40) nob=3; }

void showplan() { } }; cout<<plancode<<place<<not<<nob;

void main() { travelplan t1; t1.newplan(); t1.showplan(); }

14 | P a g e

OUTPUT:

15 | P a g e

5. 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() - a member function to calculate penalty as folllows: Extra time <=10 >10 & <=20 otherwise 2 5 Penalty 1

Public members Extradata() - to allow user to enter values for Target_score, Overs_bowled, Extra_time and call function cal_penalty(). Dispdata() - to allow user to view the contents of all the data members.

#include<iostream.h> #include<conio.h>

class Cricket { int target_score, overs_bowled, extra_time, penalty; void cal_penalty() { if(extra_time<=10) penalty=1; else if(extra_time>10 && extra_time<=20) penalty=2; else penalty=5; } public: void enterdata() { cout<<"\nEnter target score= "; cin>>target_score; cout<<"\nEnter overs bowled= "; cin>>overs_bowled; cout<<"\nEnter extra time taken "; cin>>extra_time; 16 | P a g e

cal_penalty(); } void dispdata() { cout<<endl<<"Target score was= "<<target_score; cout<<endl<<"Overs bowled were= "<<overs_bowled; cout<<endl<<"Extra time taken was= "<<extra_time<<endl; cout<<endl<<"The penalty is= "<<penalty; } };

void main() { clrscr(); Cricket c1; c1.enterdata(); c1.dispdata(); getch(); }

17 | P a g e

OUTPUT:

18 | P a g e

6. Create a class Account that stores Account Number, Customer Name and Balance. 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. 500 to display the account details

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h>

class account { long unsigned int acc_no; char cust_name[20]; double balance;

public:

void get_info() { cout<<"\nEnter account no= "; cin>>acc_no; cout<<"\nEnter name= "; gets(cust_name); cout<<"\nEnter amount to save= "; cin>>balance; }

void deposit() { double amnt; cout<<"\nEnter amount to be deposited= "; cin>>amnt; balance+=amnt; } 19 | P a g e

void withdraw() { double amnt; cout<<endl<<"Enter the amount to be withdrawn= "; cin>>amnt; if(balance-amnt>=500) balance-=amnt; else cout<<"\nYour account should have a balance of at least Rs 500."; withdraw(); }

void display() { cout<<endl<<"Name= "<<cust_name; cout<<endl<<"Account no.= "<<acc_no; cout<<endl<<"Account balance= "<<balance; } };

void main() { clrscr(); account a; a.get_info(); int choice; cout<<endl<<"Enter 1 to deposit"<<endl<<"Enter 2 to withdraw"<<endl; cin>>choice; if(choice==1) a.deposit(); else if(choice==2) a.withdraw(); else { cout<<"\nInvalid choice"; exit(0); } a.display(); getch(); } 20 | P a g e

OUTPUT:

21 | P a g e

7. Define a class Candidate with the following members: Private members : Roll no Score Name Remarks long float string string

AssignRem() - to 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, Name, Score and to call AssignRem() to assign remarks Display() : to show all the data members

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

class Candidate { long rno; float score; char name[60], rem[30]; void AssignRem() { if(score>=50) strcpy(rem,"selected"); else strcpy(rem,"not selected"); } public : void enter() { cout<<"\nEnter your roll no: "; cin>>rno; cout<<"\nEnter your name: "; gets(name); cout<<"\nEnter your score: "; cin>>score; 22 | P a g e

AssignRem(); }

void display() { cout<<"\nDetails are"<<endl<<"your rollno: "<<rno<<endl;

cout<<"\nYour name: "; puts(name); cout<<"\nYour score: "<<score<<endl; cout<<"REMARKS: "<<rem; } };

void main() { Candidate c; c.enter(); c.display(); }

23 | P a g e

OUTPUT:

24 | P a g e

8. Define a class Nutrition having the following members: Private members: Access no. Calories Name of food Foodtype Cost int int char[20] char[30] float

Assignaccess() - to return a random access no. between 10 and 1000.

Public members: Intake() - to input the food name, food type, calories and the cost. Output() - to display all the details if the foodtype is fruit.

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

class Nutrition { int accessno, cal; char name[20] , foodtype[30]; float cost;

int assignaccess() { } return(10+random(991));

public: void Intake() { cout<<"\nEnter calories ";cin>>cal;cout<<"enter the cost "; cin>>cost; cout<<"\nEnter name "; gets(name); 25 | P a g e

cout<<"\nEnter foodtype "; gets(foodtype); accessno=assignaccess(); }

void Output() { if (strcmpi(foodtype,"fruit")==0) { cout<<"\nYour access no: "<<accessno<<endl;

cout<<"Cost:"<<cost<<endl; cout<<"Calories:<<cal<<endl; cout<<"Name of food: "; puts(name); cout<<"food type: "; puts(foodtype); } } };

void main() { Nutrition n1; n1.Intake(); n1.Output(); }

26 | P a g e

OUTPUT:

27 | P a g e

CONSTRUCTORS AND DESTRUCTORS

28 | P a g e

9. Illustrate the working of constructors and destructors through a suitable c++ code.

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

class student { int admno; char name[40]; public : student() { cout<<"\n default constructor ";

admno=1; strcpy(name, "null"); } student(int a, char b[]) { cout<<"\n parametrised";

admno=a; strcpy(name,b); } student(student & obj) { admno=obj.admno; strcpy(name,obj.name); } ~student() { } void Output() { } }; void main() { student s1; student s2(3,"peter"); student s3=s2; student s4(s3); s4.Output(); } 29 | P a g e cout<<"\n"<<admno<<endl<<name; cout<<"\n destroying object";

OUTPUT:

30 | P a g e

10. Illustrate the working of constructors and destructors along with the working of static function using a suitable c++ code.

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

class student { int admno; char name[40]; static int c;

public : student() { c=c+1; cout<<"\nconstructor object "<<c<<endl; }

static void show() { } cout<<"\ntotal objects are: "<<c<<endl;

~student() { cout<<"\ndestroying object "<<c<<endl; c=c-1; } }; int student::c; void main() { student::show(); student s1; student s2; } 31 | P a g e

OUTPUT:

32 | P a g e

INHERITANCE: EXTENDING CLASSES

33 | P a g e

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

{ } }; class E:public C { public: D d; B b; E() { } ~E() { } }; void main() {

cout<<"D";

cout<<"E";

cout<<"E";

cout<<"Order of Constructor is : "; E e; cout<<"\nOrder of Destructor is : ";

35 | P a g e

OUTPUT:

36 | P a g e

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

37 | P a g e

OUTPUT:

38 | P a g e

FILE HANDLING

39 | P a g e

13. Write a program in c++ to write the name and admission number of students in a file.
#include<stdlib.h> #include<string.h> #include<fstream.h> #include<iostream.h>

struct student { int admno; char name[20]; }; void main() { ofstream fout("abc.dat",ios::binary); student s1;char ch; do { cin>>s1.admno; gets(s1.name); fout.write((char*) & s1,sizeof(s1)); cout<<"do u want to write more records : "; cin>>ch; }while(ch=='y'); fout.close(); }

40 | P a g e

OUTPUT:

41 | P a g e

14. Write a program in c++ to display the name and admission number of students from a file.
#include<fstream.h> #include<stdio.h>

struct student { int admno; char name[20]; };

void main() { student s1; ifstream fin("abc.dat",ios::binary); fin.read((char *)&s1,sizeof(s1)); while(!fin.eof()) { cout<<s1.admno<<endl<<s1.name<<endl; fin.read((char *)&s1,sizeof(s1)); } fin.close(); }

42 | P a g e

OUTPUT:

43 | P a g e

15. Write a program to read a file character by character and count the number of alphabets in the file.
#include<fstream.h> #include<stdlib.h> #include<ctype.h>

void main() { ofstream fout("loren.txt"); fout<<"my name is loren jade"; fout.close(); ifstream fin ("loren.txt"); if(!fin) { } char ch; int ct=0; while(!fin.eof()) { fin.get(ch); if(isalpha(ch)) ct++; } fin.close(); cout<<"no of alphabets are: "<<ct; } cout<<"sorry";exit(0);

44 | P a g e

OUTPUT:

45 | P a g e

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

46 | P a g e

OUTPUT:

47 | P a g e

17. 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.
#include<fstream.h> #include<stdlib.h> #include<string.h>

void main() { ofstream fout("lorenb.txt"); fout<<"the books are on the table"; fout.close(); ifstream fin ("lorenb.txt"); if(!fin) cout<<"sorry";exit(0); char ch[80]; int ct=0; while(!fin.eof()) { fin>>ch; if(strcmpi(ch,"the")==0) ct++; } fin.close(); cout<<"no of 'the' are : "<<ct; }

48 | P a g e

OUTPUT:

49 | P a g e

18. Write a program to read a file word by word and count the number of words starting with a vowel.

#include<fstream.h> #include<stdio.h> #include<stdlib.h> void main() { char a[20]; int ct=0; ofstream fout(abc.txt); fout<<Hello, my name is abc; fout.close(); ifstream fin(abc.txt); if(!fin) { cout<<Sorry; exit(0); } while(!fin.eof()) { fin>>a; switch(a[0]) { case a: case A: case e: case E: case i: case I: case o: case O: case u: case U: ct++; } } fin.close(); cout<<\nNo. Of Words Starting With A Vowel: <<ct; }

50 | P a g e

OUTPUT:

51 | P a g e

19. Write a program in c++ to read a file and count the number of lines in the file.
#include<stdlib.h> #include<string.h> #include<fstream.h> #include<iostream.h>

void main() { ofstream fout("loren.txt"); fout<<"computer is fun\npracticals are awesome"; fout.close(); ifstream fin ("loren.txt"); if(!fin) {cout<<"sorry";exit(0);} char ch[80]; int ct=0; while(!fin.eof()) { fin.getline(ch,80); cout<<ch<<"\n"; ct++; } fin.close(); cout<<" no of lines are : "<<ct; }

52 | P a g e

OUTPUT:

53 | P a g e

20. Write a program to read a file line by line and count the number of lines starting with the letter I.

#include<fstream.h> #include<stdio.h> #include<stdlib.h> void main() { char a[20]; int ct=0; ofstream fout(abc.txt); fout<<My name is abc.\nI am ninty nine years old.\nI live in Jaipur; fout.close(); ifstream fin(abc.txt); if(!fin) { cout<<Sorry; exit(0); } while(!fin.eof()) { fin.getLine(a,80); if(a*0+==I) ct++; } fin.close(); cout<<The No. Of Lines Starting With I: <<ct; }

54 | P a g e

OUTPUT:

55 | P a g e

21. Write a program in c++ to write, display, search, append and modify in a file.
#include<fstream.h> #include<stdio.h> #include<stdlib.h> #include<string.h>

struct student { int admno; char name[20]; }; void create(); void disp(); void app(); void modify();

void main() { int ch; while(1) { cout<<"\n 1.create\n 2.display\n 3.append\n 4.modify\n 5.exit"; cout<<"\n enter your choice"; cin>>ch; switch(ch) { case 1: create(); break; case 2: disp(); break; case 3: app(); break; case 4: modify(); break; case 5: exit(0); break; default: cout<<"wrong choice"; }//switch ends }//while ends 56 | P a g e

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

57 | P a g e

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

fin.read((char*)& s1,sizeof(s1)); while(!fin.eof()) { cout<<"admno"<<s1.admno<<endl; cout<<"name"<<s1.name<<endl; fin.read((char*)&s1,sizeof(s1)); } fin.close (); } }

59 | P a g e

OUTPUT:

60 | P a g e

61 | P a g e

POINTERS

62 | P a g e

22. WAP to reverse a string (character array) using pointers.

#include<iostream.h> #include<stdio.h> #include<string.h> void print(char *p) { int len=strlen(p),i=0; char *d=&p[len-1]; while(i<len/2) { char t=*p; *p=*d; *d=t; p++; d--; i++; } } void main() { char s[80]; cout<<"Enter a string: "; gets(s); print(s); cout<<"\nReversed string is:\n"<<s; }

63 | P a g e

OUTPUT:

64 | P a g e

23. WAP to find whether a string is a palindrome or not, using pointers.


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

OUTPUT:

66 | P a g e

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

67 | P a g e

OUTPUT:

68 | P a g e

ARRAYS

69 | P a g e

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

void main() { int a[3][4]; for(int i=0;i<3;i++) for(int j=0;j<4;j++) cin>>a[i][j]; dispnten(a,3,4); }

70 | P a g e

OUTPUT:

71 | P a g e

26. 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.h> void fun(int a[32],int s) { int i,j,b[34][23]; for(i=0;i<s;i++) for(j=0;j<s;j++) if(i+j<=s-1) b[i][j]=a[j]; else b[i][j]=0; cout<<"foll is the array"<<endl; for(i=0;i<s;i++) { for(j=0;j<s;j++) cout<<b[i][j]<<' '; cout<<endl; } } void main() { int a[67],s; cin>>s; for(int i=0;i<s;i++) cin>>a[i]; fun(a,s); } 72 | P a g e

OUTPUT:

73 | P a g e

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

void main() { int r,c,i,j; cin>>r>>c; int a[34][56]; for(i=0;i<r;i++) for(j=0;j<c;j++) cin>>a[i][j]; fun(a,r,c); }

74 | P a g e

OUTPUT:

75 | P a g e

28. Write a program in c++ to create, search, sort, insert, delete and display a one dimensional integer array.
#include<iostream.h> #include<stdlib.h>

const int size=50; int a[size],n; void create() { cout<<"\nEnter size"; cin>>n; for(int i=0;i<n;++i) cin>>a[i]; }

void search() {

//binary search

cout<<"Enter no. to search"; int num,beg=0,end=n-1,mid,f=0; cin>>num; while(beg<=end) { mid=(beg+end)/2; if(a[mid]==num) { cout<<"Found"; f=1; break; } else if(a[mid]<num) beg=mid+1; else end=mid-1; } if(f==0) cout<<"Not Found";

} 76 | P a g e

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

OUTPUT:

79 | P a g e

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

OUTPUT:

81 | P a g e

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

82 | P a g e

OUTPUT:

83 | P a g e

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

84 | P a g e

OUTPUT:

85 | P a g e

LINKED LISTS, STACKS AND QUEUES

86 | P a g e

32. Write a program in c++ to insert, delete and display in a static queue.
#include<iostream.h> #include<process.h> #include<stdio.h> int a[5]; int top=-1;

void insert() { if(top==4) cout<<"\noverflow"; else { int n; cin>>n; top++; a[top]=n; } }

void del() { if(top==-1) cout<<"underflow"; else { cout<<"\nElement deleted is "<<a[top]; top--; } }

void display() { if(top==-1) cout<<"stack empty"; else for(int i=top;i>=0;i--) cout<<a[i]<<endl; 87 | P a g e

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

88 | P a g e

OUTPUT:

89 | P a g e

33 . Define function stackpush() to delete and display , for a static circular queue of array q[10]. #include<iostream.h> const int s=10;

void stackpush(int q[10],int &f,int &r) { if(((f==0)&&r==(s-1))||(f==r+1)) { cout<<f<<r<<endl; cout<<"overflow"; } else { cout<<"enter the elements to be inserted"<<endl; int n; cin>>n; if(r==-1) f=r=0; else if(r==s-1) r=0; else r=r+1; q[r]=n; } }

void stackpop(int q[10],int &f,int &r) { if(f==-1) cout<<"underflow"; else { cout<<"the element to be deleted :"<<q[f]<<endl; if(f==r) f=r=-1; 90 | P a g e

else if(f==s-1) f=0; else f=f+1; } }

void traversal(int q[10],int &f,int &r) { if(f==-1) cout<<"empty queue"<<endl; else { if(f<=r) { for(int i=f;i<=r;i++) cout<<q[i]<<"->"<<endl;} else { int i=f; while(i<=s-1) cout<<q[i++]<<"->"; i=0; while(i<=r); cout<<q[i++]<<"->"<<endl; } } }

void main() { int queue[s]; int front=-1,rear=-1; char ch='y'; int choice; do { cout<<"1.insert"<<endl; cout<<"2.delete"<<endl; cout<<"3.traversal"<<endl; cin>>choice; 91 | P a g e

switch(choice) { case 1: stackpush(queue,front,rear);break; case 2:stackpop(queue,front,rear);break; case 3:traversal(queue,front,rear);break; } cout<<"want to continue"; cin>>ch; }while(ch=='y'); }

92 | P a g e

OUTPUT:

93 | P a g e

34. Write a program in c++ to push, pop, delete in a dynamic stack.


#include<iostream.h> #include<process.h> #include<stdio.h>

struct stud { int admno; char name[20]; stud * link; } * top=NULL;

void insert() { stud*temp=new stud; cin>>temp->admno; gets(temp->name); temp->link=NULL; if(top==NULL) top=temp; else { temp->link=top; top=temp; } }

void del() { if(top==NULL) cout<<"underflow"; else { stud*temp=top; top=top->link; temp->link=NULL; delete temp; } }

void display() 94 | P a g e

if(top==NULL) cout<<"stack empty"; else { stud*temp=top; while(temp!=NULL) { cout<<temp->admno<<endl; cout<<temp->name<<endl; temp=temp->link; } }

void main() { int ch; while(1) { cout<<"\n1.PUSH\ n2.POP\ n3.DELETE\ n4.EXIT "; cout<<"\nEnter your choice "; cin>>ch; switch(ch) { case 1: insert(); break; case 2: display(); break; case 3: del(); break; case 4: exit(0); break; }//switch ends }//while ends }

95 | P a g e

OUTPUT:

96 | P a g e

35. Write a program in c++ to insert, delete and display in a dynamic queue.
#include<iostream.h> #include<process.h> #include<stdio.h>

struct stud { int admno; char name[20]; stud * link; } * f=NULL, * r=NULL;

void insert() { stud*temp=new stud; cin>>temp->admno; gets(temp->name); temp->link=NULL; if(f==NULL) f=r=temp; else { r->link=temp; r=temp; } }

void del() { if(f==NULL) cout<<"underflow"; else { stud*temp=f; f=f->link; if(f==NULL) r=NULL; temp->link=NULL; 97 | P a g e

delete temp; } }

void display() { if(f==NULL) cout<<"stack empty"; else { stud*temp=f; while(temp!=NULL) { cout<<temp->admno<<endl; cout<<temp->name<<endl; temp=temp->link; } } }

void main() { int ch; while(1) { cout<<"\n1.Insert\n2.Delete\ n3.Display\ n4.EXIT"; cout<<"\nEnter your choice "; cin>>ch; switch(ch) { case 1: insert(); break; case 2: display(); break; case 3: del(); break; case 4: exit(0); break; } } }

98 | P a g e

OUTPUT:

99 | P a g e

STRUCTURED QUERY LANGUAGE

100 | 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

WRITE SQL COMMANDS


Q1. To display first name, last name, address and city of all employees living in paris from table employees.

Select first name, last name , address, city from employees where city=paris; First name
Sam peter

Last name
tones Thompson

address
33 elm st 11 red road

city
paris paris

Q2. To display empid and first name in descending order of empid from table employees.

Select empid, first name from employees order by empid desc; Empid
441 335 300 215 152 105 010

First name
Peter Henry Robert Sarah Sam Mary george

Q3. To display distinct designations from table empsalary.

Select distinct designation from empsalary; Designation


Manager Director Salesman clerk

Q4. To display details of employees whose last name is like_ones.


102 | P a g e

Select * from employees where last name like _ones; empid


105 152

First name
Mary sam

Last name
Jones Tones

address
842 vine ave 33 elm st

City
Losantiville Paris

Q5. To display empid, first name and salary whose salary lies between 25000 and 50000.

Select empid, first name,salary from employees, empsalary where employees.empid=empsalary.empid and salary between 25000 and 50000; empid
441 300 335

First name
Peter Robert Henry

Salary
28000 45000 40000

Q6.To display maximum salary among manager and clerk from the table empsalary.

Select max(salary) from empsalary group by designation having designation in (manager, clerk); max(salary)
75000

103 | P a g e

Potrebbero piacerti anche