Computer Practicals-File
Computer Practicals-File
Computer Practicals-File
1.
Binary Search:
#include<conio.h>
#include<iostream.h>
int first,last,mid;
int found=0;
first=0;
last=N-1;
mid=int((first+last)/2);
if(a[mid]==data)
found=1;
if(a[mid]<data)
first=mid+1;
if(a[mid]>data)
last=mid-1;
return(found);
int main()
{
clrscr();
int x[10],data, i;
for(i=0;i<10;i++)
cin>>x[i];
cin>>data;
int res=Binary_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
getch();
return 0;
}
/*
11
14
16
19
20
23
found
*/
2.
Linear Search
#include<conio.h>
#include<iostream.h>
int i, found=0;
for(i=0;i<N;i++)
if(x[i]==data)
found=1;
return(found);
int main()
clrscr();
int x[10],data, i;
for(i=0;i<10;i++)
cin>>x[i];
cin>>data;
int res=Linear_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
getch();
return 0;
/* Enter 10 numbers:1
10
found */
3.
Bubble Sort
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
int main()
int x[10],i,j,temp;
//input phase
clrscr();
for(i=0;i<10;i++)
cin>>x[i];
// Processing
for(i=0;i<9;i++)
for(j=0;j<=(9-i);j++)
if(x[j]>x[j+1])
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
// Output Phase
for(i=0;i<10;i++)
cout<<setw(6)<<x[i];
getch();
return 0;
/*
Enter 1 value 3
Enter 2 value 45
Enter 3 value 67
Enter 4 value 88
Enter 5 value 12
Enter 6 value 90
Enter 7 value 34
Enter 8 value 76
Enter 9 value 29
Enter 10 value 12
Sorted Array 3 12 12 29 34 45 67 76 88 90
*/
4. Selection Sort
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
void main()
clrscr();
int x[10],i,j,temp,low,pos;
// input
for(i=0;i<10;i++)
cin>>x[i];
// Processing
for(i=0;i<9;i++)
low=x[i];
pos=i;
for(j=i+1;j<10;j++)
if(low>x[j])
low=x[j];
pos=j;
temp=x[i];
x[i]=x[pos];
x[pos]=temp;
// output phase
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];
getch();
return ;
/*
74
88
92
34
54
60
71
22
29
Sorted Array: 22 23 29 34 54 60 71 74 88 92
*/
5. Insertion Sort
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
void main()
clrscr();
int x[10],i,j,temp;
// input
for(i=0;i<10;i++)
cin>>x[i];
// Processing
for(i=1;i<10;i++)
temp=x[i];
j=i-1;
{
x[j+1]=x[j];
j=j-1;
x[j+1]=temp;
// output
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];
getch();
return;
/*
23
65
87
66
12
16
62
94
74
90
sorted Array: 12 16 23 62 65 66 74 87 90 94
*/
6. Copy Constructor
#include<conio.h>
#include<iostream.h>
#include<string.h>
class student
int roll;
char name[30];
public:
student()
cout<<"\n Constructor:";
roll=10;
strcpy(name,"Rahul");
student(student &s)
roll=s.roll;
strcpy(name,s.name);
void input_void()
cin>>roll;
cin>>name;
void show_data()
cout<<roll;
cout<<"\n Name:";
cout<<name;
}};
int main()
student s;
s.show_data();
cout<<"\n";
student A(s);
A.show_data();
getch();
return 0;
}
/*
Constructor:
Roll no:10
Name:Rahul
Copy constructor:
Roll no:10
Name:Rahul */
7. Parametrised Constructor
#include<conio.h>
#include<iostream.h>
class read_constructor
int x;
public:
read_constructor(int a)
x=a;
void read_data()
cin>>x;
void show_data()
cout<<"Value of x:"<<x;
};
int main()
read_constructor obj(10);
obj.show_data();
getch();
return 0;
/*
Value of x:10
*/
8. Lower Matrix
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
int main()
clrscr();
int i,j;
// output phase
cout<<"Lower matrix:";
for(i=0;i<4;i++)
cout<<endl;
for(j=0;j<4;j++)
if(i>=j)
cout<<setw(6)<<x[i][j];
else
cout<<setw(6)<<" ";
getch();
return 0;
}
/*
Lower matrix:
5 6
9 10 11
13 14 15 16
*/
9. Sum of Matrices
#include<conio.h>
#include<iostream.h>
int main()
clrscr();
int x[2][3]={10,9,8,8,3,4};
int i,j,sum;
// Processing
sum=0;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
sum=sum +x[i][j];
// output
for(i=0;i<2;i++)
cout<<endl;
for(j=0;j<3;j++)
cout<<" \t"<<x[i][j];
getch();
return 0;
/*
10 9 8
8 3 4
*/
10. Nesting
#include<conio.h>
#include<iostream.h>
class area
int b, h;
double ar;
public:
void input_data()
cin>>b;
cin>>h;
void calculate()
input_data();
ar=0.5*b*h;
}
void output()
calculate();
};
int main()
clrscr();
area A;
A.output();
getch();
return 0;
/*
Enter base:10
Enter height:5
Area of triangle:25
*/
11. Pointers
#include<conio.h>
#include<iostream.h>
int* read()
int x;
x=45;
return(&x);
int main()
clrscr();
int *res;
res=read();
getch();
return 0;
}
/*
Value at res:0x8fc7ffee
*/
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
//function prototype
void add(int &front, int &rear, int x[], int N, int value);
void main()
clrscr();
int x[100],front,rear,choice,value;
front=-1;
rear=-1;
do
cout<<"\n 1. Addition";
cout<<"\n 2. Deletion";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cin>>choice;
switch(choice)
case 1:
cin>>value;
add(front,rear,x,100,value);
break;
case 2:
deletion(front,rear);
break;
case 3:
display(front,rear,x);
getch();
break;
case 4:
break;
default:
getch();
}while(choice!=4);
getch();
return ;
void add(int &front, int &rear, int x[], int N, int value)
if(rear==-1)
front=0;
rear=0;
x[rear]=value;
else
if(rear>=(N-1))
cout<<"full";
else
rear=rear+1;
x[rear]=value;
if(front==-1)
cout<<"Empty";
else
if(front==rear)
front=-1;
rear=-1;
else
front=front+1;
return;
int i;
if(front==-1)
cout<<"Empty";
else
for(i=front;i<=rear;i++)
cout<<setw(4)<<x[i];
}
return;
/*
3. Display
4. Exit
Enter value 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
25 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
5 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
Enter value 9
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
58 9
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
*/
13.Circular Queue
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
//Function prototype
void main()
clrscr();
int x[100],m,data,choice;
m=0;
do
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cin>>choice;
switch(choice)
case 1:
cin>>data;
push(x,100,m,data);
break;
case 2:
pop(m);
break;
case 3:
display(x,m);
getch();
break;
case 4:
break;
default:
getch();
}while(choice!=4);
}
void push(int x[], int N, int &M, int data)
if(M>=N)
cout<<"STACK full";
else
x[M]=data;
M=M+1;
return;
if(m<1)
else
m=m-1;
return;
int i;
if(m<1)
else
for(i=0;i<m;i++)
cout<<setw(6)<<x[i];
return;
/*
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:4
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:6
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:8
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:3
2 4 6 8
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:2
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:3
2 4 6
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:4
*/
14. Read Binary File
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
struct student{
int roll;
char name[30];
char address[80];
};
int main()
clrscr();
ifstream obj("student.dat");
student s;
while(obj.read((char*)&s, sizeof(student)))
cout<<"\nRoll no:"<<s.roll;
cout<<"\n Name:"<<s.name;
cout<<"\n Address:"<<s.address;
obj.close();
getch();
return 0;
/*
Roll no:1
Name:ashish
Address:delhi
*/
15. Write in Binary File
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
struct student
int roll;
char name[30];
char address[80];
};
int main()
clrscr();
ifstream fin("student.dat");
ofstream fout("temp.dat");
int troll;
student s;
cin>>troll;
while(fin.read((char*)&s, sizeof(student)))
if(troll==s.roll)
cin>>s.name;
cin>>s.address;
fout.write((char*)&s, sizeof(student));
fin.close();
fout.close();
//remove("student.dat");
//rename("temp.dat","student.dat");
getch();
return 0;
/*
*/
SQL COMMANDS
Command 1:
Show databases;
Command 2:
Create database;
Command 3 & 4:
Command 7:
Command 9:
Select-Like
Command 10:
Select- order by
Command 11:
Select-distinct
Command 12:
Command 13:
Select – group by
Command 15:
Update
Command 16:
Create view
Command 17:
Drop view
INDEX
● PROGRAMS:
□ Binary Search
□ Linear Search
□ Bubble Sorting
□ Selection Sorting
□ Insertion Sorting
□ Copy Constructor
□ Paramterised Constructor
□ Pointer
□ Linear Queue
□ Circular Queue
□ Show database
□ Create database
□ LIKE command
□ SELECT-ORDER BY command
□ SELECT-DISTINCT command
□ SELECT-GROUP BY command
□ UPDATE command