You are on page 1of 28

Student Details

#include<iostream.h>
#include<conio.h>
class stud
{
!
int regno,marks[3],total;
!
char name[10];
!
public:
!
void in()
!
{
!
!
cout<<"\n\nEnter the name:";
!
!
cin>>name;
!
!
cout<<"Enter the regno:";
!
!
cin>>regno;
!
!
cout<<"Enter the marks in three subjects:";
!
!
for(int i=0;i<3;i++)
!
!
{
!
!
!
cin>>marks[i];
!
!
}
!
}
!
void out()
!
{
!
!
total=marks[0]+marks[1]+marks[2];
!
!
cout<<"Total="<<total;
!
}
};
void main()
{
!
stud s[3];
!
clrscr();
!
for(int i=0;i<3;i++)
!
{
!
!
s[i].in();
!
!
s[i].out();
!
}
!
getch();
};

Linear Search
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
!
int a[50],e,n;
!
clrscr();
!
cout<<"Enter the no of elements of the array:";
!
cin>>n;
!
for(int i=0;i<n;i++)
!
{
!
!
cout<<"Enter element"<<i+1<<":";
!
!
cin>>a[i];
!
}
!
cout<<"Enter the element to be searched for:";
!
cin>>e;
!
for(i=0;i<n;i++)
!
{
!
!
if(e==a[i])
!
!
{
!
!
!
cout<<"Element found at "<<i+1<<" positon";
!
!
!
getch();
!
!
!
exit(0);
!
!
}
!
}
!
cout<<"Element not found";
!
getch();
}

Sentinal Search
#include<iostream.h>
#include<conio.h>
void main()
{
!
int a[50],n,i,e;
!
clrscr();
!
cout<<"Enter the number of elements:";
!
cin>>n;
!
cout<<"Enter the elements:";
!
for(i=0;i<n;i++)
!
!
cin>>a[i];
!
cout<<"Enter the element to be found.";
!
cin>>e;
!
a[n]=e;
!
i=0;
!
while(e!=a[i])
!
!
i++;
!
if(i<n)
!
!
cout<<"Element found at "<<i+1<<" position.";
!
else
!
!
cout<<"Element not found";
!
getch();
}

Probability Search
#include<iostream.h>
#include<conio.h>
void main()
{
!
int a[50],n,i,e,temp;
!
clrscr();
!
cout<<"Enter the number of elements:";
!
cin>>n;
!
cout<<"Enter the elements:";
!
for(i=0;i<n;i++)
!
!
cin>>a[i];
!
cout<<"Enter the element to be found.";
!
cin>>e;
!
i=0;
!
while(i<n&&e!=a[i])
!
!
i++;
!
if(e==a[i])
!
{
!
!
cout<<"Element found at "<<i+1<<" position";
!
!
if(i!=0)
!
!
{
!
!
!
temp=a[i+1];
!
!
!
a[i+1]=a[i];
!
!
!
a[i]=temp;
!
!
!
i--;
!
!
}
!
}
!
else
!
!
cout<<"Element not found";
!
getch();
}

Ordered List Search


#include<iostream.h>
#include<conio.h>
void main()
{
!
int a[50],n,i,e;
!
clrscr();
!
cout<<"Enter the number of elements:";
!
cin>>n;
!
cout<<"Enter the elements:";
!
for(i=0;i<n;i++)
!
!
cin>>a[i];
!
cout<<"Enter the element to be found:";
!
cin>>e;
!
if(e<=a[n-1])
!
{
!
!
i=0;
!
!
while(e>a[i])
!
!
!
i++;
!
}
!
else
!
!
cout<<"Element not found.";
!
if(e==a[i])
!
!
cout<<"Element found at "<<i+1<<" position";
!
getch();
}

Binary Search
#include<iostream.h>
#include<conio.h>
void main()
{
!
int a[50],i,e,first=0,last,mid;
!
clrscr();
!
cout<<"Enter the number of elements:";
!
cin>>last;
!
cout<<"Enter the elements:";
!
for(i=0;i<last;i++)
!
!
cin>>a[i];
!
cout<<"Enter the element to be found:";
!
cin>>e;
!
while(first<=last)
!
{
!
!
mid=(first+last)/2;
!
!
if(e>a[mid])
!
!
!
first=mid+1;
!
!
else if(e<a[mid])
!
!
!
last=mid-1;
!
!
else
!
!
!
first=last+1;
!
}
!
if(e==a[mid])
!
!
cout<<"Element found at "<<mid+1<<" position.";
!
else
!
!
cout<<"Element not found.";
!
getch();
}

Merge Sort
#include<iostream.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low, int high)
{
!
int mid;
!
if(low<high)
!
{
!
!
mid=(low+high)/2;
!
!
merge_sort(low,mid);
!
!
merge_sort(mid+1,high);
!
!
merge(low,mid,high);
!
}
}
void merge(int low,int mid,int high)
{
!
int h,i,j,b[50],k;
!
h=low;
!
i=low;
!
j=mid+1;
!
while((h<=mid)&&(j<=high))
!
{
!
!
if(a[h]<=a[j])
!
!
{
!
!
!
b[i]=a[h];
!
!
!
h++;
!
!
}
!
!
else
!
!
{
!
!
!
b[i]=a[j];
!
!
!
j++;
!
!
}
!
!
i++;
!
}
!
if(h>mid)
!
{
!
!
for(k=j;k<=high;k++)
!
!
{
!
!
!
b[i]=a[k];
!
!
!
i++;
!
!
}
!
}
!
else
!
{
!
!
for(k=h;k<=mid;k++)
!
!
{
!
!
!
b[i]=a[k];
!
!
!
i++;

!
!
}
!
}
!
for(k=low;k<=high;k++)
!
!
a[k]=b[k];
}
void main()
{
!
int num,i;
!
clrscr();
!
cout<<"Enter the no of elements in the array:";
!
cin>>num;
!
cout<<"Enter the elements:";
!
for(i=1;i<=num;i++)
!
!
cin>>a[i];
!
merge_sort(1,num);
!
cout<<"\nSorted array is:";
!
for(i=1;i<=num;i++)
!
!
cout<<a[i]<<" ";
!
getch();
}

Hashing
#include<iostream.h>
#include<conio.h>
#include<process.h>
void menu()
{
!
cout<<"==========MENU===========";
!
cout<<"\n1.Direct hashing.";
!
cout<<"\n2.Subtraction.";
!
cout<<"\n3.Modulo Division.";
!
cout<<"\n4.Digit Exraction.";
!
cout<<"\n5.Mid Square.";
!
cout<<"\n6.Folding.";
!
cout<<"\n7.Rotation.";
!
cout<<"\n8.Pseudo random.";
!
cout<<"\n9.Exit.";
!
cout<<"\nEnter your choice:";
}
void direct()
{
!
long int key;
!
cout<<"Enter the key:";
!
cin>>key;
!
cout<<"address"<<key;
}
void sub()
{
!
long int key,add;
!
cout<<"\nEnter your 10 digit reg no:";
!
cin>>key;
!
add=key-1031040000;
!
cout<<"\nYour roll no is:"<<add;
}
void mod()
{
!
long int key,add;
!
cout<<"\nGive the key:";
!
cin>>key;
!
add=key%3;
!
cout<<"\nThe address is: "<<add;
}
void digit()
{
!
long int key,add;
!
cout<<"\nEnter a 6 digit no:";
!
cin>>key;
!
key/=100;
!
add=key%100;
!
cout<<"\nAddress afer exracting middle two digits: "<<add;
}

