Professional Documents
Culture Documents
C++ PROGRAMS:
1. ONE DIMENSIONAL ARRAY
3. LINEAR SEARCH
4. BINARY SEARCH
5. INSERTION IN ARRAY
6. DELETION IN ARRAY
7. BUBBLE SORTING
8. INSERTION SORTING
9. SELECTION SORTING
21. TO CALCULATE TSA AND VOLUME OF A CUBE ,A CUBOID AND A CYLINDER DEPENDING
ON USER’S CHOICE
SQL COMMANDS: (15 commands)
1. Show database
2. Create database
3. To insert data
9. LIKE command
INPUT:
#include <iostream>
int main ()
print(arr,3);
return 0;
OUTPUT:
PROGRAM 2: TO ILLUSTRATE A TWO DIMENSIONAL ARRAY
INPUT:
#include <iostream>
int main ()
print(arr,4,3);
return 0;
OUTPUT:
PROGRAM 3: TO SEARCH AN ELEMENT IN AN ARRAY
THROUGH LINEAR SEARCH
INPUT:
#include<iostream.h>
#include<conio.h>
int main()
cin>>N;
{cin>>AR[i];}
cin>>ITEM;
index= Lsearch(AR,N,ITEM);
if(index==-1)
return 0;
}
int Lsearch(int AR[],int size,int item) //function to perform liner search
return -1; //the control will reach here only when item is not found
OUTPUT:
PROGRAM 4: TO SEARCH FOR AN ELEMENT IN AN ARRAY
THROUGH BINARY SEARCH
INPUT :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{ system("cls");
cin>>N;
cin>>AR[i];
cin>>ITEM;
index=Bsearch(AR,N,ITEM);
if(index==-1)
return 1;
}
int Bsearch(int AR[],int size, int item) //function to perform binary search
{ int beg,last,mid;
beg=0 ; last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
else
else last=mid-1;
return 0;
OUTPUT:
PROGRAM 5: TO INSERT AN ELEMENT IN AN ARRAY
INPUT:
#include<iostream.h>
int main()
cin>>N;
cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
cin>>ITEM;
if(N==50)
{ cout<<"Overflow\n";
exit(1);
cin>>ch;
cout<<AR[i]<<" ";
cout<<endl;
return 0;
int FindPos(int AR[], int size,int item) //function to determine the position for
new element
{ int pos;
else
}
if(i== size-1) pos=size;
return pos;
OUTPUT:
PROGRAM 6: TO DELETE AN ELEMENT FROM AN ARRAY
INPUT:
#include<iostream.h>
int main()
cin>>N;
cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
cin>>ITEM;
if(N==0)
{ cout<<"Underflow";
exit(1);}
if(index!=-1) AR[index]=0;
cout<<AR[i]<<" ";
cout<<endl;
for(i=index;i<N;i++)
cin>>ch;}
cout<<"The array after shifting ALL emptied spaces towards right is...\n";
cout<<AR[i]<<"";
cout<<endl;
return 0;
{ if(AR[i]==item) return i;
return -1; //the control will reach here only when item is not found
}
OUTPUT:
PROGRAM 7: TO SORT AN ARRAY WITH THE HELP OF
BUBBLE SORTING
INPUT :
#include<iostream.h>
#include<conio.h>
int array[size],n;
void bubble_sort()
{ int temp;
for(int j=0;j<i;j++)
if(array[j]<array[j+1])
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
void main()
clrscr();
cin>>n;
for(int i=0;i<n;i++)
cin>>array[i];
bubble_sort();
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
getch(); }
OUTPUT:
PROGRAM 8: TO SORT AN ARRAY THROUGH INSERTION SORTING
INPUT:
#include<iostream.h>
#include<conio.h>
int array[size],n;
void Ins_sort()
int tmp,j;
array[0]=-32678;
for(int k=1;k<=n;k++)
j=k-1;
tmp=array[k];
while(tmp<array[j])
array[j+1]=array[j];
j--;
array[j+1]=tmp;
}
void main()
clrscr();
cin>>n;
for(int i=1;i<=n;i++)
cin>>array[i];
Ins_sort();
cout<<"Sorted Array:\n";
for(i=1;i<=n;i++)
cout<<array[i]<<" ";
getch();
OUTPUT:
PROGRAM 9: TO SORT AN ARRAY WITH THE HELP OF
SELECTION SORTING
INPUT:
#include<iostream.h>
#include<conio.h>
int array[size],n;
void sel_sort()
int small,pos,temp;
for(int i=0;i<n;i++)
small=array[i];
pos=i;
for(int j=i+1;j<n;j++)
if(array[j]<small)
small=array[j];
pos=j;
}
}
temp=array[i];
array[i]=array[pos];
array[pos]=temp;
void main()
clrscr();
cin>>n;
for(int i=0;i<n;i++)
cin>>array[i];
sel_sort();
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
getch(); }
OUTPUT:
PROGRAM 10: TO MERGE TWO ARRAYS, A AND B, INTO AN
ARRAY C WHERE THE ELEMENTS ARE IN ASCENDING
ORDER
INPUT:
#include<iostream.h>
#include<conio.h>
int A[40],B[40],C[60];
int m,n,mn;
void Merge()
int a=0,b=0,c=0;
while(a<m&&b<n)
if(A[a]<B[b])
C[c++]=A[a++];
else
C[c++]=B[b++];
if(a<m)
while(a<m)
C[c++]=A[a++];
else
while(b<n)
C[c++]=B[b++];
void main()
cin>>m;
for(int i=0;i<m;i++)
cin>>A[i];
cin>>n;
for(i=0;i<n;i++)
cin>>B[i];
mn=m+n;
if(mn<=60)
Merge();
else
{
return;
for(i=0;i<mn;i++)
cout<<C[i]<<" ";
getch();}
OUTPUT:
PROGRAM 11: TO ILLUSTRATE A FRIEND FUNCTION IN A
CLASS PROVIDING ACESS TO PRIVATE AND PROTECTED
MEMBERS OF THAT CLASS
INPUT:
#include <iostream>
class Rectangle
{ private :
int length;
int width;
public:
{ length = len;
width = wid; }
int getArea()
friend double
};
{ double cost;
cost = rect.length * rect.width * 5;
return cost;
int main ()
{ Rectangle floor;
floor.setData(20,3);
return 0;
OUTPUT:
PROGRAM 12: TO ILLUSTRATE A CLASS AS A FRIEND OF AN
OTHER CLASS
INPUT:
#include <iostream>
class CostCalculator;
class Rectangle
{ private :
int length;
int width;
public:
{ length = len;
width = wid; }
int getArea()
friend class
};
class CostCalculator
{ public :
double getCost (Rectangle rect)
{ double cost;
return cost; }
};
int main ()
{ Rectangle floor;
floor.setData(20,3);
CostCalculator calc;
return 0;
OUTPUT:
PROGRAM 13: TO ILLUSTRATE A CONSTRUCTOR IN A
DERIVED CLASS B OF BASE CLASS A
INPUT:
class Rectangle
{private :
float length;
float width;
public:
Rectangle ()
{ length = 0;
width = 0; }
{ length = len;
width = wid; }
float area()
{ private :
float height;
public:
Box ()
{ height = 0; }
Box (float len, float wid, float ht) : Rectangle(len, wid)
{ height = ht; }
float volume()
};
int main ()
{ Box bx;
Box cx(4,8,5);
return 0;
OUTPUT:
PROGRAM 14 : TO ILLUSTRATE A BINARY FILE AND ITS
OPERATIONS SUCH AS READ(), WRITE() AND MODIFY()
INPUT:
#include<iostream.h>
#include<fstream.h>
#include<cstdio.h>
class Student
{ int admno;
char name[50];
public:
void setData()
cin.getline(name,50);
void showData()
int retAdmno()
{ return admno;
}
};
/*
*/
void write_record()
{ ofstream outFile;
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();
/*
*/
void display()
{ ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{ obj.showData();
} inFile.close();
}
/*
*/
void search(int n)
{ ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() == n)
{ obj.showData();}
inFile.close();
/*
*/
void delete_record(int n)
{ Student obj;
ifstream inFile;
inFile.open("student.dat", ios::binary);
ofstream outFile;
while(inFile.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() != n)
{ outFile.write((char*)&obj, sizeof(obj)); } }
inFile.close();
outFile.close();
remove("student.dat");
rename("temp.dat", "student.dat");
/*
*/
void modify_record(int n)
{ fstream file;
file.open("student.dat",ios::in | ios::out);
Student obj;
while(file.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() == n)
obj.setData();
file.write((char*)&obj, sizeof(obj)); }
} file.close();
int main()
write_record();
display();
//Search record
search(100);
//Delete record
delete_record(100);
//Modify record
modify_record(101);
return 0;
}
OUTPUT:
PROGRAM 15: TO ILLUSTRATE READING A TEXT FILE
INPUT:
#include<fstream.h>
#include<iostream.h>
int main()
{ ifstream fin;
fin.open("out.txt");
char ch;
while(!fin.eof())
{ fin.get(ch);
fin.close();
return 0;}
OUTPUT:
PROGRAM 16: TO COUNT THE NUMBER OF CHARACTERS IN
A FILE
INPUT:
#include<fstream>
#include<iostream>
int main()
{ ifstream fin;
fin.open("out.txt");
int count = 0;
char ch;
while(!fin.eof())
{ fin.get(ch);
count++; } cout << "Number of characters in file are " << count;
fin.close();
return 0;}
OUTPUT:
PROGRAM 17: TO ILLUSTRATE A STATIC QUEUE WITH
BASIC OPERATIONS
INPUT:
#include<iostream.h>
#include<conio.h>
#define size 4
class cqueue
{ int data[size];
int front,rear;
public:
cqueue()
{ front=-1;rear=-1;}
void insert();
void remove();
};
void cqueue::insert()
{ if(rear==size-1&&front==0 || front==rear+1)
return;
}else if(rear==-1)
{rear++;
front++; }
else if(rear==size-1)
rear=0;
else
rear++;
cin>>data[rear];}
void cqueue::remove()
{ if(front==-1)
cout<<data[front]<<" deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1;}
else if(front==size-1)
front=0;
else
front++;}
void main()
{ cqueue cq;
int ch;
do
cin>>ch;
switch(ch)
case 1: cq.insert();break;
case 2: cq.remove();break;
}while(ch!=3); }
OUTPUT:
PROGRAM 18: TO ILLUSTRATE A DYNAMIC QUEUE
INPUT:
#include<iostream.h>
#include<conio.h>
struct node
{ int data;
node *next;};
class queue
{ node *rear,*front;
public:
queue()
{ rear=NULL;front=NULL;}
void qinsert();
void qdelete();
void qdisplay();
~queue();
};
void queue::qinsert()
{ node *temp;
temp=new node;
cout<<"Data :";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{ rear=temp;
front=temp;
} else
{ rear->next=temp;
rear=temp; }
void queue::qdelete()
{ if(front!=NULL)
{ node *temp=front;
cout<<front->data<<"deleted \n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL;
} else
cout<<"Queue Empty..";}
void queue::qdisplay()
{ node *temp=front;
while(temp!=NULL)
{ cout<<temp->data<<endl;
temp=temp->next; }
queue::~queue()
{ while(front!=NULL)
{ node *temp=front;
front=front->next;
delete temp; } }
void main()
cin>>ch;
switch(ch)
} }while(ch!='q');
OUTPUT:
PROGRAM 19: TO ILLUSTRATE A STATIC STACK
INPUT:
#include<iostream.h>
#include<conio.h>
#define size 4
class stack
{ int data[size];
int top;
public:
stack()
{ top=-1; }
void push();
void pop();
void display();
};
void stack::push()
{ if(top==size-1)
{ cout<<"\nStack is full";
return; }
else
{ top++;
void stack::pop()
{ if(top==-1)
else
{ cout<<data[top]<<"deleted "<<endl;
top--;
void stack::display()
{ int t=top;
while(t>=0)
{ cout<<data[t]<<endl;
t--;
void main()
{ stack st;
int ch;
do
switch(ch)
{ case 1: st.push();break;
case 2: st.pop();break;
case 3: st.display();
}while(ch!=4); }
OUTPUT:
PROGRAM 20: TO ILLUSTRATE A DYNAMIC STACK WITH
BASIC OPERATIONS
INPUT:
#include<iostream.h>
#include<conio.h>
struct node
{ int data;
node *next;
};
class stack
{ node *top;
public :
stack()
{ top=NULL;}
void push();
void pop();
void display();
~stack();
};
void stack::push()
{ node *temp;
temp=new node;
cout<<"Enter data :";
cin>>temp->data;
temp->next=top;
top=temp; }
void stack::pop()
{ if(top!=NULL)
{ node *temp=top;
top=top->next;
cout<<temp->data<<"deleted";
delete temp;}
else
cout<<"Stack empty"; }
void stack::display()
{ node *temp=top;
while(temp!=NULL)
{ cout<<temp->data<<" ";
temp=temp->next; }
stack::~stack()
{while(top!=NULL)
{node *temp=top;
top=top->next;
delete temp; }
void main()
{ stack st;
char ch;
do{ cout<<"stack options\nP for push \nO for Pop \nD for Display \nQ for
quit";
cin>>ch;
switch(ch)
}while(ch!='Q');}
OUTPUT:
PROGRAM 21: A MENU DRIVEN PROGRAM TO CALCULATE
TSA AND VOLUME OF A CUBE, CUBOID AND A CYLINDER
DEPENDING ON USER’S CHOICE
INPUT:
#include<iostream.h>
#include<conio.h>
void Tsa_Volume(int a)
{cout<<"\nVolume:"<<a*a*a;
cout<<"\nVolume:"<<a*b*c;
cout<<"\nVolume:"<<(22*a*a*b)/7;
void main()
{
int a,b,c;
int choice;
do{
clrscr();
cin>>choice;
switch(choice)
cin>>a;
Tsa_Volume(a);
break;
cin>>a>>b>>c;
Tsa_Volume(a,b,c);
break;
cin>>a;
cin>>b;
Tsa_Volume(a,b);
break;
case 4: return ;
default:cout<<"Wrong Choice!!!!";
break;}
getch();
}while(choice!=4) ;
OUTPUT:
SQL COMMANDS: (15 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
PREPARED AND SUBMITTED BY : SOMYA BHATNAGAR
SESSION -2017-18