You are on page 1of 30

1) Write a C++ program to create a structure called Employee with three data members,

Emp_nuber, Emp_Name , Basic salary, Allowances and net salary. Write member functions for reading
the details of an employee, Computing the net salary and for printing the details of employee.
#include<iostream.h>
#include<conio.h>
typedef struct
{
int cno;
char na[20];
float bs,net,da,gross,it;
}EMP;
EMP e[50];
void read(int n)
{ int i;
for(i=0;i<n;i++)
{
cout<<" Emp "<<(i+1)<<endl;

cout<<"Enter Cno,Name,Basic salary\n";


cin>>e[i].cno>>e[i].na>>e[i].bs;
}
}
void print(int n)
{ int i;
cout<<"\n Details \n";
cout<<"cus nu\t name\t basic_sal\tnet_sal\n";
for(i=0;i<n;i++)

cout<<e[i].cno<<"\t"<<e[i].na<<"\t"<<e[i].bs<<"\t"<<e[i].net<<endl;
}
void netsal(int n)
{ int i;
for(i=0;i<n;i++)
{
e[i].da=(52*e[i].bs)/100.0;
e[i].gross=e[i].bs+e[i].da;
e[i].it=(30*e[i].gross)/100.0;
e[i].net=e[i].gross+e[i].da-e[i].it;

}
}
void main()
{
int n,i;
clrscr();
cout<<"\nEnter the number of employee\n";
cin>>n;
cout<<"Enter the details of "<<n<<" emp\n";
read(n);
netsal(n);
print(n);
getch();
}
2) Write a C program to create an array of structure where each structure holds the following details of a
student
USN Name Marks1 Marks2 Marks3
Non-zero 25 Characters Positive Positive Positive
Positive integer integer integer
integer
Write necessary functions1) Input details for n students.(n is the parameter to be read) 2) To display
all the records created 3) To search for specific based on the USN. In case the required record is not
found, suitable message should be displayed. Both the options in this case must be demonstrated.

#include<stdio.h>
#include<conio.h>
typedef struct
{
int usn,m1,m2,m3;
char na[20];
}STU;
STU s[50];
void input(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter USN,Name,M1,M2,M3\n");
scanf("%d%s%d%d%d",&s[i].usn,s[i].na,&s[i].m1,&s[i].m2,&s[i].m3);
}
}
void disp(int n)
{
int i;
printf("Information of students\n");
for(i=0;i<n;i++)
{
printf("%d Student\n",i+1);
printf("%4d %-10s %4d %4d %4d\n",s[i].usn,s[i].na,s[i].m1,s[i].m2,s[i].m3);
}
}
void srch(int n,int u)

{
int i;
for(i=0;i<n;i++)
{
if(u==s[i].usn)
{
printf("Record found\n");
printf("Name=%s , M1=%d , M2=%d , M3=%d\n",s[i].na,s[i].m1,s[i].m2,s[i].m3);
return;
}
}
printf("Record not found\n");
}
void main()
{
int n,u;
clrscr();
printf("Enter no of students\n");
scanf("%d",&n);
input(n);
disp(n);
printf("Enter usn to be searched\n");
scanf("%d",&u);
srch(n,u);
getch();
}

3. Write a C program to construct a stack of integers and to perform the following operations on it.
i. Push
ii. Pop
iii. Display
The program should print appropriate messages for stack overflow, stack underflow and stack
empty.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define TRUE 1
#define FALSE 0
#define MAX 2
typedef struct
{
int item[MAX];
int top;
}STACK;
int isempty(STACK *s)
{
if(s->top==-1)
return TRUE;
else
return FALSE;
}
int isfull(STACK *s)
{ if(s->top==MAX-1)
return TRUE;
else
return FALSE;
}
void push(STACK *s,int x)
{
if(isfull(s))
{
cout<<"Overflow"<<endl;
return;
}
s->top++;
s->item[s->top]=x;
cout<<x<<"is inserted"<<endl;
}
void pop(STACK *s)
{
int x;
if(isempty(s))
{
cout<<"Underflow"<<endl;
return;
}
x=s->item[s->top];
s->top--;
cout<<x<<" is popped\n"<<endl;
}
void display(STACK *s)
{
int x;
if(isempty(s))
{
cout<<"Underflow"<<endl;
return;
}
for(x=s->top;x>=0;x--)
cout<<s->item[x]<<"\t"<<endl;
}
void main()
{
STACK stk;
int op,x;
clrscr();
stk.top=-1;
do
{
cout<<"1.PUSH 2.POP 3.DISPLAY 4.EXIT\n";
cout<<"Enter the option\n";
cin>>op;
switch(op)
{
case 1:cout<<"Enter value";
cin>>x;
push(&stk,x);
break;
case 2:pop(&stk);
break;
case 3:display(&stk);
break;
case 4:exit(0);

default:cout<<"Inavalid option\n";
}
}
while(op!=4);
getch();
}

4) Write a C program to convert and print a given valid parenthesized infix arithmetic expression to postfix
expression. The expression consists of single character operands and the binary operators + (plus), -
(minus), * (multiply) and / (divide).

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define MAX 50
typedef struct
{
char item[MAX];
int top;
}STACK;
int is_empty(STACK *s)
{
if(s->top==-1)
return(TRUE);
else
return(FALSE);
}
int is_full(STACK *s)
{
if(s->top==MAX-1)
return(TRUE);
else
return(FALSE);
}
void push(STACK *s,int x)
{
if(is_full(s))
{
printf("overflow\n");
return;
}
s->top++;
s->item[s->top]=x;
}

char pop(STACK *s)


{
char x;
if(is_empty(s))
{
printf("underflow\n");
return 0;
}
x=s->item[s->top];
s->top--;
}
int pval(char op)
{
switch(op)
{
case '$':return(5);
case '*':return(4);
case '/':return(4);
case '+':return(3);
case '-':return(3);
case '(':return(2);
}
}
int isopnd(char c)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return(TRUE);
else
return(FALSE);
}
void postfix(char infix[],char postr[])
{
STACK opstk;
char symb,x;
int i,outpos=0;
opstk.top=-1;
for(i=0;infix[i]!='\0';i++)
{
symb=infix[i];
switch(symb)
{
case '(':push((&opstk),symb);
break;
case ')':while(opstk.item[opstk.top]!='(')
{
x=pop(&opstk);
postr[outpos++]=x;
}
x=pop(&opstk);
break;
default:if(isopnd(symb))
postr[outpos++]=symb;
else
{
if(is_empty(&opstk))
push(&opstk,symb);
else
{

while((pval(opstk.item[opstk.top])>=pval(symb)) &&(!is_empty(&opstk)))
{
x=pop(&opstk);
postr[outpos++]=x;
}
push(&opstk,symb);
}
}
}
}
while(!is_empty(&opstk))
{
x=pop(&opstk);
postr[outpos++]=x;
}
postr[outpos]='\0';
}
void main()
{
char infix[50];
char postr[50];
clrscr();
printf("enter valid infix expression\n");
gets(infix);
postfix(infix,postr);
printf("infix:\n");
puts(infix);
printf("\n postfix:\n");
puts(postr);
getch();
}
/* Output
Enter valid infix expression
(a+b)*(c+d)
Infix:
(a+b)*(c+d)
Postfix:
ab+cd+*

5). Write a C program to evaluate a valid suffix/postfix expression-using stack. Assume that the suffix /
postfix expression is read as a single line consisting of non-negative single digit operands and binary
arithmetic operators. The arithmetic operators are + (ADD), - (SUBTRACT), *(MULTIPLY) and /
(DIVIDE).

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h> /* isdigit() function is defined in this library */
#define TRUE 1
#define FALSE 0
#define MAX 50
typedef struct
{
double item[MAX];
int top;
}STACK;
int is_empty(STACK *s)
{
if(s->top==-1)
return(TRUE);
else
return(FALSE);
}
int is_full(STACK *s)
{
if(s->top==MAX-1)
return(TRUE);
else
return(FALSE);
}
void push(STACK *s,double x)
{
if(is_full(s))
{
printf("overflow\n");
return;
}
s->top++;
s->item[s->top]=x;
}
double pop(STACK *s)
{
double x;
if(is_empty(s))
{
printf("underflow\n");
exit(1);
}
x=s->item[s->top];
s->top--;
return(x);
}
double evaluate(char op,double op1,double op2)
{
switch(op)
{
case '+':return(op1+op2);
case '-':return(op1-op2);
case '*':return(op1*op2);
case '/':return(op1/op2);
case '$':return(pow(op1,op2));
default:printf("invalid operator\n");
return 0;
}
}
void main()
{
char expr[50],symb;
STACK opndstk;
int i;
double op1,op2,res;
clrscr();
opndstk.top=-1;
printf("enter a valid postfix expression\n");
gets(expr);
for(i=0;expr[i]!='\0';i++)
{
symb=expr[i];
if(isdigit(symb))
push(&opndstk,(double) symb-'0');
else
{
op2=pop(&opndstk);
op1=pop(&opndstk);
res=evaluate(symb,op1,op2);
push(&opndstk,res);
}
}
printf("given expression %s\n",expr);
printf("result=%lf\n",res);
getch();
}

/* Output
enter a valid postfix expression
72+56+*
given expression 72+56*
result=99.000000

*/
6) Using constructor to initialize pointers (rear and front), demonstrate the operations on a Linear Queue,
writing a C++ program.

#include<iostream.h>
#include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 4
class cq
{
private:
typedef struct
{
int front,rear,item[MAX];
}CQUEUE;
CQUEUE q;
public:
cq()
{
q.front=0;
q.rear=-1;
}
int qempty()
{
return((q.rear<q.front)?TRUE:FALSE);
}
int qfull()
{
return((q.rear==MAX-1)?TRUE:FALSE);
}
void qinsert(int x)
{
if(qfull())
{
cout<<"Overflow\n";
return;
}
q.rear=((q.rear)+1);
q.item[q.rear]=x;
cout<<"Inserted element is "<<x<<endl;
}
void qdelete()
{
int x;
if(qempty() )
{
cout<<"Undeflow\n";
return;
}
//q.front=((q.front)+1)%MAX;
x=q.item[q.front];
q.front++;
cout<<x<<" is deleted\n";
}
void qdisp()

{ int x;
if(qempty())
{
cout<<"Underflow\n";
return;
}
for(int i=q.front;i<=q.rear;i++)
{
/*x=q.front;
do
{
x=(x+1)%MAX; */
cout<<q.item[i]<<endl;
}
//}while(x!=q.rear);
}
};
void main()
{
cq c;
int ch,x;
clrscr();
do
{
cout<<"choose\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the value to be inserted\n";
cin>>x;
c.qinsert(x);
break;
case 2:c.qdelete();
break;
case 3:c.qdisp();
break;
case 4:cout<<"EXIT";
break;
default:cout<<"Invalid entry\n";
}
}while(ch!=4);
getch();
}
7) Using constructor to initialize pointers (rear and front), demonstrate the operations on a Circular Queue,
writing a C++ program.

#include<iostream.h>
#include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 4
class cq
{
private:
typedef struct
{
int front,rear,item[MAX];
}CQUEUE;
CQUEUE q;
public:
cq()
{
q.front=q.rear=MAX-1;
}
int qempty()
{
return((q.rear==q.front)?TRUE:FALSE);
}
int qfull()
{

return((((q.rear+1)%MAX)==q.front)?TRUE:FALSE);
}
void qinsert(int x)
{
if(qfull())
{
cout<<"Overflow\n";
return;
}
q.rear=((q.rear)+1)%MAX;
q.item[q.rear]=x;
cout<<"Inserted element is "<<x<<endl;
}
void qdelete()
{
int x;
if(qempty() )
{
cout<<"Undeflow\n";
return;
}
q.front=((q.front)+1)%MAX;
x=q.item[q.front];
cout<<x<<" is deleted\n";
}
void qdisp()

{ int x;
if(qempty())
{
cout<<"Underflow\n";
return;
}
x=q.front;
do
{
x=(x+1)%MAX;
cout<<q.item[x]<<endl;
}while(x!=q.rear);
}
};
void main()
{
cq c;
int ch,x;
clrscr();
do
{
cout<<"choose\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the value to be inserted\n";
cin>>x;
c.qinsert(x);
break;
case 2:c.qdelete();
break;
case 3:c.qdisp();
break;
case 4:cout<<"EXIT";
break;
default:cout<<"Invalid entry\n";
}
}while(ch!=4);
getch();

8) Write a C/C++ program to implement a priority queue to add and delete the
elements.
#include<iostream.h>
#include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 4

class pq
{
public:

int front,rear,item[MAX];

pq()
{

front=rear=MAX-1;

}
int qempty(pq *q)
{
return((q->rear==q->front)?TRUE:FALSE);
}
int qfull(pq *q)
{
return((((q->rear+1)%MAX)==q->front)?TRUE:FALSE);
}
void qinsert(pq *q,int x)
{

q->rear=((q->rear)+1)%MAX;
q->item[q->rear]=x;
cout<<"Inserted element is "<<x<<endl;
}
int qdelete(pq *q)
{
int x;

q->front=((q->front)+1)%MAX;
x=q->item[q->front];
return(x);
}
void qdisplay(pq *q)

{ int x;

x=q->front;

while(x!=q->rear)
{
x=(x+1)%MAX;
cout<<q->item[x]<<endl;
}
}
};
void main()
{
pq q1,q2,q3;
int ch,x,n,ele;
clrscr();
do
{
cout<<"\nchoose\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the priority of the queue\n";
cin>>n;
cout<<"enter the element to insert\n";
cin>>x;
switch(n)
{
case 1: if(!q1.qfull(&q1))
q1.qinsert(&q1,x);
else
cout<<"queue 1 is full\n";
break;
case 2: if(!q2.qfull(&q2))
q2.qinsert(&q2,x);
else
cout<<"queue 2 is full\n";
break;
case 3: if(!q3.qfull(&q3))
q3.qinsert(&q3,x);
else
cout<<"queue 3 is full\n";
break;
}
break;
case 2:if(!q1.qempty(&q1))
{
ele=q1.qdelete(&q1);
cout<<ele<<" is removed from queue 1";
}
else
{
cout<<"queue1 is empty\n";
if(!q2.qempty(&q2))
{
ele=q2.qdelete(&q2);
cout<<ele<<" is removed from queue 2";
}
else
{
cout<<"queue 2 is empty\n";
if(!q3.qempty(&q3))
{
ele=q3.qdelete(&q3);
cout<<ele<<" is removed from queue 3";
}
else
cout<<"queue 3 is empty\n";
}
}

break;
case 3: if(!q1.qempty(&q1))
{
cout<<" Q1 contents are \n";
q1.qdisplay(&q1);
}
else
cout<<"Q1 is empty \n";
if(!q2.qempty(&q2))
{
cout<<" Q2 contents are \n";
q2.qdisplay(&q2);
}
else
cout<<"Q2 is empty \n";

if(!q3.qempty(&q3))
{
cout<<" Q3 contents are \n";
q3.qdisplay(&q3);
}
else
cout<<"Q3 is empty \n";
break;
case 4:cout<<"EXIT";
break;
default:cout<<"Invalid entry\n";
}
}while(ch!=4);
getch();

}
9) Write a C++ program to create a singly linked list and perform insertion, deletion and display operations.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NODE
{
int info;
struct NODE *next;
};
class STACK
{
private:struct NODE *top;

public:
STACK()
{
top=NULL;
}
struct NODE * getnode(struct NODE *p)
{
p=(struct NODE *)malloc(sizeof(NODE));
return(p);
}
void freenode(struct NODE *p)
{
free(p);
}
void push (int x)
{
struct NODE *n;
n=getnode(n);
n->info=x;
n->next=top;
top=n;
cout<<x<<" pushed\n";
}
void pop ()
{
int x;
struct NODE *p;
p=top;
x=p->info;
top=p->next;
freenode(p);
cout<<x<<" popped\n";
}
void disp()
{
struct NODE *p;
if(top==NULL)
{
cout<<"Underflow\n";
return;
}
for(p=top;p!=NULL;p=p->next)
cout<<(p->info)<<endl;
}
};
void main()
{
class STACK s;
int opt,x;
clrscr();
do
{
cout<<"1.Push\n2.Pop\n3.Display\n4.Exit\n";
cin>>opt;

switch(opt)
{
case 1 :cout<<"Enter element to push\n";
cin>>x;
s.push(x);
break;
case 2 :s.pop();
break;
case 3 :s.disp();
break;
case 4 :cout<<"Exit?";
break;
default:cout<<"Invalid entry\n";
}
}while(opt!=4);
getch();
}
10) Write a C++ program to create and display an ordered linked list of integers #include<stdlib.h>
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
class list
{
node *head;
public:
list()
{
head=NULL;
}
void remove()
{
node *temp=head;
if(temp==NULL)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"item deleted is\n"<<temp->info<<endl;
head=head->next;
delete temp;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->info<<"-->";
temp=temp->next;
}
cout<<"NULL\n";
}
void place()
{
int x;
node *newnode=new node;
node *p,*q;
cout<<"enter a new no."<<endl;
cin>>x;
newnode->info=x;
q=NULL;
for(p=head;p!=NULL&&p->info<x;p=p->next)
q=p;
if(q==NULL)
{
newnode->next=head;
head=newnode;
}
else
{
newnode->next=p;
q->next=newnode;
}
}
};
void main()
{
int ch;
list link;
clrscr();
do
{
cout<<"\nMenu\n1:insert at front 2:delete at front\n3:display 4:exit\n";
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:link.place();
break;
case 2:link.remove();
break;
case 3:link.display();
break;
case 4:exit(0);
}
}while(ch!=4);
getch();
}
11) Write a C++ program to create a doubly linked list and perform insertion, deletion and display
operations.

#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *lptr;
node *rptr;
};
class list
{
node *head;
public:
list()
{
head=NULL;
}
void insert()
{
int val;
cout<<"enter the element"<<endl;
cin>>val;
node *newnode=new node;
newnode->info=val;
newnode->lptr=NULL;
newnode->rptr=head;
if(head!=NULL)
head->lptr=newnode;
head=newnode;
cout<<val<<"inserted\n";
}
void remove()
{
node *temp=head;
if(temp==NULL)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"item deleted is\n"<<temp->info<<endl;
head=head->rptr;
head->lptr=NULL;
delete temp;
}
}
void display()
{
node *temp=head;
cout<<"NULL<==>";
while(temp!=NULL)
{
cout<<temp->info<<"<==>";
temp=temp->rptr;
}
cout<<"NULL\n";
}
};
void main()
{
int ch;
list link;
clrscr();
do
{
cout<<"\nMenu\n1:insert at front 2:delete at front\n3:display 4:exit\n";
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:link.insert();
break;
case 2:link.remove();
break;
case 3:link.display();
break;
case 4:exit(0);
}
}while(ch!=4);
getch();
}

12) Write a C++ program to create a binary search tree and traverse the same in the three basic traversal
methods.

#include <iostream.h>
#include <stdlib.h>

class node
{
public: int info;
node *left;
node *right;
};

typedef node *NODE;

class BIN_TREE
{
public: node *root;

public: BIN_TREE()
{
root=NULL;
}
//function to get inorder expression
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->info<<"\t";
inorder(root->right);
}
}
//function to get preorder expression
void preorder(node *root)
{
if(root!=NULL)
{
cout<<root->info<<"\t";
preorder(root->left);
preorder(root->right);
}
}
//function to get postorder expression
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->info<<"\t";
}
}
//function to insert elements
NODE insert(int item,node *root)
{
node *temp,*prev,*cur;
temp=new node;
temp->info=item;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item==cur->info)
{
cout<<"Duplicate elements are
not allowed"<<endl;
delete(temp);
return root;
}
cur=(item<cur->info)?cur->left:cur->right;
}
if(item<prev->info) prev->left=temp;
else prev->right=temp;
return root;
}
};