void mid()
{
!
long int key,add;
!
cout<<"Enter the key (bet 325-999):";
!
cin>>key;
!
key=key*key;
!
key/=100;
!
add=key%100;
!
cout<<"Address="<<add;
}
void folding()
{
!
long int a,b,c,key;
!
cout<<"\nEnter the key(6 digit):";
!
cin>>key;
!
a=key%100;
!
key/=100;
!
b=key%100;
!
key/=100;
!
c=key%100;
!
cout<<"\nAddress="<<(a+b+c);
}
void rotation()
{
!
long int key,add,a;
!
cout<<"Enter the key(6 digit):";
!
cin>>key;
!
a=key%10;
!
key/=10;
!
add=(a*100000)+key;
!
cout<<"\nAddress="<<add;
}
void rand()
{
!
long int key,add,b,c;
!
cout<<"Enter the key:";
!
cin>>key;
!
cout<<"Give b&c:";
!
cin>>b>>c;
!
add=(b*key)+c;
!
cout<<"\nAddress:"<<add;
}
void main()
{
!
clrscr();
!
int ch;
!
char c;
!
do
!
{
!
!
clrscr();
!
!
menu();
!
!
cin>>ch;

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
}

!
switch(ch)
!
{
!
!
case 1:direct();break;
!
!
case 2:sub();break;
!
!
case 3:mod();break;
!
!
case 4:digit();break;
!
!
case 5:mid();break;
!
!
case 6:folding();break;
!
!
case 7:rotation();break;
!
!
case 8:rand();break;
!
!
default:exit(0);
!
}
!
cout<<"\nContinue?(y/n)";
!
cin>>c;
}while(c=='y');
getch();

Maximum and Minimum In an array


