C program to implement linear search using function and pointer

Posted: January 28, 2012 in Lab 5
#include<stdio.h>
#include<stdlib.h>
int c=0;
int linearsearch(int *, int,int);
int linearsearch(int *a,int b,int  n)
{
	int i;
for(i=0;i<=n-1;i++)
    {
         if(*(a+i)==b)
	   {
             c=1;
             break;
         }
    }
	return i;

}

int main()
{
	int *a,i,n,m;

    printf("Enter the size of an array: ");
    scanf("%d",&n);
	a=(int*)malloc(n*sizeof(int));

    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++)
    {
         scanf("%d",(a+i));
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);
	i=0;
	i=linearsearch(a,m,n);
    
    if(c==0)
         printf("The number is not in the list\n");
    else
         printf("The number is found at position %d\n",i+1);

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s