Tuesday, 28 February 2012

C program to search an element in an array using Binary search



#include<stdio.h>
main()
{
int a[20],i,j,d,t,x,l=0,low,mid,high;
printf("\nEnter the number of elements\n");
scanf("%d",&d);
printf("Enter the numbers\n");
for(i=0;i<d;i++)
scanf("%d",&a[i]);
for(i=0;i<d;i++)
{
for(j=i+1;j<d;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\nThe sorted list :");
for(i=0;i<d;i++)
printf("%d ",a[i]);
printf("\nEnter the number to be searched\n");
scanf("%d",&x);
low=0;
high=d-1;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
else
{
if(x==a[mid])
{
l++;
printf("The item %d is found at location %d\n",x,mid+1);
exit(0);
}
}
}
if(l==0)
printf("Item not found\n");
}




Saturday, 18 February 2012

C program to implement a queue using linked list



#include<stdio.h>
void enq();
void deq();
void display();
main()
{
int n;
printf("\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

typedef struct node
{
int data;
struct node *link;
}n;
n *front=NULL;
n *rear=NULL;

void enq()
{
int item;
n *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item;
temp->link=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}

void deq()
{
int item;
if(front==NULL)
printf("Queue is empty\n");
else
{
item=front->data;
printf("The element deleted = %d\n",item);
}
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->link;
}

void display()
{
n *ptr;
if(front==NULL)
printf("Queue is empty\n");
else
{
ptr=front;
printf("The elements of the queue are :");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->link;
}
}
}



C program to implement a stack using linked list



#include<stdio.h>
void push();
void pop();
void display();
main()
{
int n;
printf("\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

typedef struct node
{
int data;
struct node *link;
}n;
n *top=NULL;

void push()
{
int item;
n *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item;
temp->link=top;
top=temp;
}

void pop()
{
n *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data);
free(temp);
top=top->link;
}
}

void display()
{
n *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save->data);
save=save->link;
}
printf("\nTopmost element = %d\n",top->data);
}
}



Saturday, 4 February 2012

C program to implement a queue using array



#include<stdio.h>
main()
{
int q[10]={0},i,front=-1,rear=-1,max=10,n,item;
printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(rear<max-1)
{
printf("Enter the element\n");
scanf("%d",&item);
if(rear==-1)
{
front=0;
rear=0;
q[rear]=item;
}
else
q[++rear]=item;
}
else
printf("Overflow\n");
break;

case 2:
if(front>=0)
{
printf("The deleted item =%d",q[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
else
printf("Underflow\n");
break;

case 3:
if((front==-1)&&(rear==-1))
printf("The queue is empty\n");
else
{
printf("The elements of the queue are :");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}



C program to implement a stack using array



#include<stdio.h>
main()
{
int a[10]={0},i,top=-1,max=10,n,x;
printf("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
break;

case 2:
if(top<0)
printf("Underflow\n");
else
printf("The deleted item =%d",a[top--]);
break;

case 3:
if(top<0)
printf("The stack is empty\n");
else
{
printf("The elements of the stack are :");
for(i=0;i<=top;i++)
printf("%d\n",a[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}






Tuesday, 10 January 2012

C program for addition of two times



#include<stdio.h>
main()
{
int h,m,s,h1,m1,s1,h2,m2,s2,day;
printf("Enter first hours,minutes and seconds\n");
scanf("%d%d%d",&h1,&m1,&s1);
printf("Enter second hours,minutes and seconds\n");
scanf("%d%d%d",&h2,&m2,&s2);
s=h=m=day=0;
s=s1+s2;
if(s>60)
{
m=s/60;
s=s%60;
}
m=m+m1+m2;
if(m>60)
{
h=m/60;
m=m%60;
}
h=h+h1+h2;
if(h>24)
{
day=1;
h=h%24;
}
printf("First time = %d:%d:%d",h1,m1,s1);
printf("\nSecond time = %d:%d:%d",h2,m2,s2);
printf("\nAdded time =");
if(day==0)
printf("%d:%d:%d\n",h,m,s);
else
{
printf("%d day",day);
printf("%d:%d:%d\n",h,m,s);
}
}



C program to print a character pattern using pointers



#include<stdio.h>
main()
{
char a[20],*ptr;
int x,y;
printf("Enter any string\n");
scanf("%s",a);
printf("\nThe name is %s",a);
x=0;
while(x<=strlen(a))
{
printf("\n");
ptr=a;
for(y=0;y<x;y++)
{
printf("%c",*ptr);
ptr++;
}
x++;
}
}



C program to print the Fibonacci series using recursion



#include<stdio.h>
void fib(int);
main()
{
int z;
printf("Enter the limit\n");
scanf("%d",&z);
printf("Fibonacci sequence\n");
fib(z);
}
void fib(int z)
{
static int a,b;
int c;
if(z<2)
{
a=0;
b=1;
}
else
{
fib(z-1);
c=b;
b=a+b;
a=c;
}
printf("\n%d",a);
}




C program to find the largest and smallest values of a matrix



#include<stdio.h>
main()
{
int a[10][10],l,s,i,j,p,q;
printf("Enter the order of the matrix\n");
scanf("%d%d",&p,&q);
printf("Enter the values\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&a[i][j]);
}
l=a[0][0];
s=a[0][0];
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if(l<a[i][j])
l=a[i][j];
if(a[i][j]<s)
s=a[i][j];
}
}
printf("The matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The biggest value = %d\n",l);
printf("The smallest value = %d\n",s);
}