#include<iostream.h>
#include<conio.h>
int a[100],max,min,mid;
void maxmin(int i,int j)
{
!
int max1,min1;
!
if(i==j)
!
!
max=min=a[i];
!
else if(i==j-1)
!
{
!
!
if(a[i]>a[j])
!
!
{
!
!
!
max=a[i];
!
!
!
min=a[j];
!
!
}
!
!
else
!
!
{
!
!
!
max=a[j];
!
!
!
min=a[i];
!
!
}
!
}
!
else
!
{
!
!
mid=(i+j)/2;
!
!
maxmin(i,mid);
!
!
max1=max;
!
!
min1=min;
!
!
maxmin(mid+1,j);
!
!
if(max<max1)
!
!
!
max=max1;
!
!
if(min>min1)
!
!
!
min=min1;
!
}
}
void main()
{
!
int n;
!
clrscr();
!
cout<<"\nEnter the number of elements:";
!
cin>>n;
!
cout<<"\nEnter the elements:";
!
for(int k=1;k<=n;k++)
!
!
cin>>a[k];
!
max=a[0];
!
min=a[0];
!
maxmin(1,n);
!
cout<<"\nMax="<<max;
!
cout<<"\nMin="<<min;
!
getch();

Linked Lists
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
void main()
{
node *head=NULL;
int info=0,node_number=0,counter=0;
int ch;
clrscr();
do
{
clrscr();
cout<<"\n\n";
cout<<"0.Quit\n";
cout<<"1.Insert at first\n";
cout<<"2.Traverse\n";
cout<<"3.Insert at last\n";
cout<<"4.Insert after specified number of node\n";
cout<<"5.Delete at first node\n";
cout<<"6.Delete at last node\n";
cout<<"7.Delete specified number of node\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 0 :
break;
case 1 :
!
cout<<"Enter any number:";
!
cin>>info;
!
cout<<"Input data:"<<info<<"Node No :"<<++counter;
!
node *temp;
!
temp=new node;
!
temp->data=info;
!
temp->next=head;
!
head=temp;
!
break;
case 2 :
!
node *temp1;
!
temp1=head;
!
if(temp1==NULL)
!
{
!
cout<<endl<<"The Linked list is empty"<<endl;
!
break;
!
}
!
else
!
{

!
!
!
!
!
!
!
!

cout<<"Linked List: ";


while(temp1!=NULL)
{ cout<<temp1->data<<" ";
!
temp1=temp1->next;
}
cout<<endl<<"Number of Nodes:"<<counter;
break;
}
case 3 : if(head==NULL)
!
{
!
!
cout<<"Enter Any Number:";
!
!
cin>>info;
!
!
cout<<"Input Data:"<<info<<"\nNumber of nodes:"<<+
+counter;
!
!
node* temp;
!
!
temp= new node;
!
!
temp->data=info;
!
!
temp->next=NULL;
!
!
head=temp;
!
!
!
!
!
!
+counter;
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

}
else
{
cout<<"Enter any number:";
cin>>info;
cout<<"Input data:"<<info<<"\nNumber of nodes:"<<+
node *temp1;
temp1=new node;
temp1=head;
while(temp1->next!=NULL)
temp1=temp1->next;
node *temp;
temp= new node;
temp->data=info;
temp->next=NULL;
temp1->next=temp;
break;
}

case 4: if(head==NULL)
{
cout<<"The linked list is empty";
break;
}
cout<<"Enter any number";
cin>>info;
cout<<"Input data:"<<info<<endl;
cout<<"Enter the node number:";
cin>>node_number;
temp1=head;
for(int i=1;i<node_number;i++)

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

{
temp1=temp1->next;
if( temp1==NULL)
{
!
cout<<node_number<<"node is not exist"<<endl;
!
break;
}
}
if(i==node_number)
{
node*temp2=new node;
temp2->data=info;
temp2->next=temp1->next;
temp1->next=temp2;
cout<<"Number of nodes:"<<++counter;
break;
}
break;
case 5 : if(head==NULL)
{
cout<<"The linked list is empty"<<endl;
break;
}
else if(head->next==NULL)
{
head=NULL;
cout<<"\nThe first node of the linked list is deleted";
cout<<"\nThe linked list is empty";
counter--;
cout<<"\nNumber of nodes:"<<counter;
break;
}
else
{
temp= head;
head=temp->next;
delete(temp);
cout<<"\nThe fist node of the linked list is deleted";
counter--;
cout<<"\nNumber of nodes:"<<counter;
break;
}
case 6 : if(head==NULL)
{
cout<<"The linked list is empty\n";
break;
}
else if(head->next==NULL)
{
head=NULL;
cout<<"The last node of the linked list is deleted\n";
cout<<"The linked list is empty\n";

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
\n";
!
!

counter--;
cout<<"\nNumber of nodes:"<<counter;
break;
}
temp1=head;
node *old_temp;
old_temp=new node;
while(temp1->next!=NULL)
{
old_temp=temp1;
temp1=temp1->next;
}
old_temp->next=NULL;
delete(temp1);
cout<<"The last node of the linked list is deleted\n";
counter--;
cout<<"\nNumber of nodes:"<<counter;
break;
case 7: if(head==NULL)
{
cout<<"Linked list is empty\n";
break;
}
else if(head->next==NULL)
{
head=NULL;
cout<<"The last node of the linked list is deleted\n";
cout<<"Linked list is empty\n";
counter--;
cout<<"\nNumber of nodes:"<<counter;
break;
}
cout<<"Enter the node number";
cin>>node_number;
if(node_number>counter)
{
cout<<"No such node exists";
break;
}
temp1=head;
old_temp=temp1;
if(node_number==1)
{
head=temp1->next;
delete(temp1);
counter--;
cout<<"\nNumber of nodes:"<<counter;
cout<<node_number<<"node of the linked list is deleted
break;
}

!
!
!
!
!
!
!
!
!
!
!

for(i=1;i<node_number;i++)
{
old_temp=temp1;
temp1=temp1->next;
}
old_temp->next=temp1->next;
delete(temp1);
counter--;
cout<<"\nNumber of nodes:"<<counter;
cout<<node_number<<"node of the linked list is deleted\n";
break;

}getch();
}while(ch!=0);
}

