Friday, October 2, 2009

ADA 5th sem lab prgms

/* Program No. 1 (b)
C Program to search an element using Linear Search and to find the time taken to search an element..*/
#include
#include
#include
int num[20], max, element, m;
void ls(int m)
{
if(element==num[m])
{
printf("Element is available at the position: %d\n", m+1);
return;
}
if(element>=max)
{
printf("Element is not available\n");
return;
}
ls(m+1);
}
void main()
{
clock_t start, end;
clrscr();
printf("Enter the array size\n");
scanf("%d",&max);
printf("Enter the array elements\n");
for(m=0;m
scanf("%d",&num[m]);
printf("Enter the element to be searched\n");
scanf("%d",&element);
start=clock();
ls(0);
end=clock();
printf("Time taken for linear search is %f",(end-start)/CLK_TCK);
}

No comments:

Post a Comment