Tuesday, February 17, 2015

Linear Search Implementation Using C language

#include <stdio.h>
int linearsearch(int*,int,int);

int main()
{
int array[50],search,c,n,position;
clrscr();
printf("Enter thE number of elements in array\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);

    for(c=0;c<n;c++)
  
    scanf("%d",&array[c]);
    }

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

position=linersearch(array,n,search);
    if(position==-1)
    {
    printf("%d is not present in array\n",search);
    }
    else
    {
    printf("%d is present at location %d\n",search,position+1);
    }
getch();
return 0;
}

int linersearch(int *pointer,int n,int find)//Implementation of linersearch

function
{
int c;
    for(c=0;c<n;c++)
    {
        if(*(pointer+c)==find)
    {
    return c;
    }
}
return -1;
}

No comments: