Вы находитесь на странице: 1из 30

1

/*WRITE A C PROGRAM TO SEARCH FOR AN ELEMENT IN AN ARRAY


USING BINARY SEARCH */

#include<stdio.h>
#include<conio.h>
void bin_search(int item,int a[],int n,int *pos)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
*pos=mid;
return;
}
if(item<a[mid])
{
high=mid-1;
}
else
low=mid+1;
}
*pos=-1;
}

void main()
{
int n,a[20],i,item,position;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("enter the item to be searched\n");
scanf("%d",&item);
bin_search(item,a,n,&position);
if(position==-1)
{
printf("not found\n");
}
else
{
printf("item found at %dth the position",position);
}
getch();
}
/*OUTPUT

enter the value of n


5
enter the array element
2
3
4
5
6
7
enter the item to be searched
5
item found at 3th the position */
2
/*WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS USING BUBBLE
SORT */

#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,temp,n;
clrscr();
printf("enter the size\n");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
/*OUT PUT

enter the size


5
enter the array element
9
4
5
3
8
sorted array is
3
4
5
8
9 */
3
/*WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS
USING MERGE SORT */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void marge_sort(int a[],int low,int high);
void simple_marge(int a[],int low,int mid,int high);

void main()
{
int i,n,a[20];
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
marge_sort(a,0,n-1);
printf("sorted array as fallow\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

void marge_sort(int a[],int low,int high)


{
int mid;
if(low<high)
{
mid=(low+high)/2;
marge_sort(a,low,mid);
marge_sort(a,mid+1,high);
simple_marge(a,low,mid,high);
}
}
void simple_marge(int a[],int low,int mid,int high)
{
int c[20],i=low,j=mid+1,k=low;
while(i<=mid && j<=high)
{
if(a[i]<=a[j])
{
c[k]=a[i];
i++;k++;
}
else
{
c[k]=a[j];
j++;k++;
}
}
while(i<=mid)
{
c[k]=a[i];
i++;k++;
}
while(j<=high)
{
c[k]=a[j];
j++;k++;
}
for(i=low;i<=high;i++)
a[i]=c[i];
}
/*OUT PUT
enter the limit
7
enter the array element
20
40
5
50
60
2
8
sorted array as fallow

2
5
8
20
40
50
60 */
4
/*WRITE A C PROGRAM TO FIND THE BINOMIAL CO-EFFICENT
USING RECURSION */

#include<stdio.h>
#include<conio.h>
int bio_cof(int k,int m)
{
if(m==0||m==k)
return(1);
return bio_cof(k-1,m-1)+bio_cof(k-1,m);
}

void main()
{
int k,m,ncr;
clrscr();
printf("enter the value\n");
scanf("%d%d",&k,&m);
ncr=bio_cof(k,m);
printf("bio_cof(%d,%d)=%d\n",k,m,ncr);
getch();
}

/*OUT PUT
enter the value
3
2
bio_cof(3,2)=3 */
5
/*WRITE A C PROGRAM THE TOWER HONNAI OF PROBLEM
USING RECURSION */

#include<stdio.h>
#include<conio.h>
void tower(int n,int sourse,int temp,int dest)
{
if(n==1)
{
printf("\n move disc %d form %c to %c\n",n,sourse,dest);
return;
}

tower(n-1,sourse,dest,temp);
printf("\n move disc %d form %c to %c\n",n,sourse,dest);
tower(n-1,temp,sourse,dest);
}

void main()
{
int n;
clrscr();
printf("enter the value of n\n\n");
scanf("%d",&n);
tower(n,'A','B','C');
getch();
}
/*OUT PUT

enter the value of n


3

move disc 1 form A to C

move disc 2 form A to B

move disc 1 form C to B

move disc 3 form A to C

move disc 1 form B to A

move disc 2 form B to C

move disc 1 form A to C */


6
/*WRITE A C PROGRAM TO SORT TA LIST OF N ELEMENT
USING QUICK SORTS */

#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int low,int high);
int partition(int a[],int low,int high);

main()
{
int i,n,a[70];
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

void quicksort(int a[],int low,int high)

{
int keypos;
if(low<high)
{
keypos=partition(a,low,high);
quicksort(a,low,keypos-1);
quicksort(a,keypos+1,high);
}
}
int partition(int a[],int low,int high)
{
int i,j,temp,key;
key=a[low],i=low+1,j=high;
while(1)
{
while(key>=a[i])
i++;
while(key<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}
}
}
/*OUT PUT

enter the limit


5
enter the array element
6
8
9
-8
0
sorted array is

-8
0
6
8
9

7
/*

WRITE A C PROGRAM TO SEARCH AN ELEMENT USING


SEQUENTIAL SEARCH*/

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[10],item;
clrscr();
printf("enter the value\n ");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the item\n");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
printf("element %d found",item);
exit(0);
}
}
printf("element %d notfound",item);
getch();
}
/*OUT PUT
enter the value
5
enter the array element
70
20
30
5
10
enter the item
10
element 10 found */
8/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN
STACK USING ARRAY */

