Wednesday, November 23, 2011

Recursion

Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied.
A function is called recursive if a statement within in the body of a function calls the same function.When a recursive program is executed, the recursive function calls are not executed immediately.

Advantages of recursive function
1. Used to create clearer and simpler versions of several algorithams
2. Memory occupied when the recursive function is called is very less
3. Very useful way of creating and accessing certain dynamic data structures such as linked lists, stacks, queues,etc.

Pre Processor

The c preprocessor is a collection of special statements called directives that are executed at the beginning of the compilation process, which permits the insertion of files in to a c program.The commonly used preprocesssors are
1.File inclusion
2.Macro definition
3.Conditional compilation
#define     #endif      #ifdef      #line       #elif       #error     #include   # if

All processors begins with # sign and followed by a directive.Preprocessors are usually appear at the beginning of a program, but it may appear any were in the program


Binary Search- program

#include<stdio.h>
#include<conio.h>
voidmain()
{
int a[50],i,j,n,k,min,max,mid,s=0;
clrscr();
printf("enter the range");
scanf("%d", &n);
printf("enter the values")
for(i=1;i<=n;i++)
{
scanf("%d",7a[i]);
}
printf("enter key");
scanf("%d",&k);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j=1]= t;
]
while(min<=max)
{
mid= min=max/2;
if(a[mid]==k)
{
printf("key found");
s=1;
break;
}
else
if(a[mid]>=k)
max=mid-1;
else
if(a[mid]>=k)
min=mid+1;
}
}
if(s==0)
]
printf("not");
}
getch();
}

Bubble Sorting -program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,t;
clrscr();
printf("enter the no. of elements");
scanf("%d",&n);
printf("enter the elements")
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;J++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j=1];
a[j+1]=t;
}
}
}
for(i=1;i<=n;i++)
{
printf("%d",a[i]);
}
getch();
}