Stack Operations
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
void main()
{
node *head=NULL;
int info=0,node_number=0,counter=0;
int ch;
clrscr();
do
{
clrscr();
cout<<"\n\n";
cout<<"0.Quit\n";
cout<<"1.push\n";
cout<<"2.pop\n";
cout<<"3.stack top\n";
cout<<"4.traverse\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 0 :
break;
case 1 :
!
cout<<"Enter data:";
!
cin>>info;
!
node *temp;
!
temp=new node;
!
temp->data=info;
!
temp->next=head;
!
head=temp;
!
counter++;
!
break;
!
!
!
!
!
!
!
!
!
!

case 2 : if(head==NULL)
{
cout<<"The stack is empty"<<endl;
break;
}
else
{
temp= head;
head=temp->next;
cout<<"Popped element is:"<<temp->data;
delete(temp);

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

counter--;
break;
}
case 3 : if(head==NULL)
{
cout<<"The stack is empty\n";
break;
}
cout<<"Top:"<<head->data;
break;
case 4 :
node *temp1;
temp1=head;
if(temp1==NULL)
{
cout<<endl<<"The stack is empty"<<endl;
break;
}
else
{
cout<<"stack: ";
while(temp1!=NULL)
{ cout<<temp1->data<<" ";
!
temp1=temp1->next;
}
cout<<endl<<"Number of Nodes:"<<counter;
break;
}
}getch();
}while(ch!=0);

Queues
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
void main()
{
node *front=NULL;
node *rear=NULL;
int info=0,counter=0;
int ch;
do
{
clrscr();
cout<<"\n\n";
cout<<"\n";
cout<<"Queue menu\n";
cout<<"flflflflflflflflflfl\n";
cout<<"0.Quit\n";
cout<<"1.Enqueue\n";
cout<<"2.Traverse\n";
cout<<"3.Dequeue\n";
cout<<"4.Queue front\n";
cout<<"5.Queue count\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 0 :
break;
case 1 :
!
cout<<"Enter any number:";
!
cin>>info;
!
counter++;
!
node *temp;
!
temp=new node;
!
temp->data=info;
!
temp->next=NULL;
!
rear->next=temp;
!
rear=temp;
!
if(front==NULL)
!
{
!
!
front=temp;
!
}
!
break;
case 2 :
!
node *temp1=front;
!
if(temp1==NULL)
!
{

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

cout<<endl<<"The queue is empty"<<endl;


break;
}
else
{
cout<<"Queue: ";
while(temp1!=NULL)
{ cout<<temp1->data<<" ";
!
temp1=temp1->next;
}
cout<<endl<<"Number of Nodes:"<<counter;
break;
}
case 3 : if(front!=NULL)
{
!
node* temp;
!
temp= new node;
!
temp=front;
!
front=temp->next;
!
delete(temp);
!
counter--;
}
else
!
cout<<"Stack empty - Cant dequeue!";
break;

case 4: if(front==NULL)
!
{
!
cout<<"The queue is empty";
!
break;
!
}
!
cout<<"\nFront="<<front->data;break;
case 5 : if(front==NULL)
!
{
!
cout<<"The linked list is empty"<<endl;
!
break;
!
}
!
cout<<"\nCount="<<counter;
!
break;
}getch();
}while(ch!=0);
}

Quicksort
#include<iostream.h>
#include<conio.h>
int a[100];
void interchange(int t,int u)
{
int temp;
temp=a[t];
a[t]=a[u];
a[u]=temp;
}
int partition(int m ,int n)
{
int v=a[m];
int i=m,j=n;
do
{
do
i++;
while(a[i]<v);
do
j--;
while(a[j]>v);
if(i<j)
interchange(i,j);
}while(i<j);
a[m]=a[j];
a[j]=v;
return j;
}
void quicksort(int p,int q)
{
if(p<q)
{
int j=partition(p,q+1);
quicksort(p,j-1);
quicksort(j+1,q);
}
}
void main()
{
int n;
clrscr();
cout<<"\n---QUICK SORT---\n";
cout<<"\nEnter the size of array(max 100)";
cin>>n;
cout<<"\nEnter Array Elements";
for(int l=1;l<=n;l++)
cin>>a[l];
quicksort(1,n);

cout<<"\n---SORTED ARRAY---";
for(l=1;l<=n;l++)
cout<<a[l]<<" ";
getch();
}