#include<stdio.h>
#include<process.h>
#include<conio.h>
#define stacksize 5
int p;
int semp(int top)
{
return(top==-1)?1:0;
}
int sfull(int top)
{
return(top==stacksize-1)?1:0;
}
void push(int s[],int *top,int item)
{
if(sfull(*top))
{
printf("stack full\n");
return;
}
s[++(*top)]=item;
}

int pop(int s[],int *top)


{
int itemdel;
if(semp(*top))
{
printf("under flow\n");
return 0;
}
return s[(*top)--];
}
void display(int s[25],int top)
{
int i;
if(semp(top))
{
printf("stack empty");
}
else
{
printf("element on stack are\n");
for(i=0;i<=top;++i)
{
printf("\t%d",s[i]);
}
}
}
void main()
{
int s[25],top=-1,k=0 ,item;
int choice; f
clrscr();
printf("program to perform stack operations\n");
do
{
printf("1:push\n 2:pop\n 3:display\n 4:exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("enter the item\n");
scanf("%d",&item);
push(s,&top,item);
break;
case 2: k=pop(s,&top);
if(k!=0)
printf("%d\n",k);
break;
case 3: display(s,top);
break;
case 4: exit(0);
break;
default:printf("\nwrong choice\n");
break;
}
printf("\ndo you want to continue (1/0?)");
scanf("%d",&p);
}
while(p);

getch();
}

/*OUT PUT
***program to perform stack operations***
1:push
2:pop
3:display
4:exit
enter the choice
3
***stack empty***

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
1
enter the item
10

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
1
enter the item
20

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
1
enter the item
30

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
3
element on stack are
10 20 30

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
2
30

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
2
20

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
2
10

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
2
***under flow***

do you want to continue (1/0?)1


1:push
2:pop
3:display
4:exit
enter the choice
4 */
9/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN
ODINARY QUEUE USING ARRAY */
#include<stdio.h>
#include<process.h>
#include<conio.h>
#define queuesize 5
int f,r,i,item,q[10];
void insertrear()
{
if(r==queuesize-1)
{
printf("**queue over flow**\n");
return;
}
else
{
printf(" enter the item to be inserted\n");
scanf("%d",&item);
r=r+1;
q[r]=item;
}
return;
}

void delete()
{
if(f>r)
{
printf("**under flow**\n");
return;
}
else
{
item=q[f];
printf(" the element deleted is %d\n",item);
f++;
}
}

