code:

LinearSearch(Array A, value)
{
BEGIN
    for i=0 to A.length-1
        if(A[i]==value) then
            return i;
        end if
    end for
    return -1 //if desired value not found return -1
END
}

 

#include<bits/stdc++.h>
using namespace std;

int LinearSearch(int *A,int n,int x)
{
    int i;
    for(i=0; i<n; i++)
    {
        if(A[i]==x)
            return i;
    }
    return -1;
}

int main()
{
    int x,i,n;
    int A[5] = {3,5,9,8,16};
    printf("Put value to search from an array: ");
    scanf("%d",&x);
    int output=LinearSearch(A,5,x);
    if(output==-1)
    {
        printf("%d is not found",x);
    }
    else
    {
        printf("%d is in index %d\n",x,output);
    }


    return 0;
}

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

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