//main function
void main()
{
BIN_TREE bin;
int choice,item;
for(;;)
{
cout<<"1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit"<<endl;
cout<<"Enter choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter item:";
cin>>item;
bin.root=bin.insert(item,bin.root);
break;

case 2: if(bin.root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Inorder Traveresal:"<<endl;
bin.inorder(bin.root);
cout<<endl;
}
break;

case 3: if(bin.root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Preorder Traveresal:"<<endl;
bin.preorder(bin.root);
cout<<endl;
}
break;
case 4: if(bin.root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Postorder Traveresal:"<<endl;
bin.postorder(bin.root);
cout<<endl;
}
break;

default: exit(0);
}
}
}

/*OUTPUT:
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:50
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:25
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:23
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:34
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:60
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:56
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:1
Enter item:70
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:2
Inorder Traveresal:
23 25 34 50 56 60 70
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:3
Preorder Traveresal:
50 25 23 34 60 56 70
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:4
Postorder Traveresal:
23 34 25 56 70 60 50
1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit
Enter choice:5
Press any key to continue
*/

13. Write recursive C programs for the following:


 Searching an element on a given list of integers using the Binary search method.

#include <stdio.h>
#include <stdlib.h>
int bin_srch(int a[], int n, int key, int low, int high)
{
int mid;
if(low <= high)
{
mid = (low + high)/2;
if(a[mid] == key)
{
return mid;
}
else if(key < a[mid])
{
return bin_srch(a, n, key, low, mid-1);
}
else if(key > a[mid])
{
return bin_srch(a, n, key, mid+1, high);
}
}
else
{
return -1;
}
}
int main()
{
int a[10], n, key, low, mid, high, i, indx;
clrscr();
printf("\nEnter size of array: ");
scanf("%d", &n);
printf("\nEnter elements of array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the key element to be searched: ");
scanf("%d", &key);
low = 0;
high = n-1;
indx = bin_srch(a, n, key, low, high);
if(indx == -1)
{
printf("\nSearch unsuccessful");
}
else
{
printf("\nElement found at position %d\n\n", indx+1);
}
getch();
}

/*output

Enter size of array: 5

Enter elements of array: 10


20
30
40
50

Enter the key element to be searched: 60

Search unsuccessful
----------------------
Enter size of array: 5

Enter elements of array: 2


4
6
8
10
Enter the key element to be searched: 8
Element found at position 4
------------------------------------

Enter size of array: 5


Enter elements of array: 1
2
3
4
5
Enter the key element to be searched: 6
Search unsuccessful

*/

Solving the Towers of Hanoi problem.

#include<stdio.h>
#include<conio.h>
int cnt=0;
void towers(int n,char from,char to,char aux)
{
if(n==1)
{
printf("move disc 1 from %c to %c\n",from,to);
cnt++;
return;
}
towers(n-1,from,aux,to);
printf("move disc %d from %c to %c\n",n,from,to);
cnt++;
towers(n-1,aux,to,from);
}

void main()
{
int n;
clrscr();
printf("enter n\n");
scanf("%d",&n);
towers(n,'A','C','B');
printf("the no of moves made= %d\n",cnt);
getch();
}

/*
enter n
4
move disc 1 from A to B
move disc 2 from A to C
move disc 1 from B to C
move disc 3 from A to B
move disc 1 from C to A
move disc 2 from C to B
move disc 1 from A to B
move disc 4 from A to C
move disc 1 from B to C
move disc 2 from B to A
move disc 1 from C to A
move disc 3 from B to C
move disc 1 from A to B
move disc 2 from A to C
move disc 1 from B to C
the no of moves made= 15
*/

You might also like