Wednesday, 13 July 2016

WRITE A FUNCTION FOR LINEAR SEARCH, WHICH ACCEPTS AN ARRAY OF N ELEMENTS AND A KEY AS PARAMETER AND RETURNS THE POSITION OF KEY IN THE ARRAY AND -1 IF KEY IS NOT FOUND , ACCEPT THE KEY TO BE SEARCHED AND SEARCH IT USING THIS FUNCTION. DISPLAY APPROPRIATE MESSAGES.

#include<stdio.h>
int search(int [],int,int);
main()
{
int i,a[50],s,n,p;
printf("Enter no of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter element to be searched");
scanf("%d",&s);
p=search(a,n,s);
if(p==-1)
{
printf("Element not found");
}
else
{
printf("Position=%d",p+1);
}
}
int search(int a[],int n,int s)
{
int i,p=-1;
for(i=0;i<n;i++)
{
if(a[i]==s)
{
p=i;
break;
}
}
return p;
}





ASSIGNMENT 9)

  TO DEMONSTRATE USE OF 1-D ARRAY AND FUNCTIONS.

No comments:

Post a Comment