COMPUTER SCIENCE

PRACTICAL FILE
By:-Jayant Chandra Class :-> XII-A Roll no. :-> 10

INDEX
1. Binary searching in an array

2. Bubble sort

3. Selection sort

4. Insertion sort

5. Count number of alphabets and digits in a text file.

6. Count number of words in a text file.

7. Count number of lines in a text file.

8. Count number of lines starting with
1

‘t’ / ’T’ in a text file.

9. Merging two given arrays (ascending order) into a single array(ascending order).

10. Deletion of a record from binary file.

11. Modifying a record from a binary file.

12. Program for stack as a linked-list.

13. Program for stack as an array.

14. Program for queue an a linked-list.

15. SQL

2

binary searching
#include<iostream.h> #include<conio.h> int bsearch(int [ ],int,int); void main( ) { clrscr( ); int ar[50]; int n; int item; int index; cout<<"How many elements array do you want to create(maximum 50)"<<endl; cin>>n; cout<<"Enter array elements"<<endl; for(int i=0;i<n;i++) { cin>>ar[i]; } index=bsearch(ar,n,item); if(index==-1) { cout<<"Sorry!! Element"<<item<<" is not found"<<endl; } else
3

{ cout<<"Element found at index :"<<index<<endl; cout<<"At position"<<index+1<<endl; } getch( ); } int bsearch(int ar[ ],int size,int item) { int beg=0; int size; int last=size-1; int mid; while(beg<=last) { mid=(beg+last)/2; if(item==ar[mid]) { return mid; } else if(item>ar[mid]) { beg=mid+1; } else
4

{ last=mid-1. } } return -1. } Output 5 .

int ar[50].h> #include<conio. cin>>n. cout<<"Enter array elements"<<endl. void main( ) { clrscr( ). } getch( ). for(int j=0. for(int i=0.i<n.bubble sort #include<iostream. } 6 .i++) { cin>>ar[50].j<n.h> void bubblesort(int [ ]. 50)"<<endl.j++) { cout<<ar[j]<<endl. cout<<"The sorted array is as given below :"<<endl. } bubblesort(ar.n).int).n. cout<<"How many elements array do you want to create?(max.