void display()
{
if(f>r)
{
printf("***queue is empty***\n");
return;
}
printf("content of the queue\n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
return;
}

void main()
{
int i,ch,item;
f=0,r=-1;
clrscr();
printf("***program to perform circular queue
operations***\n");
for(;;)
{
printf(" 1:insert\n 2:delete\n 3: display\n 4:exit\n");
printf("enter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insertrear();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
}
}
getch();
}
/*OUTPUT
***program to perform stack operations***
1:insert
2:delete
3: display
4:exit
enter the choice
3
***queue is empty***
1:insert
2:delete
3: display
4:exit
enter the choice
1
enter the item to be inserted
10
1:insert
2:delete
3: display
4:exit
enter the choice
1
enter the item to be inserted
20
1:insert
2:delete
3: display
4:exit
enter the choice
1
enter the item to be inserted
30
1:insert
2:delete
3: display
4:exit
enter the choice
3
content of the queue
10
20
30
1:insert
2:delete
3: display
4:exit
enter the choice
2
the element deleted is 10
1:insert
2:delete
3: display
4:exit
enter the choice
2
the element deleted is 20
1:insert
2:delete
3: display
4:exit
enter the choice
2
the element deleted is 30
1:insert
2:delete
3: display
4:exit
enter the choice
2
**under flow**
1:insert
2:delete
3: display
4:exit
enter the choice
4 */
10/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN
CIRCULAR QUEUE USING ARRAY */

#include<stdio.h>
#include<string.h>
#include<process.h>
#define qsize 9
int i,f=0,r=-1,count=0;
int ch,q[10],item,pos;
void insertrear()
{
if(count==qsize)
{
printf("queue is full\n");
return;
}
r=(r+1)%qsize;
q[r]=item;
count++;
}
void display()
{
int i;
if(count==0)
{
printf("\t**queue is empty**\n\n");
return;
}
printf("\t**queue contain**\n");
for(i=f;i<=r;i++)
{
printf("\t**item %d is in the position %d**\n",q[i],i);
}
}
void deletefront()
{
int pos;
if(count==0)
{
printf("\t**queue if underflow**\n");
return;
}
printf("\t**deleted item %d from the position
%d**\n",q[f],f);
f=(f+1)%qsize;
count--;
}
main()
{
clrscr();
printf("program to perform stack operations\n");

for(;;)
{
printf(" 1:insert\n 2:delete\n");
printf(" 3:display\n 4:exit\n");
printf("enter the choice\n");
scanf("%d",&ch);
switch (ch)
{
case 1:printf(" enter the element to be inserted”);
scanf("%d",&item);
insertrear();
If(count<qsize)
printf("\t item inserted %d at the
position%d\n",item,i++);
display();
break;
case 2: deletefront();
break;
case 3: display();
break;
case 4: exit(0);
}
}
getch();
}
/*OUT PUT
***program to perform stack operations***

1:insert
2:delete
3:display
4:exit
enter the choice
3
**queue is empty**

1:insert
2:delete
3:display
4:exit
enter the choice
1
enter the element to be inserted
10
item inserted 10 at the position 0
**queue contain**
**item 10 is in the position 0**
1:insert
2:delete
3:display
4:exit
enter the choice
1
enter the element to be inserted
20
item inserted 20 at the position 1
**queue contain**
**item 10 is in the position 0**
**item 20 is in the position 1**

1:insert
2:delete
3:display
4:exit
enter the choice
1
enter the element to be inserted
30
item inserted 30 at the position 2
**queue contain**
**item 10 is in the position 0**
**item 20 is in the position 1**
**item 30 is in the position 2**
1:insert
2:delete
3:display
4:exit
enter the choice
2
**deleted item 10 from the position 0**
**queue contain**
**item 20 is in the position 1**
**item 30 is in the position 2**
1:insert
2:delete
3:display
4:exit
enter the choice
2
**deleted item 20 from the position 1**
**queue contain**
**item 30 is in the position 2**
1:insert
2:delete
3:display
4:exit
enter the choice
3
**queue contain**
**item 30 is in the position 2**
1:insert
2:delete
3:display
4:exit
enter the choice
4 */
10/*WRITE A C PROGRAM TO CONVERT AND PRINT A GIVEN
VALIDE FULLY PARENTHESIZED INFIX EXPRESSION TO
POSTFIX EXPRESSION */

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define stack_size 20
char F(char symbol)
{
switch(symbol)
{
case '+':
case '_':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
char G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int top,j,i;
char s[30],symbol;
top=-1;

push('#',&top,s);
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j++]=s[top--];
}
if(F(s[top])!=G(symbol))
push(symbol,&top,s);
else
pop(&top,s);
}
while(s[top]!='#')
{
postfix[j++]=pop(&top,s);
}
postfix[j]='\0';
}
void main()
{
char infix[20];
char postfix[20];
clrscr();
printf("enter valid infix expression\n");
scanf("%s",infix);

infix_postfix(infix,postfix);
printf("postfix expression is %s\n",postfix);
getch();
}
int push(char symbol,int *top,char s[])
{
if(*top==stack_size-1)
{
printf("stack is overflow\n");
}
s[++(*top)]=symbol;
return;
}
int pop(int *top,char s[])
{
char item_deleted;
item_deleted=s[(*top)--];
}

/*OUTPUT
enter valid infix expression
(A+(B-C)*D)
postfix expression is AB-CD*+ */

Вам также может понравиться