Monday 21 October 2013

Program In C For Linear Search In An Array

#include <stdio.h>
#include<conio.h>

int main()
{
int A[10],ele,i,n,pos=-1;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d",&n);

printf("Enter numbers:\n");

for (i=0 ;i<n;i++)
scanf("%d",&A[i]);

printf("Enter the number to search\n");
scanf("%d",&ele);

for (i=0;i<n;i++)
{
if ( A[i] == ele )
{   pos=i+1;
printf("%d is present at location %d.\n",ele,pos);
break;
}
}
if ( pos==-1 )
printf("%d is not present in array.\n", ele);
getch();
return 0;
}

Program In C For Circular Queue Implementation Using Array

#include<stdio.h>
#include<conio.h>
void main()
{ int queue[10],SIZE=10,i,ch,c,item,front=-1,rear=-1,c2;
clrscr();
again:
c=1;
printf("QUEUE MENU: ");
printf("\n1.Insertion(Press 1)\n2.Deletion(Press 2)\n3.Display Queue(Press 3):\n");
scanf("%d",&ch);
if(ch==1)
{ while(c==1)
{ if((front==0 && rear==SIZE-1) || (front==rear+1))
printf("\n\nQueue is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == -1)
{
rear = 0;
front = 0;
}
else if(rear == SIZE-1)
rear = 0;
else
rear++;
queue[rear] = item;
printf("\n\nItem inserted: %d\n", item);
}
printf("\nQueue After Insertion Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
printf("\nWant To Insert More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c);
}}
else if(ch==2)
{ while(c==1)
{ if(front == -1)
printf("\n\nQueue is empty.\n");
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == SIZE-1)
front = 0;
else
front++;
printf("\n\nITEM deleted: %d", item);
}
printf("\nQueue After Deletion Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
printf("\nWant To Delete More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c); }}
else if(ch==3)
{ if(front==-1||front==rear+1)
printf("Queue Is Empty !!! ");
else
{ printf("Queue Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
}}
else
printf("Invalid Choice !!!!!! ");
printf("\n1.Back To Main Menu(Press 1)\n2.Exit(Press Any Key):\n");
scanf("%d",&c2);
if(c2==1)
goto again;
}

Program For Queue Implementation Using Array

#include<stdio.h>
#include<conio.h>
void main()
{ int queue[10],max=10,i,ch,c,item,front=-1,rear=-1,c2;
clrscr();
again:
c=1;
printf("QUEUE MENU: ");
printf("\n1.Insertion(Press 1)\n2.Deletion(Press 2)\n3.Display Queue(Press 3):\n");
scanf("%d",&ch);
if(ch==1)
{ while(c==1)
{ if(rear==max)
{ printf("\nOverflow !!!! ");
break; }
printf("\nEnter The Element To Be Inserted In Queue: ");
scanf("%d",&item);
if(front==-1&&rear==-1)
{ front=rear=0; }
else
rear++;
queue[rear]=item;
printf("\nQueue After Insertion Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
printf("\nWant To Insert More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c);
}}
else if(ch==2)
{ while(c==1)
{ if(front==-1||front==rear+1)
{ printf("\nUnderFlow !!!!");
break; }
front++;
printf("Queue After Deletion Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
printf("\nWant To Delete More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c); }}
else if(ch==3)
{ if(front==-1||front==rear+1)
printf("Queue Is Empty !!! ");
else
{ printf("Queue Is: ");
for(i=front;i<=rear;i++)
{ printf("%d\t",queue[i]); }
}}
else
printf("Invalid Choice !!!!!! ");
printf("\n1.Back To Main Menu(Press 1)\n2.Exit(Press Any Key):\n");
scanf("%d",&c2);
if(c2==1)
goto again;
}

Program In C For Stack Implementation Using Array

#include<stdio.h>
#include<conio.h>
void main()
{ int stack[10],max=10,i,ch,c,item,top=-1,c2;
clrscr();
again:
c=1;
printf("STACK MENU: ");
printf("\n1.Insertion(Press 1)\n2.Deletion(Press 2)\n3.Display Stack(Press 3):\n");
scanf("%d",&ch);
if(ch==1)
{ while(c==1)
{ if(top==max)
{ printf("\nOverflow !!!! ");
break; }
printf("\nEnter The Element To Be Inserted In Stack: ");
scanf("%d",&item);
top++;
stack[top]=item;
printf("\nStack After Insertion Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
printf("\nWant To Insert More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c);
}}
else if(ch==2)
{ while(c==1)
{ if(top==-1)
{ printf("\nUnderFlow !!!!");
break; }
top--;
printf("Stack After Deletion Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
printf("\nWant To Delete More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c); }}
else if(ch==3)
{ if(top==-1)
printf("Stack Is Empty !!! ");
else
{ printf("Stack Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
}}
else
printf("Invalid Choice !!!!!! ");
printf("\n1.Back To Main Menu(Press 1)\n2.Exit(Press Any Key):\n");
scanf("%d",&c2);
if(c2==1)
goto again;
}

Program In C For Insertion Sort In An Array

#include<stdio.h>
#include<conio.h>
#include<limits.h>
void main()
{ int a[10],k,i,n,ptr,temp;
clrscr();
printf("Enter The Size Of Array: ");
scanf("%d",&n);
printf("\nEnter Array: ");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]); }
a[0]=INT_MIN;
for(k=2;k<=n;k++)
{ temp=a[k];
ptr=k-1;
while(temp<a[ptr])
{ a[ptr+1]=a[ptr];
ptr=ptr-1;
a[ptr+1]=temp; }  }
printf("\nArray After Sorting Is: ");
for(i=1;i<=n;i++)
{printf("%d\t",a[i]); }
getch();
}

Program In C For Selection Sort In An Array

#include<stdio.h>
#include<conio.h>
int min(int a[ ],int n,int k)
{ int small,loc,j;
small=a[k];
loc=k;
for(j=k+1;j<=n;j++)
{ if(small>a[j])
{ small=a[j];
loc=j; } }
return loc; }
void main()
{ int a[10],n,k,loc,i,temp;
clrscr();
printf("Enter The Size Of Array: ");
scanf("%d",&n);
printf("\nEnter Array: ");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]); }
for(k=1;k<n;k++)
{ loc=min(a,n,k);
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
printf("Array After Sorting Is: ");
for(i=1;i<=n;i++)
{ printf("%d\t",a[i]); }
getch();
}