j++) { if(ar[ j ]>ar[j+1]) { tmp=ar[ j ].i<size. ar[ j+1 ]=tmp. for(int i=0. }}}} Output 7 . ar[ j ]=ar[ j+1 ].j<(size-1).void bubblesort(int ar[ ].int size) { int tmp.i++) { for(int j=0.

int size) { 8 . int ar[50]n. } selsort(ar.int).i<n. cout<<"Enter array elements"<<endl.n). cout<<"How many elements array do you want to create"<<endl.j<n. for(int i=0. for(int j=0. void main( ) { clrscr( ).j++) { cout<ar[ j ]<<endl.i++) { cin>>ar[i].selection sort #include<iostream.h> void selsort(int [ ]. cout<<"The sorted array is as given below :"<<endl. } void selsort(int ar[ ]. cin>>n. } getch( ).h> #include<conio.

ar[i]=ar[pos].int small. } tmp=ar[i].j++) { if(ar[j]<small) { small=ar[j].tmp.i++) { small=ar[i].j<size. pos=j.i<size. { for(int j=i+1. ar[pos]=tmp. }} Output 9 . for(int i=0.

void main() { clrscr().insertion sort #include<iostream. cout<<"How many elements array do you want to create?"<<endl.h> #include<conio.j<=n.h> void insertion(int [].item . for(int i=1. } 10 .i<=n.i++) { cin>>ar[i]. int ar[50].int). } insertion(ar.index. cout<<"The sorted array is as follows-"<<endl.j++) { cout<<ar[j]<<" ".n). cout<<"Enter array elements"<<endl.h> #include<limits. for(int j=1. cin>>n. } cout<<endl. getch().n .

. while(tmp<ar[j]) { ar[j+1]=ar[j]. } ar[j+1]=tmp.int size) { int tmp.void insertion(int ar[]. j.. j=i-1.i++) { tmp=ar[i]. ar[0]=INT_MIN. }} Output 11 .i<=size. j-. for(int i=1.

int count=0. 12 .eof()) { fil. ifstream fil("honey.Count number of alphabets and digits in a text file.txt"). int count1=0.h> #include<fstream.h> #include<conio. } else if(isdigit(ch)) { count1++. #include<iostream.h> #include<ctype. char ch.get(ch).h> void main() { clrscr(). while(!fil. if(isalpha(ch)) { count++.

close(). fil. cout<<"number of digits are : "<<count1<<endl. getch().} } cout<<"number of alphabets are : "<<count<<endl. } Text file contents 1234 by Output 13 .

h> #include<fstream. } } cout<<"no of words are : "<<count<<endl. while(!fil.h> #include<ctype. } 14 . fil. getch(). char word.txt"). if(word==' ') { count++.eof()) { fil.get(word). int count=0. ifstream fil("student.h> #include<conio. #include<iostream.Count number of words in a text file.close().h> void main() { clrscr().

Output 15 .Text file contents It is a lovely weather today?.

h> #include<conio. getch(). int count=0. } cout<<"no of lines are"<<count<<endl.Count number of lines in a text file. } 16 .getline(word.eof()) { fil. char word[20]. while(!fil.txt"). ifstream fil("countline. count++. fil.h> #include<fstream. #include<iostream.20).h> #include<ctype.close().h> void main() { clrscr().

Text file contents It is A lovely Weather today?. Output 17 .

#include<iostream.h> void main() { clrscr().h> #include<conio.getline(ch. fil.h> #include<string. while(!fil.eof()) { fil.h> #include<fstream.close(). int count=0. char ch[20]. 18 . if((ch[0]=='t')||ch[0]=='T')) { count++.Count number of lines starting with ’T’ in a text file. getch().txt"). } } cout<<"number of lines starting with 't' in the file are"<<count<<endl. ifstream fil("student.20).h> #include<ctype.

} Text file contents my name id the best paret to the vert the to where i m mu the The the bets Output 19 .

int z.int y) { y=z+x.e=0. } } if(d<z) { while(d<z) { 20 .d<z.int X[].Merging two given arrays (ascending order) into a single array(ascending order).int x.e<x.f=0. for(int d=0.h> #include<conio. #include<iostream.) { if(Z[d]<X[e]) { Y[f++]=Z[d++]. } else if(Z[d]>X[e]) { Y[f++]=X[e++].h> void merge(int Z[].int Y[].

B[50].i<a. int a. int A[50]. cout<<"Enter elements of second array in ascending order"<<endl.Y[f++]=Z[d++]. } } else if(e<x) { while(e<x) { Y[f++]=X[e++]. 21 .i++) { cin>>A[i]. }}} void main() { clrscr(). c. cin>>b. cin>>a. for(int i=0. } cout<<"Enter size of second array(B)"<<endl. cout<<"Enter size of first array(A)"<<endl. C[100]. cout<<"Enter elements of first array in ascending order"<<endl. b.

c). cout<<"The merged array is as given below"<<endl. } c=a+b.m<c. } Output 22 .m++) { cout<<C[m]<<" ". } getch().j<b.B.a. for(int m=0.j++) { cin>>B[j].C. merge(A.for(int j=0.b.

#include<iostream.h> #include<string. cout<<"Student name is: ".h> class student { int id.h> #include<conio. } 23 .endl. } void display() { cout<<"Student id is: "<<id<<endl. char name[20]. cout<<"Enter student name"<<endl. public: void input() { cout<<"Enter student id"<<endl.h> #include<fstream. cin>>id.Deletion of a record from binary file.h> #include<stdio. gets(name). puts(name).

fil.int getno() { return id."yes")==0)||(strcmp(ans. }while((strcmp(ans. void main() { clrscr().ios::in|ios::binary).input(). cin>>rno. fstream fil("apple. cout<<"Do you want to enter more records (yes/no)"<<endl.write((char*)&s.sizeof(s)).iso::binary|ios::in|ios::out|ios::app).dat". student s. char ans[5].dat". fil. 24 . do { s.close().ios::out|ios::binary|ios::app). cout<<"Enter student id whose record you want to delete"<<endl."Yes")==0)). gets(ans). ofstream fame("orange. int rno. } }. ifstream fout("apple.dat".

close().sizeof(s)).dat")."No")==0)) { fame.dat").getno()==rno) { cout<<"Are you sure you want to delete this record (yes/no)"<<endl. while(!fout.sizeof(s)). fame.sizeof(s)). ifstream from("apple. gets(ans).close()."apple.fout. } } remove("apple. rename("orange.read((char*)&s. } } } else { fame. 25 .write((char*)&s.dat". if(s.seekg(0).eof()) { fout.write((char*)&s.ios::in|ios::binary). fout."no")==0)||(strcmp(ans.dat". if((strcmp(ans.

read((char*)&s. } from.while(!from.eof()) { from. getch(). s.display(). } 26 .close().sizeof(s)).

gets(name). cout<<"Enter student name"<<endl. cout<<"Student name is: ".endl.h> #include<stdio. puts(name).h> #include<fstream. char name[20]. public: void input() { cout<<"Enter student id"<<endl.h> class student { int id. } 27 . #include<iostream.h> #include<conio. } void display() { cout<<"Student id is: "<<id<<endl.Modifying a record from a binary file. cin>>id.h> #include<string.

void main() { clrscr(). cout<<"Enter correct name"<<endl. gets(nm). int pos.iso::binary|ios::in|ios::out|ios::app).nm).write((char*)&s. do { s. strcpy(name. } }. } int getno() { return id. fil.dat".void modify() { char nm[20].sizeof(s)). student s. int rno.input(). fstream fil("honeysingh. 28 . char ans[5].

f.seekg(0). gets(ans).eof()) { 29 .dat". f."yes")==0)||(strcmp(ans.ios::in|ios::binary).close().read((char*)&s.tellg(). } } f. cout<<"Enter student id whose record you want to edit"<<endl.seekg(pos). f. fil.eof()) { pos=f.close().dat".sizeof(s)). f.sizeof(s)). ifstream fout("honeysingh."Yes")==0)).cout<<"Do you want to enter more records (yes/no)"<<endl.getno()==rno) { s. }while((strcmp(ans. while(!f.modify(). fstream f("honeysingh. break. cin>>rno. if(s.write((char*)&s.ios::out|ios::in|ios::binary). while(!fout.

fout. s.sizeof(s)).display(). } fout. } Output 30 .close(). getch().read((char*)&s.

h> #include<stdlib. top=NULL. int n_info.h> struct node { int info.ans1[5].h> #include<process. do { cout<<"Enter information for new node"<<endl.h> #include<string.h> #include<stdio. }*top.h> #include<conio.*newptr. 31 . char ans[5]. void main() { clrscr(). node*create_new_node(int).*save. void push(node*). node*next. void display(node*).*ptr. #include<iostream.Program for stack as a linked-list. cin>>n_info. void pop().

"Yes")==0)) { pop(). cout<<"Do you want to enter more nodes?"<<endl."Yes")==0)). cout<<"Do you want to pop a node?"<<endl. gets(ans). display(top)."yes")==0)||(strcmp(ans1.newptr=create_new_node(n_info). } node*create_new_node(int n) 32 . gets(ans1). }. display(top). while((strcmp(ans1. exit(0)."yes")==0)||(strcmp(ans. cout<<"The stack is now"<<endl. cout<<"linked stack is"<<endl. cout<<"Do you want to pop another node?"<<endl. gets(ans1). }while((strcmp(ans. } push(newptr). if(newptr==NULL) { cout<<"Cannot create new node"<<endl. getch().

} else { save=top. return ptr. } cout<<endl. } void push(node*np) { if(top==NULL) { top=np.{ ptr=new node. np=np->next. top=np. }} void display(node*np) { while(np!=NULL) { cout<<np->info<<"->". np->next=save. ptr->next=NULL. ptr->info=n. 33 .

} void pop() { { cout<<"Underflow"<<endl. top=top->next. delete ptr. }} if(top==NULL) Output 34 . } else { ptr=top.

item).item.h> #include<conio.int).top=-1.int&.h> #include<process.res. int push(int [].top. do { cout<<"Enter item for insertion"<<endl. 35 . const int size=50. char ans[5]. #include<iostream. exit(0).h> #include<string. ans1[5]. cin>>item. res=push(stack. void display(int []. if(res==-1) { cout<<"Overflow!!"<<endl. void main() { clrscr(). } cout<<"The stack now is"<<endl.Program for stack as an array.h> int pop(int [].h> #include<stdio. int stack [size].int).int&).

cout<<"Do you want to insert more items?(yes/no)"<<endl.top).top). if(res==-1) { cout<<"Underflow!!"<<endl."yes")==0)||(strcmp(ans1. while((strcmp(ans1.display(stack. gets(ans1)."Yes")==0)). exit(0). cout<<"Do you want to delete another element?(yes/no)"<<endl. cout<<"The stack now is"<<endl. }while((strcmp(ans. gets(ans1).top).int ele) 36 ."yes")==0)||(strcmp(ans.int & top. }} getch(). gets(ans). cout<<"Do you want to delete an element ?(yes/no)"<<endl. display(stack."Yes")==0)) { res=pop(stack. } int push(int stack[]. } else { cout<<"Element deleted is : "<<res<<endl.

} else { top++. if(top==-1) { return -1. } return 0.{ if(top==size-1) { return -1. stack[top]=ele. } return ret. } else { ret=stack[top]. } 37 . } int pop(int stack[]. top--.int&top) { int ret.

-"<<endl.i>=0. } cout<<stack[top]<<"<. }} Output 38 .void display(int stack[].int top) { if(top==-1) { return.i--) { cout<<stack[i]<<endl. for(int i=top-1.

front=rear=NULL.h> struct node { int info. void delete_node(). void main() { clrscr().h> #include<process.*ptr. node*create_new_node(int). char ans[5]. do { cout<<"Enter information for new node"<<endl.Program for queue an a linked-list. cin>>n_info.*save.*rear. void display(node*).h> #include<stdlib. 39 .h> #include<stdio. node*next. int n_info.h> #include<conio.h> #include<string. ans1[5]. void insert(node*). }*front.*newptr. #include<iostream.

cout<<"Do you want to delete a node?"<<endl. exit(0). cout<<"The queue now is"<<endl. gets(ans1). display(front)."yes")==0)||(strcmp(ans. } insert(newptr). gets(ans1)."Yes")==0)).newptr=create_new_node(n_info). gets(ans)."Yes")==0)) { delete_node(). display(front)."yes")==0)||(strcmp(ans. while((strcmp(ans. } 40 . }while((strcmp(ans. } getch(). if(newptr==NULL) { cout<<"Cannot create new node"<<endl. cout<<"Do you want to delete another node?"<<endl. cout<<"The queue is now"<<endl. cout<<"Do you want to enter more nodes?"<<endl.

} 41 . }} void display(node*np) { while(np!=NULL) { cout<<np->info<<"->". ptr->info=n. return ptr. ptr->next=NULL. } void insert(node*np) { if(front==NULL) { front=rear=np. rear=np.node*create_new_node(int n) { ptr=new node. np=np->next. } else { rear->next=np.

} void delete_node() { if(front==NULL) { cout<<"Underflow"<<endl. } else { ptr=front. delete ptr. }} Output 42 . front=front->next.cout<<endl.

SQL Consider the tables given below and answer the questions that follow: Table: Student No Name 1 2 3 4 5 6 7 Tushar Aditya Amit Jayant Anshul Ratul fees Zone Age Grade Comp 18 A A B C B B A 10 10 20 20 20 10 20 20000 West 35000 Centre 20 32000 West 38000 North 32000 East 37000 South 20 17 17 17 20 Devavrat 36000 North 43 .

Table: Competition Comp CName Minprize Maxprize Now 10 20 20 Music 25000 32000 50000 40000 1 5 7 Drama 20000 Debate 25000 SQL commands to:-  Create table student and competition 44 .

cname char(10). minprize integer. 3.Create table student(S.Display the details of all the student who are below 20 years of age 45 .Now integer). and Grade of all the students select fees.comp integer).age integer.grade from student.grade char(2).Display the details of all the student Select * from student.fees integer. name char(20).  Simple select 2. maxprize integer. Zone.1.Display the fees.no integer.zone. zone char(7).  Conditional Select using Where Clause 4. Create table competition (comp integer.

6.Display details of student whose grade is not NULL 46 .select * from student where age>20. 8. 5.Display the fees of all the student of department 10 select fees from student where comp=10.Display the names of all the student working in west zone select name from student where zone=’west’.  Using NULL 7.Display details of student whose grade is NULL select * from student where grade is NULL.

AND.select * from student where grade is NOT NULL.  Using Logical Operators (NOT. Display the names of various zones from the table student select distinct(zone) from student. Display the details of all the students of department 10 who are above 20 years of age select * from student 47 . OR) 11. Display the various competition numbers from the table student select distinct(comp) from student. 10.  Using DISTINCT clause 9.

Display the details of all the student who are paying a fee of more than 35000 in the department 20 select * from student where fees=35000 AND comp=20. 48 .fees from student Where zone=’west’ or zone=’centre’.where comp=10 AND age>20. 12. 13. Display the names of all the student who are working in department 20 or 30 select name from student where comp IN(20.30).  Using IN operator 14. Display the names and fees of all the student who are working in West zone or in Centre zone select name.

fees. Display the details of all the students whose names contain ‘a’ as the second character select name. Display the name.  Using LIKE Operator 16. and age of all the students whose names end with ‘a’ select name.age from student 49 .fees.age from student where name like ‘M%’. 17.age from student where name like ‘%a’. fees.fees. 18. Display the details of all the student whose fees is between 32000 and 38000 select * from student Where fees BETWEEN 32000 AND 38000. Using BETWEEN Operator 15. Display the name.fees. and age of all the student whose names start with ‘M’ select name.

where name ‘_a%’.  Using ORDER BY clause 22.min(fees) from student where comp=10.  Using Aggregate functions 19. Display the sum and average of the salaries of all the students select sum(fees). 21. 20. Display the highest and the lowest salaries being paid in department 10 select max(fees). Display the number of students working in department 10 select count(*) from student where comp=10.avg(fees) from student. Display the details of all the students in the ascending order of their salaries 50 .

 Using GROUP BY clause 24. Display the details of all the students in the descending order of their names select * from student order by name DESC. DELETE. lowest fees. Display the total number of students in each department select comp. 25.select * from student order by fees ASC.min(fees).max(fees).avg(fees) from student group by zone.count(*) from student group by comp. 23. and average fees of each zone select zone. ALTER TABLE 51 .  Using UPDATE. Display the highest fees.

Put the grade B for all those whose grade is NULL update student set grade=’B’ where grade is NULL. Increase the fees of all the students above 20 years of age by 10%. 28.1*fees) where age>20. 52 . update student set fees=fees+(0.26. Delete the records of all the students whose grade is C and fees is below 20000  delete from student where grade=’C’ AND fees<20000. 27.

29. Display the details of all the students who work in Drama competition select * from student.comp AND cname=’Drama’.  JOIN of two tables 20. competition where student. Add another column HireDate of type integer in the Student table alter table student add (HireDate integer). Display the Name and Competition Name of all the students 53 . 31.comp=competition.

Drop the table competition  drop table competition.comp. Drop the tables Student  drop table student. select name. cname from student. 54 .comp=competition. competition where student.  DROP TABLE 32. 33.