#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void search();
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----SEARCH.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
search();
getch();
break;
case '3':
exit(0);
break;
}
} while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
elseif(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
int t=0;
void src(node *tree,int);
void search()
{
int sr;
cout<<"Enter data to search\n";
cin>>sr;
src(tree,sr);
if(t==1)
cout<<"Element found";
else
cout<<"Not Found";
t=0;
}
void src(node *tree,int sr)
{
if(tree!=NULL)
{
if(tree->data==sr)
{
t=1;
}
src(tree->left,sr);
src(tree->right,sr);
}
}
Saturday, November 13, 2010
Search in linked list
#include"iostream.h"
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
Queue Operation
#include<conio.h>
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
Stack Operation using array
#include
#include
#include
class stack
{
private:
int stack_array[10];
int top;
public:
stack()
{
top = -1;
}
int full()
{
if(top == 9)
return(1);
else
return(0);
}
int empty()
{
if(top == -1)
return(1);
else
return(0);
}
void push(int passed_value)
{
top++;
stack_array[top]=passed_value;
cout<=0;i--)
{
cout<>choice;
switch(choice)
{
case 1:
if(mystack.full())
{
cout<<"\nStack Full"; break; } cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
mystack.pop();
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
#include
#include
class stack
{
private:
int stack_array[10];
int top;
public:
stack()
{
top = -1;
}
int full()
{
if(top == 9)
return(1);
else
return(0);
}
int empty()
{
if(top == -1)
return(1);
else
return(0);
}
void push(int passed_value)
{
top++;
stack_array[top]=passed_value;
cout<
{
cout<
switch(choice)
{
case 1:
if(mystack.full())
{
cout<<"\nStack Full"; break; } cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
mystack.pop();
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
Stack Operation Using Linked List
#include
#include
#include
class node
{
public:
int info;
node *next;
};
class stack
{
private:
node *top;
public:
stack()
{
top=NULL;
}
int empty()
{
if(top==NULL)
return(1);
else
return(0);
}
void push(int passed_value)
{
node *temp;
if(top==NULL)
{
top=new node;
top->info=passed_value;
top->next=NULL;
}
else
{
temp=new node();
temp->info=passed_value;
temp->next=top;
top=temp;
}
cout<<"\n"<next;
int x=tmp->info;
delete(tmp);
return(x);
}
void display()
{
if(top==NULL)// if stack is empty
cout<<"\nStack Empty\n"; else { node *currentnode; cout<<"The Contents of the Stack are:\n"; for(currentnode=top;currentnode!=NULL;currentnode=currentnode->next)
cout<info<<"\t";
}
}
};
void main()
{
clrscr();
int choice,x;
stack mystack;
while(1)
{
cout<<"\n**STACK MENU**\n\n"
<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n"
<<"Enter ur Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
x=mystack.pop();
cout<<"\nThe Popped element is : "<
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
#include
#include
class node
{
public:
int info;
node *next;
};
class stack
{
private:
node *top;
public:
stack()
{
top=NULL;
}
int empty()
{
if(top==NULL)
return(1);
else
return(0);
}
void push(int passed_value)
{
node *temp;
if(top==NULL)
{
top=new node;
top->info=passed_value;
top->next=NULL;
}
else
{
temp=new node();
temp->info=passed_value;
temp->next=top;
top=temp;
}
cout<<"\n"<
int x=tmp->info;
delete(tmp);
return(x);
}
void display()
{
if(top==NULL)// if stack is empty
cout<<"\nStack Empty\n"; else { node *currentnode; cout<<"The Contents of the Stack are:\n"; for(currentnode=top;currentnode!=NULL;currentnode=currentnode->next)
cout<
switch(choice)
{
case 1:
cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
x=mystack.pop();
cout<<"\nThe Popped element is : "<
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
To display numbers in triangular format
#include"iostream.h"
#include"conio.h"
void main()
{
int n,i,j;
clrscr();
cout<<"\n\t\tENTER RANGE:";
cin>>n;
cout<<"\nFORMAT - 1\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<j;
}
cout<<"\n";
}
cout<<"\nFORMAT - 2\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<i;
}
cout<<"\n";
}
cout<<"\nFORMAT - 3\n";
for(i=0;i<=n-1;i++)
{
for(j=0;j<=i;j++)
{
cout<<"\t"<<(n-j);
}
cout<<"\n";
}
getch();
}
#include"conio.h"
void main()
{
int n,i,j;
clrscr();
cout<<"\n\t\tENTER RANGE:";
cin>>n;
cout<<"\nFORMAT - 1\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<j;
}
cout<<"\n";
}
cout<<"\nFORMAT - 2\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<i;
}
cout<<"\n";
}
cout<<"\nFORMAT - 3\n";
for(i=0;i<=n-1;i++)
{
for(j=0;j<=i;j++)
{
cout<<"\t"<<(n-j);
}
cout<<"\n";
}
getch();
}
Student details using multiple inheritance
#include<iostream.h>
#include<conio.h>
class basic_info
{
public:
int roll;
char name[20],gender;
};
class academic_fit
{
public:
char course[10],semester[2],rank[2];
};
class financial_assist:public basic_info, public academic_fit
{
public:
float amt;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\t Course :";
cin>>course;
cout<<"\t Semester :";
cin>>semester;
cout<<"\t Rank : ";
cin>>rank;
cout<<"\t Amt Paid :";
cin>>amt;
}
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Course : "<<course;
cout<<"\n\t Semester: "<<semester;
cout<<"\n\t Rank : "<<rank;
cout<<"\n\t Amt.Paid: "<<amt;
}
};
void main()
{
financial_assist s;
clrscr();
s.getdata();
s.disp();
getch();
}
#include<conio.h>
class basic_info
{
public:
int roll;
char name[20],gender;
};
class academic_fit
{
public:
char course[10],semester[2],rank[2];
};
class financial_assist:public basic_info, public academic_fit
{
public:
float amt;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\t Course :";
cin>>course;
cout<<"\t Semester :";
cin>>semester;
cout<<"\t Rank : ";
cin>>rank;
cout<<"\t Amt Paid :";
cin>>amt;
}
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Course : "<<course;
cout<<"\n\t Semester: "<<semester;
cout<<"\n\t Rank : "<<rank;
cout<<"\n\t Amt.Paid: "<<amt;
}
};
void main()
{
financial_assist s;
clrscr();
s.getdata();
s.disp();
getch();
}
Student details using single inheritance
#include<iostream.h>
#include<conio.h>
class studentbase
{
public:
int roll,height,weight;
char name[20],gender;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\tHeight(in cm) :";
cin>>height;
cout<<"\tWeight(in Kg) :";
cin>>weight;
}
};
class studderived:public studentbase
{
public:
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Height : "<<height<<" cm";
cout<<"\n\t Weight : "<<weight<<" Kg";
}
};
void main()
{
studderived s;
clrscr();
s.getdata();
s.disp();
getch();
}
#include<conio.h>
class studentbase
{
public:
int roll,height,weight;
char name[20],gender;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\tHeight(in cm) :";
cin>>height;
cout<<"\tWeight(in Kg) :";
cin>>weight;
}
};
class studderived:public studentbase
{
public:
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Height : "<<height<<" cm";
cout<<"\n\t Weight : "<<weight<<" Kg";
}
};
void main()
{
studderived s;
clrscr();
s.getdata();
s.disp();
getch();
}
Operator overloading
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float real,float img)
{
x=real;
y=img;
}
complex operator+(complex);
void disp(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::disp(void)
{
cout<<x<<"+i"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 = ";c1.disp();
cout<<"c2 = ";c2.disp();
cout<<"Sum= ";c3.disp();
getch();
}
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float real,float img)
{
x=real;
y=img;
}
complex operator+(complex);
void disp(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::disp(void)
{
cout<<x<<"+i"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 = ";c1.disp();
cout<<"c2 = ";c2.disp();
cout<<"Sum= ";c3.disp();
getch();
}
Adam number
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,sqr,n1,r=0,r1=0,rsq;
cout<<"Enter a number:";
cin>>n;
n1=n;
sqr=n*n;
while(sqr>0)
{
r=r*10+sqr%10;
sqr=sqr/10;
}
rsq=sqrt(r);
while(rsq>0)
{
r1=r1*10+rsq%10;
rsq=rsq/10;
}
if(r1==n1)
cout<<"\nThe Given Number is an Adam No";
else
cout<<"\nThe Given Number is not an Adam no";
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,sqr,n1,r=0,r1=0,rsq;
cout<<"Enter a number:";
cin>>n;
n1=n;
sqr=n*n;
while(sqr>0)
{
r=r*10+sqr%10;
sqr=sqr/10;
}
rsq=sqrt(r);
while(rsq>0)
{
r1=r1*10+rsq%10;
rsq=rsq/10;
}
if(r1==n1)
cout<<"\nThe Given Number is an Adam No";
else
cout<<"\nThe Given Number is not an Adam no";
getch();
}
Binary to decimal
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int p,q,r,bi[20];
long num;
cout<<"Enter Binary number:";
cin>>num;
p=0;
while(num>0)
{
bi[p]=num%10;
num=num/10;
p++;
}
q=p-1;
r=0;
while(q>=0)
{
r=r+(bi[q]*pow(2,q));
q--;
}
cout<<r;
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int p,q,r,bi[20];
long num;
cout<<"Enter Binary number:";
cin>>num;
p=0;
while(num>0)
{
bi[p]=num%10;
num=num/10;
p++;
}
q=p-1;
r=0;
while(q>=0)
{
r=r+(bi[q]*pow(2,q));
q--;
}
cout<<r;
getch();
}
String Palindrome
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s[10],a[10],b[10];
cout<<"enter any string:";
cin>>s;
strcpy(a,s);
strrev(a);
if(strcmp(a,s)==0)
cout<<"\nThe given String is Palindrome";
else
cout<<"\nThe given String is not Palindrome";
getch();
}
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s[10],a[10],b[10];
cout<<"enter any string:";
cin>>s;
strcpy(a,s);
strrev(a);
if(strcmp(a,s)==0)
cout<<"\nThe given String is Palindrome";
else
cout<<"\nThe given String is not Palindrome";
getch();
}
Armstrong number
#include<iostream.h>
#include<conio.h>
void main()
{
int n,n1,s=0,d;
clrscr();
cout<<"Enter the Value:";
cin>>n;
n1=n;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==n1)
{
cout<<"The given Number "<<n1<<" is an Armstrong Number";
}
else
{
cout<<"The given Number "<<n1<<" is not an Armstrong Number";
}
getch();
}
#include<conio.h>
void main()
{
int n,n1,s=0,d;
clrscr();
cout<<"Enter the Value:";
cin>>n;
n1=n;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==n1)
{
cout<<"The given Number "<<n1<<" is an Armstrong Number";
}
else
{
cout<<"The given Number "<<n1<<" is not an Armstrong Number";
}
getch();
}
Calculating time by incrementing seconds
#include<iostream.h>
#include<conio.h>
#include<time.h>
void main()
{
clrscr();
time_t t,t1;
t=time(NULL);
cout<<"The current time is:"<<ctime(&t);
t1=t;
cout<<"\nenter the seconds:";
int sec;
cin>>sec;
t1-=sec;
t+=sec;
cout<<"\nthe incremented time is:"<<ctime(&t);
cout<<"\nthe decremented time is:"<<ctime(&t1);
getch();
}
#include<conio.h>
#include<time.h>
void main()
{
clrscr();
time_t t,t1;
t=time(NULL);
cout<<"The current time is:"<<ctime(&t);
t1=t;
cout<<"\nenter the seconds:";
int sec;
cin>>sec;
t1-=sec;
t+=sec;
cout<<"\nthe incremented time is:"<<ctime(&t);
cout<<"\nthe decremented time is:"<<ctime(&t1);
getch();
}
Leap year calculating day of month
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
cout<<"Enter a Day number of Leap year : ";
cin>>n;
if(n>=1 && n<=31)
{
cout<<"\nJanuary";
cout<<"\nDay of month :"<<n;
}
else if(n>31 && n<=60)
{
cout<<"\nFeburary";
cout<<"\nDay of month :"<<n-31;
}
else if(n>60 && n<=91)
{
cout<<"\nMarch";
cout<<"\nDay of month :"<<n-60;
}
else if(n>91 && n<=121)
{
cout<<"\nApril";
cout<<"\nDay of month :"<<n-91;
}
else if(n>121&&n<=152)
{
cout<<"\nMay";
cout<<"\nDay of month :"<<n-120;
}
else if(n>152&&n<=182)
{
cout<<"\nJune";
cout<<"\nDay of month :"<<n-152;
}
else if(n>182&&n<=213)
{
cout<<"\nJuly";
cout<<"\nDay of month :"<<n-182;
}
else if(n>213&&n<=244)
{
cout<<"\nAugust";
cout<<"\nDay of month :"<<n-213;
}
else if(n>244&&n<=274)
{
cout<<"\nSeptember";
cout<<"\nDay of month :"<<n-244;
}
else if(n>274&&n<=305)
{
cout<<"\nOctober";
cout<<"\nDay of month :"<<n-274;
}
else if(n>305&&n<=335)
{
cout<<"\nNovember";
cout<<"\nDay of month :"<<n-305;
}
else if(n>335&&n<=366)
{
cout<<"\nDecember";
cout<<"\nDay of month :"<<n-335;
}
else
{
cout<<"\nEnter Day number of Leap year..";
}
getch();
}
#include<conio.h>
void main()
{
int n,i;
clrscr();
cout<<"Enter a Day number of Leap year : ";
cin>>n;
if(n>=1 && n<=31)
{
cout<<"\nJanuary";
cout<<"\nDay of month :"<<n;
}
else if(n>31 && n<=60)
{
cout<<"\nFeburary";
cout<<"\nDay of month :"<<n-31;
}
else if(n>60 && n<=91)
{
cout<<"\nMarch";
cout<<"\nDay of month :"<<n-60;
}
else if(n>91 && n<=121)
{
cout<<"\nApril";
cout<<"\nDay of month :"<<n-91;
}
else if(n>121&&n<=152)
{
cout<<"\nMay";
cout<<"\nDay of month :"<<n-120;
}
else if(n>152&&n<=182)
{
cout<<"\nJune";
cout<<"\nDay of month :"<<n-152;
}
else if(n>182&&n<=213)
{
cout<<"\nJuly";
cout<<"\nDay of month :"<<n-182;
}
else if(n>213&&n<=244)
{
cout<<"\nAugust";
cout<<"\nDay of month :"<<n-213;
}
else if(n>244&&n<=274)
{
cout<<"\nSeptember";
cout<<"\nDay of month :"<<n-244;
}
else if(n>274&&n<=305)
{
cout<<"\nOctober";
cout<<"\nDay of month :"<<n-274;
}
else if(n>305&&n<=335)
{
cout<<"\nNovember";
cout<<"\nDay of month :"<<n-305;
}
else if(n>335&&n<=366)
{
cout<<"\nDecember";
cout<<"\nDay of month :"<<n-335;
}
else
{
cout<<"\nEnter Day number of Leap year..";
}
getch();
}
Calculating Simple interest
#include<iostream.h>
#include<conio.h>
class simple
{
public:
float p,r,si,amt;
int n;
void getin()
{
cout<<"Enter Principal Amt:";
cin>>p;
cout<<"Enter Intrest Rate :";
cin>>r;
cout<<"Enter No. of Years :";
cin>>n;
si=(p*n*r)/100;
amt=p+si;
cout<<"\n\t*-*-*-*\n";
cout<<" Intrest : Rs."<<si<<"\n";
cout<<" Amount : Rs."<<amt;
}
};
void main()
{
clrscr();
simple s;
cout<<"*-*-*-*Calulate Simple Interest*-*-*-*\n\n";
s.getin();
getch();
}
#include<conio.h>
class simple
{
public:
float p,r,si,amt;
int n;
void getin()
{
cout<<"Enter Principal Amt:";
cin>>p;
cout<<"Enter Intrest Rate :";
cin>>r;
cout<<"Enter No. of Years :";
cin>>n;
si=(p*n*r)/100;
amt=p+si;
cout<<"\n\t*-*-*-*\n";
cout<<" Intrest : Rs."<<si<<"\n";
cout<<" Amount : Rs."<<amt;
}
};
void main()
{
clrscr();
simple s;
cout<<"*-*-*-*Calulate Simple Interest*-*-*-*\n\n";
s.getin();
getch();
}
Factorial of a given number
#include<iostream.h>
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
cout<<"Enter a Number:";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial :"<<fact;
getch();
}
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
cout<<"Enter a Number:";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial :"<<fact;
getch();
}
Hybrid inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void geta()
{
cout<<"Enter A: ";
cin>>a;
}
};
class B:public A
{
public:
int b;
void getb()
{
cout<<"Enter B: ";
cin>>b;
}
};
class D
{
public:
int sum;
};
class C:public D,public B
{
public:
void disp()
{
geta();
getb();
sum=a+b;
cout<<"A+B="<<sum;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a;
void geta()
{
cout<<"Enter A: ";
cin>>a;
}
};
class B:public A
{
public:
int b;
void getb()
{
cout<<"Enter B: ";
cin>>b;
}
};
class D
{
public:
int sum;
};
class C:public D,public B
{
public:
void disp()
{
geta();
getb();
sum=a+b;
cout<<"A+B="<<sum;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
Multi-level inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void disp()
{
getdata();
add();
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void disp()
{
getdata();
add();
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
Single inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
void disp()
{
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
B b;
clrscr();
b.getdata();
b.add();
b.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
void disp()
{
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
B b;
clrscr();
b.getdata();
b.add();
b.disp();
getch();
}
Student details using inside the class and outside the class
#include<iostream.h>
#include<conio.h>
class stud //Outside Class
{
public:
int regno,m1,m2,m3,tot;
float avg;
char name[20];
};
void main() //Main Function
{
clrscr();
stud s; //Object For Outside Class
class studin //Inside Class
{
public:
void stumark()
{
cout<<"Enter Student Details:";
cout<<"\n\tReg.No :";
cin>>s.regno;
cout<<"\tName:";
cin>>s.name;
cout<<"\tMark1:";
cin>>s.m1;
cout<<"\tMark1:";
cin>>s.m2;
cout<<"\tMark1:";
cin>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\n\tStudent Details";
cout<<"\n\t Number : "<<s.regno;
cout<<"\n\t Name : "<<s.name;
cout<<"\n\t Mark1 : "<<s.m1;
cout<<"\n\t Mark2 : "<<s.m2;
cout<<"\n\t Mark3 : "<<s.m3;
cout<<"\n\t Total : "<<s.tot;
cout<<"\n\t Average : "<<s.avg;
}
};
studin s1; //Object For inside Class
s1.stumark();
getch();
}
#include<conio.h>
class stud //Outside Class
{
public:
int regno,m1,m2,m3,tot;
float avg;
char name[20];
};
void main() //Main Function
{
clrscr();
stud s; //Object For Outside Class
class studin //Inside Class
{
public:
void stumark()
{
cout<<"Enter Student Details:";
cout<<"\n\tReg.No :";
cin>>s.regno;
cout<<"\tName:";
cin>>s.name;
cout<<"\tMark1:";
cin>>s.m1;
cout<<"\tMark1:";
cin>>s.m2;
cout<<"\tMark1:";
cin>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\n\tStudent Details";
cout<<"\n\t Number : "<<s.regno;
cout<<"\n\t Name : "<<s.name;
cout<<"\n\t Mark1 : "<<s.m1;
cout<<"\n\t Mark2 : "<<s.m2;
cout<<"\n\t Mark3 : "<<s.m3;
cout<<"\n\t Total : "<<s.tot;
cout<<"\n\t Average : "<<s.avg;
}
};
studin s1; //Object For inside Class
s1.stumark();
getch();
}
Employee Details using class & member function
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_no;
char emp_name[20];
float emp_sal;
public:
void get();
void dis();
};
void employee::get()
{
cout<<"Enter employee number:";
cin>>emp_no;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter salary:";
cin>>emp_sal;
}
void employee::dis()
{
cout<<"\n\tEmployee Details";
cout<<"\n\t Number : "<<emp_no;
cout<<"\n\t Name : "<<emp_name;
cout<<"\n\t Salary : "<<emp_sal;
}
void main()
{
clrscr();
employee e;
e.get();
e.dis();
getch();
}
#include<conio.h>
class employee
{
int emp_no;
char emp_name[20];
float emp_sal;
public:
void get();
void dis();
};
void employee::get()
{
cout<<"Enter employee number:";
cin>>emp_no;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter salary:";
cin>>emp_sal;
}
void employee::dis()
{
cout<<"\n\tEmployee Details";
cout<<"\n\t Number : "<<emp_no;
cout<<"\n\t Name : "<<emp_name;
cout<<"\n\t Salary : "<<emp_sal;
}
void main()
{
clrscr();
employee e;
e.get();
e.dis();
getch();
}
Sum of two numbers using increment operator
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i=1;
clrscr();
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
while(i<=b)
{
a=a++;
i++;
}
cout<<"The sum is:";
cout<<a;
getch();
}
#include<conio.h>
void main()
{
int a,b,i=1;
clrscr();
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
while(i<=b)
{
a=a++;
i++;
}
cout<<"The sum is:";
cout<<a;
getch();
}
Student Details Using Single Linked List
#include
#include
#include
#include
class node
{
public:
int rollnumber;
char name[20];
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnode()
{
int rollno;
char nm[20];
cout<<"\nEnter the roll number of the student:"; cin>>rollno;
cout<<"\nEnter the name of the student:"; cin>>nm;
node *newnode=new node;
newnode->rollnumber=rollno;
strcpy(newnode->name,nm);
if(start==NULL || rollno<=start->rollnumber)
/* If the node to be inserted is the first node */
{
if(start!=NULL && (rollno == start->rollnumber))
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } newnode->next=start;
start=newnode;
return;
}
node *previous,*current;
previous=start;
current=start;
while((current!=NULL) && (rollno>=current->rollnumber))
{
if(rollno==current->rollnumber)
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } previous=current; current=current->next;
}
newnode->next=current;
previous->next=newnode;
}
int listempty()
{
if(start==NULL)
return(1);
else
return(0);
}
int delnode(int rollno)
{
node *current,*previous;
if(search(rollno,&previous,¤t)==0)
return 0;
previous->next=current->next;
if(current==start)
start=start->next;
delete current;
return 1;
}
/* int search(int rollno,node **previous,node **current)
{
*previous=start;
*current=start;
while((*current!=NULL)&&((rollno!=current->rollnumber)))
{
*previous=*current;
*current=(*current)->next;
}
if(*current!=NULL)
return 1;
else
return 0;
}
*/ void traverse()
{
if(listempty())
cout<<"\nList is empty"; else { cout<<"The records in the list are:\n"; node *currentnode; for(currentnode=start;currentnode!=NULL;currentnode=currentnode->next)
{
cout<rollnumber<<"\t"<name<<"\n";
}
cout<>ch;
switch(ch)
{
case '1':
obj.addnode();
break;
case '2':
if(obj.listempty()==0)
{
cout<<"List Is Empty:"; break; } cout<<"\nEnter Rollno. to delete record:"; cin>>rollno;
if(obj.delnode(rollno)==0)
cout<<"Record not found"; else cout<<"Record deleted."; break; case '3': obj.traverse(); break; case '4': if(obj.listempty()==0) { cout<<"\nList Empty"; break; } node *previous,*current; cout<<"\nEnter rollno. to search:"; cin>>rollno;
if(obj.search(rollno,&previous,¤t)==0)
{
cout<<"\n Record not Found"; break; } else { cout<<"\n Record found\n"; cout<<"Roll Number: "<rollnumber;
cout<<"\nName: "<name;
}
break;
case '5':
exit(0);
default:
cout<<"Enter correct choice:";
break;
}
}
}
#include
#include
#include
class node
{
public:
int rollnumber;
char name[20];
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnode()
{
int rollno;
char nm[20];
cout<<"\nEnter the roll number of the student:"; cin>>rollno;
cout<<"\nEnter the name of the student:"; cin>>nm;
node *newnode=new node;
newnode->rollnumber=rollno;
strcpy(newnode->name,nm);
if(start==NULL || rollno<=start->rollnumber)
/* If the node to be inserted is the first node */
{
if(start!=NULL && (rollno == start->rollnumber))
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } newnode->next=start;
start=newnode;
return;
}
node *previous,*current;
previous=start;
current=start;
while((current!=NULL) && (rollno>=current->rollnumber))
{
if(rollno==current->rollnumber)
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } previous=current; current=current->next;
}
newnode->next=current;
previous->next=newnode;
}
int listempty()
{
if(start==NULL)
return(1);
else
return(0);
}
int delnode(int rollno)
{
node *current,*previous;
if(search(rollno,&previous,¤t)==0)
return 0;
previous->next=current->next;
if(current==start)
start=start->next;
delete current;
return 1;
}
/* int search(int rollno,node **previous,node **current)
{
*previous=start;
*current=start;
while((*current!=NULL)&&((rollno!=current->rollnumber)))
{
*previous=*current;
*current=(*current)->next;
}
if(*current!=NULL)
return 1;
else
return 0;
}
*/ void traverse()
{
if(listempty())
cout<<"\nList is empty"; else { cout<<"The records in the list are:\n"; node *currentnode; for(currentnode=start;currentnode!=NULL;currentnode=currentnode->next)
{
cout<
switch(ch)
{
case '1':
obj.addnode();
break;
case '2':
if(obj.listempty()==0)
{
cout<<"List Is Empty:"; break; } cout<<"\nEnter Rollno. to delete record:"; cin>>rollno;
if(obj.delnode(rollno)==0)
cout<<"Record not found"; else cout<<"Record deleted."; break; case '3': obj.traverse(); break; case '4': if(obj.listempty()==0) { cout<<"\nList Empty"; break; } node *previous,*current; cout<<"\nEnter rollno. to search:"; cin>>rollno;
if(obj.search(rollno,&previous,¤t)==0)
{
cout<<"\n Record not Found"; break; } else { cout<<"\n Record found\n"; cout<<"Roll Number: "<
cout<<"\nName: "<
}
break;
case '5':
exit(0);
default:
cout<<"Enter correct choice:";
break;
}
}
}
Monday, November 8, 2010
Converting Expression from Infix to Postfix using C++
#include<iostream.h>
#include<conio.h>
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{
if(full())
{
cout<<"\nStack overflow:\n";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{
if(empty())
return('#'); // Return value '#' indicates stack is empty
else
return(stack_array[top--]);
}
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
int full()
{
if(top==49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout<<"\nEnter an infix expression:";
cin>>infix;
}
int white_space(char symbol)
{
if(symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l,precedence,p;
char entry1,entry2;
p=0;
for(int i=0;infix[i]!='\0';i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while((entry2=s.pop())!='(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=prec(entry1);
entry2=s.pop();
while(precedence<=prec(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1;
break;
}
}
}
while(!s.empty()) //while stack is not empty
postfix[p++]=s.pop();
postfix[p]='\0';
cout<<"\nThe postfix expression is: "<<postfix<<endl;
}
int prec(char symbol)
{
switch(symbol)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
};
void main()
{
char choice='y';
Expression expr;
while(choice=='y')
{
expr.read();
expr.ConvertToPostfix();
cout<<"\n\nDo you want to continue ? (y/n): ";
cin>>choice;
}
}
#include<conio.h>
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{
if(full())
{
cout<<"\nStack overflow:\n";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{
if(empty())
return('#'); // Return value '#' indicates stack is empty
else
return(stack_array[top--]);
}
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
int full()
{
if(top==49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout<<"\nEnter an infix expression:";
cin>>infix;
}
int white_space(char symbol)
{
if(symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l,precedence,p;
char entry1,entry2;
p=0;
for(int i=0;infix[i]!='\0';i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while((entry2=s.pop())!='(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=prec(entry1);
entry2=s.pop();
while(precedence<=prec(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1;
break;
}
}
}
while(!s.empty()) //while stack is not empty
postfix[p++]=s.pop();
postfix[p]='\0';
cout<<"\nThe postfix expression is: "<<postfix<<endl;
}
int prec(char symbol)
{
switch(symbol)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
};
void main()
{
char choice='y';
Expression expr;
while(choice=='y')
{
expr.read();
expr.ConvertToPostfix();
cout<<"\n\nDo you want to continue ? (y/n): ";
cin>>choice;
}
}
Binary Search using C++
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[20];
int n;
int i;
while(1)
{
cout<<"\nEnter the number of elements in the array:";
cin>>n;
if((n>1)&&(n<=20))
break;
else
cout<<"\nArray should have minimum 1 and maximum 20 elements\n\n";
}
cout<<"Enter elements in ascending order:\n";
for(i=0;i<n;i++)
{
cout<<"<"<< (i+1) <<">";
cin>>arr[i];
}
char ch;
do
{
int item,ctr;
cout<<"\nEnter the element to be searched:";
cin>>item;
int lowerbound=0;
int upperbound=n-1;
int mid=(lowerbound+upperbound)/2;
while((item!=arr[mid])&&(lowerbound<=upperbound))
{
if(item>arr[mid])
lowerbound=mid+1;
else
upperbound=mid-1;
mid=(lowerbound+upperbound)/2;
ctr++;
}
if(item==arr[mid])
cout<<"\n"<<item<<" found in the position "<<(mid+1)<<endl;
else
cout<<"\n"<<item<<" not found in the array"<<endl;
cout<<"\nNumber of comparisions:"<<ctr;
cout<<"\n\nContinue search (y/n):";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
}
#include<conio.h>
void main()
{
int arr[20];
int n;
int i;
while(1)
{
cout<<"\nEnter the number of elements in the array:";
cin>>n;
if((n>1)&&(n<=20))
break;
else
cout<<"\nArray should have minimum 1 and maximum 20 elements\n\n";
}
cout<<"Enter elements in ascending order:\n";
for(i=0;i<n;i++)
{
cout<<"<"<< (i+1) <<">";
cin>>arr[i];
}
char ch;
do
{
int item,ctr;
cout<<"\nEnter the element to be searched:";
cin>>item;
int lowerbound=0;
int upperbound=n-1;
int mid=(lowerbound+upperbound)/2;
while((item!=arr[mid])&&(lowerbound<=upperbound))
{
if(item>arr[mid])
lowerbound=mid+1;
else
upperbound=mid-1;
mid=(lowerbound+upperbound)/2;
ctr++;
}
if(item==arr[mid])
cout<<"\n"<<item<<" found in the position "<<(mid+1)<<endl;
else
cout<<"\n"<<item<<" not found in the array"<<endl;
cout<<"\nNumber of comparisions:"<<ctr;
cout<<"\n\nContinue search (y/n):";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
}
Monday, November 1, 2010
DMBS Queries
1. select * from route_header where origin='madras' and destination='cochin';
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select date_add(doi,interval 2 month) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select period_diff(dot,doi) from ticket_header;
13. SELECT DAY,CASE when (4-weekday(day))>0 then day + interval(4-weekday(day))day else day+interval(11-weekday(day))day end "Next Friday"from fleet_header;
14.
i. select day,case when(month(day)<6) then makedate(year(day),1) else makedate(year(day),12) end "Rounded date" from fleet_header;
ii. select day,case when(day(day))>15 then last_day(day)else date_add(day,interval(1-day(day)) day)end "Round of day"from fleet_header;
iii. select day,case when(weekday(day)>3) then date_sub(day,interval(weekday(day))day) else date_add(day,interval(6-weekday(day))day) end "Round of day" from fleet_header;
iv. select day, case when (year(day)<1900) then date_\c select day, case when(year(day)<=1950)then date_add(day,interval (2000-year(day))day) else date_sub(day,interval(year(day))day) end "round of year" from fleet_header;
15. select date_format(day, '%D %m %y') from fleet_header;
16. select max(period_diff(dot,doi))"Max.Differance" from ticket_header;
17. select date_add(doi,interval 31 day) from ticket_header;
18. select date_sub(day,interval 60 day) from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select ucase(origin)"upper of Orgin",ucase(destination)"upper of Destination" from route_header;
21. select lcase(origin),lcase(destination) from route_header;
22. select substring('bangalore',1,3) from place_header;
23. select substring('hyderabad',-3)from place_header;
24. select replace(place_name,'m','v') from place_header where place_name like 'm%';
25. select substring(name,4,3) from ticket_detail;
26. select replace(place_name,'s','e'),replace(place_name,'a','e') from place_header;
27. select round(sum(fare),2) from route_header ;
28. select round(sum(fare))"Total Fare" from route_header;
29. select date_format(curdate(), '%D %M %Y');
30. select date_format(curdate(),'%d %m %Y');
31.
32. select avg(fare)"Average Fare" from route_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by ticket_no;
37. update fleet_header set day=date_add(day,interval 3 day) where weekday(day)=6;
Procedure
mysql> delimiter //
mysql> create procedure k1()
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k1();
mysql> create procedure k2(k int)
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k2(5);
Trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
-> FOR EACH ROW SET @sum = @sum + NEW.amount;
mysql> SET @sum = 0;
mysql> INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
mysql> SELECT @sum AS 'Total amount inserted';
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48 |
+-----------------------+
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN -> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter ;
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select date_add(doi,interval 2 month) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select period_diff(dot,doi) from ticket_header;
13. SELECT DAY,CASE when (4-weekday(day))>0 then day + interval(4-weekday(day))day else day+interval(11-weekday(day))day end "Next Friday"from fleet_header;
14.
i. select day,case when(month(day)<6) then makedate(year(day),1) else makedate(year(day),12) end "Rounded date" from fleet_header;
ii. select day,case when(day(day))>15 then last_day(day)else date_add(day,interval(1-day(day)) day)end "Round of day"from fleet_header;
iii. select day,case when(weekday(day)>3) then date_sub(day,interval(weekday(day))day) else date_add(day,interval(6-weekday(day))day) end "Round of day" from fleet_header;
iv. select day, case when (year(day)<1900) then date_\c select day, case when(year(day)<=1950)then date_add(day,interval (2000-year(day))day) else date_sub(day,interval(year(day))day) end "round of year" from fleet_header;
15. select date_format(day, '%D %m %y') from fleet_header;
16. select max(period_diff(dot,doi))"Max.Differance" from ticket_header;
17. select date_add(doi,interval 31 day) from ticket_header;
18. select date_sub(day,interval 60 day) from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select ucase(origin)"upper of Orgin",ucase(destination)"upper of Destination" from route_header;
21. select lcase(origin),lcase(destination) from route_header;
22. select substring('bangalore',1,3) from place_header;
23. select substring('hyderabad',-3)from place_header;
24. select replace(place_name,'m','v') from place_header where place_name like 'm%';
25. select substring(name,4,3) from ticket_detail;
26. select replace(place_name,'s','e'),replace(place_name,'a','e') from place_header;
27. select round(sum(fare),2) from route_header ;
28. select round(sum(fare))"Total Fare" from route_header;
29. select date_format(curdate(), '%D %M %Y');
30. select date_format(curdate(),'%d %m %Y');
31.
32. select avg(fare)"Average Fare" from route_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by ticket_no;
37. update fleet_header set day=date_add(day,interval 3 day) where weekday(day)=6;
Procedure
mysql> delimiter //
mysql> create procedure k1()
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k1();
mysql> create procedure k2(k int)
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k2(5);
Trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
-> FOR EACH ROW SET @sum = @sum + NEW.amount;
mysql> SET @sum = 0;
mysql> INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
mysql> SELECT @sum AS 'Total amount inserted';
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48 |
+-----------------------+
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN -> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter ;
What's New in Visual C++ 6.0
What's New in Visual C++ 6.0
Visual C++ offers many new features and improvements over its predecessor, Visual C++ 5.0. The new features covered in this book follow:- The compiler offers improved support for the ANSI C++ standard. Boolean types are supported, and template support is improved.
- The development system includes new enhancements to MFC, the Microsoft Foundation Class Library. These enhancements include classes for Internet programming and support for new common controls introduced in Internet Explorer 4.0 and Windows 98.
- The Developer Studio Editor is much improved, and it takes advantage of IntelliSense features originally released as part of Visual Basic. These features include statement completion, which greatly improves your efficiency.
- The debugger included with Visual C++ includes a new feature called Debug and Continue, which enables you to make small changes while debugging and then immediately continue debugging without restarting the application.
- An improved online help system puts the MSDN in easy reach, just a mouse click away. The online help system automatically uses the latest version of the MSDN Library if it's installed on your computer.
MySQL Cursors
Cursors are supported inside stored procedures and functions and triggers. The syntax is as in embedded SQL. Cursors in MySQL have these properties:
* Asensitive: The server may or may not make a copy of its result table
* Read only: Not updatable
* Nonscrollable: Can be traversed only in one direction and cannot skip rows
Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers.
DECLARE cursor_name CURSOR FOR select_statement
This statement declares a cursor. Multiple cursors may be declared in a stored program, but each cursor in a given block must have a unique name.
The SELECT statement cannot have an INTO clause.
OPEN cursor_name
This statement opens a previously declared cursor.
FETCH cursor_name INTO var_name [, var_name] ...
This statement fetches the next row (if a row exists) using the specified open cursor, and advances the cursor pointer.
If no more rows are available, a No Data condition occurs with SQLSTATE value 02000. To detect this condition, you can set up a handler for it (or for a NOT FOUND condition).
CLOSE cursor_name
This statement closes a previously opened cursor.
If not closed explicitly, a cursor is closed at the end of the compound statement in which it was declared.
Example:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
* Asensitive: The server may or may not make a copy of its result table
* Read only: Not updatable
* Nonscrollable: Can be traversed only in one direction and cannot skip rows
Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers.
DECLARE cursor_name CURSOR FOR select_statement
This statement declares a cursor. Multiple cursors may be declared in a stored program, but each cursor in a given block must have a unique name.
The SELECT statement cannot have an INTO clause.
OPEN cursor_name
This statement opens a previously declared cursor.
FETCH cursor_name INTO var_name [, var_name] ...
This statement fetches the next row (if a row exists) using the specified open cursor, and advances the cursor pointer.
If no more rows are available, a No Data condition occurs with SQLSTATE value 02000. To detect this condition, you can set up a handler for it (or for a NOT FOUND condition).
CLOSE cursor_name
This statement closes a previously opened cursor.
If not closed explicitly, a cursor is closed at the end of the compound statement in which it was declared.
Example:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
Subscribe to:
Posts (Atom)