linear Search

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
int linSearch(int*,int,int);
void main()
{
	int *arr,size,i,searchEle,pos;
	clrscr();
	printf("\nEnter Size Of The Array: ");
	scanf("%d",&size);
	arr=(int *)calloc(size+1,sizeof(int));
	printf("\nEnter Elements: \n");
	for(i=0;i<size;i++)
		scanf("%d",(arr+i));
	printf("\nEnter Element To Be Searched: ");
	scanf("%d",&searchEle);
	pos=linSearch(arr,size,searchEle);
	if(pos==size)
		printf("\nElement Not Found.");
	else
		printf("\nElement Found At %d Position.",pos);
	getch();
}/*End of void main()*/
int linSearch(int *arr,int size,int searchEle)
{
	int pos,i=0;
	*(arr+size)=searchEle;
	while(arr[i++]!=searchEle)
	;
	return i-1;
}/*End of int linSearch(int *,int,int)*/

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *