Sunday, September 30, 2018

How do we search an element in a given array(LINEAR SEARCH)??

1.Declare an array and also its size.
2.Enter the elements in an array.
3.You have to take that element by user which user want to search in a given array.
4.Using a loop search that element in given array.
5.If that element is found print the location and break that loop.
6.Otherwise increment the count.
7. If count is equal to size of array or greater than the size of array .
7.Print element does not found.

#include<stdio.h>
int main()
{
    int n,i,j,count=0;
    scanf("%d",&n);
    int arr[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("enter the number which has to be search");
    scanf("%d",&j);
    for(i=0;i<n;i++)
    {
        if(arr[i]==j)
        {
            printf("%d",i+1);
break;
        }

    else
    {
       count++;
    }
    }
    if(count>=n)
     printf("element does not found");

    return 0;
}

No comments: