Thursday, 10 October 2013

PROGRAM OF DOUBLE ENDED QUEUE IN C++

               DOUBLE ENDED QUEUE              

#include<iostream.h>
#include<conio.h>
#include<process.h>
int arr[10];
int max=10,left=-1,right=-1;
void inr()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"\noverload";
exit(0);
}
else if(left==-1)
{
left=0;
right=0;
}
else if(right==max-1)
right=0;
else
right=right+1;
arr[right]=data;
}
void inl()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"overflow";
exit(0);
}
if(left==-1)
{
left=0;
right=0;
}
else if(left==0)
left=max-1;
else
left=left-1;
arr[left]=data;
}
void view()
{
if(left!=-1)
{
cout<<"Queue is :";
for(int i=0;i<max;i++)
{
if(arr[i])
cout<<arr[i]<<endl;
}
}
}
void der()
{
if(left==-1)
{
cout<<"underflow";
exit(0);
}
arr[right]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(right==0)
right=max-1;
else
right=right+1;
}
void del()
{
if(left==-1)
{
cout<<"Underflow";
exit(0);
}
arr[left]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(left==max-1)
left=0;
else
left=left+1;
}
void main()
{
clrscr();
char ans;
int ch;
cout<<"MENU";
cout<<"\n1).Insert in right";
cout<<"\n2).Insert in left";
cout<<"\n3).Delete from right";
cout<<"\n4).Delete from left";
do
{
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
      inr();
      view();
      break;
case 2:
       inl();
       view();
       break;
case 3:
       der();
       view();
       break;
case 4:
       del();
       view();
       break;
default: cout<<"\n you made a wrong choice";
}
cout<<"Do you want more y for yes : ";
cin>>ans;
}while(ans=='y');
getch();
}



 

PROGRAM OF BINARY SEARCH THROUGH C++

           PROGRAM OF  BINARY SEARCH                         THROUGH C++.                            

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5];
int max=5;
int n;
cout<<" Enter the elements of array";
for(int i=0;i<max;i++)
{
cin>>arr[i];
}
cout<<"Enter the elements to search";
cin>>n;
int lb=0,ub=max-1;
int mid=(lb+ub)/2;
while(lb<=ub && arr[mid]!=n)
{
if(n<arr[mid])
ub=mid-1;
else
lb=mid+1;
mid=(lb+ub)/2;
}
if(arr[mid]==n)
cout<<"\nSearch is Successful. "<<n<<" is found at "<<mid+1;
else
cout<<"\nUnSuccessful.";
getch();
}
 

Tuesday, 14 May 2013

 
SUM OF 2 COMPLEX NUMBER 
  #include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  struct complex
  {
      int real;
      int img;
  };
  int main()
  {
      struct complex a, b, c;
      printf(" Enter a and b where a + ib is the first complex number. ");
      printf("\n a = ");
      scanf(" %d ", &a.real);
      printf(" b = ");
      scanf(" %d ", &a.img);
      printf(" Enter c and d where c + id is the second complex number. ");
      printf("\n c = ");
      scanf(" %d ", &b.real);
      printf(" d = ");
      scanf(" %d ", &b.img);
      c.real = a.real + b.real;
      c.img = a.img + b.img;
      if ( c.img >= 0 )
          printf(" Sum of two complex numbers = %d + %di ", c.real, c.img);
      else
          printf(" Sum of two complex numbers = %d %di ", c.real, c.img);
      getch();
      return 0;
  }