Professional Documents
Culture Documents
Binary Search
Binary Search
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[50], n,p;
cout<<"Enter the Size of array : ";
cin>>n;
cout<<"Enter the elements in ascending or descending order : ";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\nArray formed is : ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\nEnter the element to be searched : ";
cin>>p;
void binary_search(int A[], int n, int p);
binary_search(A,n,p);
getch();
}
while(L<=U)
{ mid=(L+U)/2;
if(A[mid]==p)
{ cout<<"\nElement "<<p<<" found. Search Successful.";
cout<<"\nSubscript = "<<mid<<" \nPosition = "<<mid+1;
break;
}
else if(p<=A[mid])
U=mid-1;
else
L=mid+1;
}
if(L>U)
{cout<<"\nUnsuccessful search.";
cout<<"\nWant to search again. : ";
cin>>ch;
if(ch=='y'||ch=='Y')
{cout<<"\nEnter the element to be searched : ";
cin>>p;
goto lb;}
}
}
Output:
Enter the Size of array : 5
Enter the elements ascending or descending order:
2
5
8
9
11
Array formed is : 2 5 8 9 11
Enter the element to be searched : 8
public :
void input()
{ cout<<"\nEnter Book no. : "; cin>>book_no;
cout<<"\nEnter book title : ";
cin.get();
cin.getline(book_title,80);
cout<<"\nEnter book price : "; cin>>price;
}
void purchase()
{ cout<<"\nEnter number of copies to be purchased : "; cin>>p;
cout<<"\nTotal cost (in Rs ) = "<<total_cost(p)<<"\n";
}
void display()
{ cout<<"\nBook title is : "<<book_title;
cout<<"\nBook number is : "<<book_no;
cout<<"\nPrice of book is : "<<price;
cout<<"\nTotal cost of "<<p<<" books => "<<total_cost(p)<<" Rs\n";
} };
void main()
{clrscr();
book b[3],p[3];
fstream bo;
cout<<"---------------------------------------------------------Enter Details----------------------------------------------“;
for(int i=0; i<3; i++)
{
b[i].input();
b[i].purchase();
bo.open("xy.txt",ios::out|ios::binary);
bo.write((char *)&b[i],sizeof(b[i]));
bo.close();
bo.open("xy.txt",ios::in |ios::binary);
bo.read((char *)&p[i], sizeof(p[i]));
bo.close();
}
cout<<"\n--------------------------------------------------- Book Details --------------------------------------------------
“;
for(i=0; i<3; i++)
{ p[i].display(); }
getch();
}
Output:
--------------------------------------------------------------------- Enter Details ----------------------------------------------
#include<iostream.h>
#include<conio.h>
const int size=50;
int q[size],rear=0,front=0;
char ch;
void insert(int q[],int ele)
{
if(rear>=size)
{ cout<< “queue is full”;getch(); }
else if(rear==0)
{
front=rear=1;
q[rear]=ele;
}
else
{
rear++;
q[rear]=ele;
};
}
void main()
{
clrscr();
int n,u,m;
char ans;
do
{
cout<<"\nChoose from the menu :\n"
<<"\n 1. Insert"
<<"\n 2. Delete"
<<"\n 3. Display"
<<"\n\n Enter your choice : "; cin>>n;
switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted :"; cin>>m;
insert(q,m);
cout<<"\n The resultant Queue is : ";
display(q,front,rear);
cout<<"\n\n Want to enter again ?: ";
cin>>ans;
}
break;
case 2: ans='y';
while(ans=='y'||ans=='Y')
{
u=delet(q);
{ cout<<"\n The deleted element is "<<u<<"\n";
cout<<"\n The resultant Queue is : \n\n";
display(q,front,rear);
}
cout<<"\n\n Want to delete again ?: ";
cin>>ans;
}
break;
} while(ch=='y'||ch=='Y'); getch();
}
Output:
Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 1
#include<iostream.h>
#include<conio.h>
int fibonacci(int n);
void main()
{
clrscr();
int n;
cout<<"\n Enter the number of terms upto which you want the sum of fibonnaci series : ";
cin>>n;
cout<<"\nThe fibonacci series generated is : \n";
cout<<"1 1 ";
int s=fibonacci(n);
cout<<"\nThe sum of fibonacci series for "<<n<<" terms = "<<s;
getch();
}
int first=1;
int second=1;
int third;
int i=2;
int sum=0;
int fibonacci(int n)
{ if(n==1)
sum=1;
else if(n==2)
sum=2;
else
if(n>1 && i!=n)
{ third=first+second;
cout<<third<<” “;
if(i==2)
sum+=first+second+third;
else
sum+=third;
first=second;
second=third;
++i;
fibonacci(n);}
return sum ;}
Output :
Enter the number of terms upto which you want the sum of fibonnaci series : 5
The fibonacci series generated is :
1 1 2 3 5
The sum of fibonacci series for 5 terms = 12
/* WAP in c++ to perform basic operations on the two complex numbers using structures. */
#include<iostream.h>
#include<math.h>
#include<conio.h>
struct complex {
int i,r;
}x,y;
do
{cout<<"\nChoose from the folowing : ";
cout<<"\n1. Add two complex numbers ";
cout<<"\n2. Subtract two complex numbers ";
cout<<"\n3. Multiply two complex numbers ";
cout<<"\n4. Divide two complex numbers ";
cout<<"\nEnter your choice : "; cin>>choice;
switch(choice)
{
case 1:add(x,y); break;
case 2:subtract(x,y); break;
case 3:multiply(x,y); break;
case 4:divide(x,y); break;
}
cout<<"\nWant to Choose again -> ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
Output:
Complex number 1
Enter the real part : 2
Enter the imaginary part : 5
Complex number 1 -> 2+(5)i
Complex number 2 :
Enter the real part : 1
Enter the imaginary part : 3
Complex number 2 -> 1+(3)i
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float p)
{
return 3.14*p*p;
}
float area(int p)
{
return p*p;
}
void main()
{clrscr();
int p,q,ch;
float a,b,r;
char choice;
do{
cout<<"\nChoose from the following : ";
cout<<"\n1. Area of square ";
cout<<"\n2. Area of circle ";
cout<<"\n3. Area of rectangle ";
cout<<"\n4. Area of right triangle ";
cout<<"\n5. Area of triangle ";
cout<<"\nEnter your choice : "; cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter side of square : ";
cin>>p;
cout<<"area of square is : "<<area(p);
break;
case 2: cout<<"\nEnter radius of circle : ";
cin>>a;
cout<<"area of circle is : "<<area(a);
break;
}
cout<<"\nWant to choose again : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
}
Output:
node *create(int a)
{
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}
void delet()
{
if(front==NULL)
cout<<"\n Underflow!!!!";
else
{
ptr=front;
front=front->next;
delete ptr;
}
}
void main()
{
clrscr();
front=rear=NULL;
int n,m;
char ans,ch;
do
{ cout<<"\nChoose from the menu : "
<<"\n 1) Insert."
<<"\n 2) Delete
<<"\n 3) Display."
<<"\n\n Enter your choice : ";
cin>>n;
switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);
if(newptr==NULL)
cout<<"\n Cannot create !!!!";
insert(newptr);
display(front);
case 2: ans='y';
while(ans=='y'||ans=='Y')
{
delet();
cout<<"\n Queue : ";
display(front);
cout<<"\n Want to delete more ?: ";
cin>>ans;
}
break;
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node {
int roll;
node* next;
}*top,*save,*ptr,*newptr,*np;
node *create(int a)
{
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}
void pop()
{
if(top==NULL)
cout<<"\n Underflow!!!!";
else
{
ptr=top;
top=top->next;
delete ptr;
}
}
void main()
{
clrscr();
top=NULL;
int n,m;
char k,ch;
do {
cout<<"\nChoose from the menu :\n"
<<"\n 1. Push."
<<"\n 2. Pop."
<<"\n 3. Display."
<<"\n 4. Quit."
<<"\n\nEnter your choice : ";
cin>>n;
switch(n)
{
case 1: k='y';
while(k=='y'||k=='Y')
{
cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);
if(newptr==NULL)
cout<<"\n Cannot create !!!!";
push(newptr);
cout<<"\n The Stack formed is : ";
display(top);
case 2: k='y';
while(k=='y'||k=='Y')
{
pop();
cout<<"\n The Stack formed is : \n\n";
display(top);
cout<<"\n\n Want to delete again ?: ";
cin>>k;
}
break;
case 4: exit(0);
break;
Output:
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.
getch(); }
Output :
Enter the number of elements : 5
Enter the elements :
1
9
2
8
6
The array formed is :
1 9 2 8 6
The smallest element is : 1
The largest element is : 9
/* wap using pointers to find the length of a string and print the reversed string . */
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char *str= new char[256];
char intermediate;
*(str+j)=*(str+i);
*(str+i)=intermediate;
}
cout<<str;
getch();
}
Output :