Professional Documents
Culture Documents
Comp Science Practical File 12th Class PDF
Comp Science Practical File 12th Class PDF
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
t / T in a text file.
15. SQL
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
bubble sort
#include<iostream.h>
#include<conio.h>
void bubblesort(int [ ],int);
void main( )
{
clrscr( );
int ar[50],n;
cout<<"How many elements array do you want to create?(max. 50)"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>ar[50];
}
bubblesort(ar,n);
cout<<"The sorted array is as given below :"<<endl;
for(int j=0;j<n;j++)
{
cout<<ar[j]<<endl;
}
getch( );
}
6
Output
selection sort
#include<iostream.h>
#include<conio.h>
void selsort(int [ ],int);
void main( )
{ clrscr( );
int ar[50]n;
cout<<"How many elements array do you want to create"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
selsort(ar,n);
cout<<"The sorted array is as given below :"<<endl;
for(int j=0;j<n;j++)
{
cout<ar[ j ]<<endl;
} getch( );
}
void selsort(int ar[ ],int size)
{
8
int small,tmp;
for(int i=0;i<size;i++)
{
small=ar[i];
{
for(int j=i+1;j<size;j++)
{
if(ar[j]<small)
{
small=ar[j]; pos=j;
}
tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
}}
Output
insertion sort
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void insertion(int [],int);
void main()
{ clrscr();
int ar[50],item ,n ,index;
cout<<"How many elements array do you want to create?"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(int i=1;i<=n;i++)
{
cin>>ar[i];
}
insertion(ar,n);
cout<<"The sorted array is as follows-"<<endl;
for(int j=1;j<=n;j++)
{
cout<<ar[j]<<" ";
}
cout<<endl;
getch();
}
10
Output
11
}
}
cout<<"number of alphabets are : "<<count<<endl;
cout<<"number of digits are : "<<count1<<endl;
fil.close();
getch();
}
Output
13
14
Output
15
16
Output
17
Output
19
Y[f++]=Z[d++];
}
}
else if(e<x)
{
while(e<x)
{
Y[f++]=X[e++];
}}}
void main()
{
clrscr();
int A[50], B[50], C[100];
int a, b, c;
cout<<"Enter size of first array(A)"<<endl;
cin>>a;
cout<<"Enter elements of first array in ascending order"<<endl;
for(int i=0;i<a;i++)
{
cin>>A[i];
}
cout<<"Enter size of second array(B)"<<endl;
cin>>b;
cout<<"Enter elements of second array in ascending order"<<endl;
21
for(int j=0;j<b;j++)
{
cin>>B[j];
}
c=a+b;
merge(A,a,B,b,C,c);
cout<<"The merged array is as given below"<<endl;
for(int m=0;m<c;m++)
{
cout<<C[m]<<" ";
} getch();
}
Output
22
int getno()
{
return id;
}
};
void main()
{
clrscr();
student s;
int rno;
char ans[5];
fstream fil("apple.dat",iso::binary|ios::in|ios::out|ios::app);
do
{
s.input();
fil.write((char*)&s,sizeof(s));
cout<<"Do you want to enter more records (yes/no)"<<endl;
gets(ans);
}while((strcmp(ans,"yes")==0)||(strcmp(ans,"Yes")==0));
fil.close();
cout<<"Enter student id whose record you want to delete"<<endl;
cin>>rno;
ifstream fout("apple.dat",ios::in|ios::binary);
ofstream fame("orange.dat",ios::out|ios::binary|ios::app);
24
fout.seekg(0);
while(!fout.eof())
{
fout.read((char*)&s,sizeof(s));
if(s.getno()==rno)
{
cout<<"Are you sure you want to delete this record (yes/no)"<<endl;
gets(ans);
if((strcmp(ans,"no")==0)||(strcmp(ans,"No")==0))
{
fame.write((char*)&s,sizeof(s));
}
}
}
else
{
fame.write((char*)&s,sizeof(s));
}
}
remove("apple.dat");
rename("orange.dat","apple.dat");
fame.close();
fout.close();
ifstream from("apple.dat",ios::in|ios::binary);
25
while(!from.eof())
{
from.read((char*)&s,sizeof(s));
s.display();
}
from.close();
getch();
}
26
void modify()
{
char nm[20];
cout<<"Enter correct name"<<endl;
gets(nm);
strcpy(name,nm);
}
int getno()
{
return id;
}
};
void main()
{
clrscr();
student s;
char ans[5];
int rno;
int pos;
fstream fil("honeysingh.dat",iso::binary|ios::in|ios::out|ios::app);
do
{
s.input();
fil.write((char*)&s,sizeof(s));
28
fout.read((char*)&s,sizeof(s));
s.display();
}
fout.close();
getch();
}
Output
30
newptr=create_new_node(n_info);
if(newptr==NULL)
{
cout<<"Cannot create new node"<<endl;
exit(0);
}
push(newptr);
cout<<"linked stack is"<<endl;
display(top);
cout<<"Do you want to enter more nodes?"<<endl;
gets(ans);
}while((strcmp(ans,"yes")==0)||(strcmp(ans,"Yes")==0));
cout<<"Do you want to pop a node?"<<endl;
gets(ans1);
while((strcmp(ans1,"yes")==0)||(strcmp(ans1,"Yes")==0))
{ pop();
cout<<"The stack is now"<<endl;
display(top);
cout<<"Do you want to pop another node?"<<endl;
gets(ans1);
};
getch();
}
node*create_new_node(int n)
32
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void push(node*np)
{
if(top==NULL)
{
top=np;
}
else {
save=top;
top=np;
np->next=save;
}}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<endl;
33
}
void pop()
{
if(top==NULL)
{
cout<<"Underflow"<<endl;
}
else {
ptr=top;
top=top->next;
delete ptr;
}}
Output
34
display(stack,top);
cout<<"Do you want to insert more items?(yes/no)"<<endl;
gets(ans);
}while((strcmp(ans,"yes")==0)||(strcmp(ans,"Yes")==0));
cout<<"Do you want to delete an element ?(yes/no)"<<endl;
gets(ans1);
while((strcmp(ans1,"yes")==0)||(strcmp(ans1,"Yes")==0))
{
res=pop(stack,top);
if(res==-1)
{
cout<<"Underflow!!"<<endl;
exit(0);
}
else {
cout<<"Element deleted is : "<<res<<endl;
cout<<"The stack now is"<<endl;
display(stack,top);
cout<<"Do you want to delete another element?(yes/no)"<<endl;
gets(ans1);
}}
getch();
}
int push(int stack[],int & top,int ele)
36
{
if(top==size-1)
{
return -1;
}
else {
top++;
stack[top]=ele;
}
return 0;
}
int pop(int stack[],int&top)
{
int ret;
if(top==-1)
{
return -1;
}
else {
ret=stack[top];
top--;
}
return ret;
}
37
Output
38
newptr=create_new_node(n_info);
if(newptr==NULL)
{
cout<<"Cannot create new node"<<endl;
exit(0);
}
insert(newptr);
cout<<"The queue now is"<<endl;
display(front);
cout<<"Do you want to enter more nodes?"<<endl;
gets(ans);
}while((strcmp(ans,"yes")==0)||(strcmp(ans,"Yes")==0));
cout<<"Do you want to delete a node?"<<endl;
gets(ans1);
while((strcmp(ans,"yes")==0)||(strcmp(ans,"Yes")==0))
{
delete_node();
cout<<"The queue is now"<<endl;
display(front);
cout<<"Do you want to delete another node?"<<endl;
gets(ans1);
}
getch();
}
40
node*create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert(node*np)
{
if(front==NULL)
{
front=rear=np;
}
else {
rear->next=np;
rear=np;
}}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
41
cout<<endl;
}
void delete_node()
{
if(front==NULL)
{
cout<<"Underflow"<<endl;
}
else {
ptr=front;
front=front->next;
delete ptr;
}}
Output
42
SQL
Consider the tables given below and answer
the questions that follow:
Table: Student
No Name
fees
Zone
Tushar
20000 West
18
10
Aditya
35000 Centre 20
10
Amit
32000 West
20
20
Jayant
38000 North
17
20
Anshul
32000 East
17
20
Ratul
37000 South
17
10
20
20
43
Table: Competition
Comp CName Minprize Maxprize Now
10
Music
25000
32000
20
Drama 20000
50000
20
Debate 25000
40000
44
46
48
update student
set fees=fees+(0.1*fees)
where age>20;
53
DROP TABLE
32. Drop the tables Student
54