Knapsack Problem
#include<iostream.h>
#include<conio.h>
void main()
{
!
int n,m,w[10],p[10],i,j;
!
float r[10],x[10],temp,u;
!
clrscr();
!
cout<<"\n";
!
cout<<"Knapsack problem\n";
!
cout<<"flflflflflflflflflflflflflflflfl\n";
!
cout<<"Enter the capacity of the knapsack:";
!
cin>>m;
!
cout<<"Enter the number of elements(max 10):";
!
cin>>n;
!
cout<<"Enter the weights:";
!
for(i=0;i<n;i++)
!
!
cin>>w[i];
!
cout<<"Enter the corresponding profit:";
!
for(i=0;i<n;i++)
!
!
cin>>p[i];
!
for(i=0;i<n;i++)
!
!
r[i]=(float)(p[i]/w[i]);
!
for(i=0;i<n-1;i++)
!
{
!
!
for(j=0;j<(n-i-1);j++)
!
!
{
!
!
!
if(r[i]<r[i+1])
!
!
!
{
!
!
!
!
temp=r[i];
!
!
!
!
r[i]=r[i+1];
!
!
!
!
r[i+1]=temp;
!
!
!
!
temp=w[i];
!
!
!
!
w[i]=w[i+1];
!
!
!
!
w[i+1]=temp;
!
!
!
!
temp=p[i];
!
!
!
!
p[i]=p[i+1];
!
!
!
!
p[i+1]=temp;
!
!
!
}
!
!
}
!
}
!
cout<<"\nThe objects reordered are:";
!
cout<<"\nWeights:";
!
for(i=0;i<n;i++)
!
!
cout<<w[i]<<" ";
!
cout<<"\nProfit :";
!
for(i=0;i<n;i++)
!
!
cout<<p[i]<<" ";
!
u=m;
!
for(i=0;i<n;i++)
!
{

!
!
!
!
!
!
!
!
!
!
!
}

!
if(w[i]>u)
!
!
break;
!
x[i]=1.0;
!
u=u-w[i];
}
if(i<n)
!
x[i]=u/w[i];
cout<<"\nThe objects are selected as:";
for(i=0;i<n;i++)
!
cout<<x[i]<<" ";
getch();

Insertion Sort
#include<iostream.h>
#include<conio.h>
void main()
{
!
int n,a[50],current,hold,walker,i;
!
clrscr();
!
cout<<"\n";
!
cout<<"Insertion Sort\n";
!
cout<<"flflflflflflflflflflflflflfl\n";
!
cout<<"\nEnter the number of elements in the array:";
!
cin>>n;
!
cout<<"\nEnter the elements\n";
!
for(i=1;i<=n;i++)
!
!
cin>>a[i];
!
for(current=2;current<=n;current++)
!
{
!
!
hold=a[current];
!
!
for(walker=current-1;walker>0&&hold<a[walker];walker--)
!
!
!
a[walker+1]=a[walker];
!
!
a[walker+1]=hold;
!
}
!
cout<<"\nSorted array is:";
!
for(i=1;i<=n;i++)
!
!
cout<<a[i]<<" ";
!
getch();
}

Selection Sort
#include<iostream.h>
#include<conio.h>
void main()
{
!
int n,a[50],current,temp,walker,i,smallest;
!
clrscr();
!
cout<<"\n";
!
cout<<"Selection Sort\n";
!
cout<<"flflflflflflflflflflflflflfl\n";
!
cout<<"\nEnter the number of elements in the array:";
!
cin>>n;
!
cout<<"\nEnter the elements\n";
!
for(i=0;i<n;i++)
!
!
cin>>a[i];
!
for(current=0;current<n;current++)
!
{
!
!
smallest=current;
!
!
for(walker=current+1;walker<n;walker++)
!
!
!
if(a[walker]<a[smallest])
!
!
!
!
smallest=walker;
!
!
temp=a[current];
!
!
a[current]=a[smallest];
!
!
a[smallest]=temp;
!
}
!
cout<<"\nSorted array is:";
!
for(i=0;i<n;i++)
!
!
cout<<a[i]<<" ";
!
getch();
}

You might also like