Sunday, March 17, 2013

Linear Search In A Vector

#include<iostream>
using namespace std;
int main()
{
    int arr[30],i,n,m,pos,flag=1;
    cout<<"Enter size: ";
    cin>>n;
    cout<<"Enter the array elements: ";
    for(i=0;i<n;++i)
    cin>>arr[i];
    cout<<"Enter the element to search for:";
    cin>>m;
    for(i=0;i<n;++i)
    {
        if(arr[i]==m)
        {
            flag=0;
            pos=i;
            break;
        }
    }
    if(flag)
    cout<<"Not Found!!!!!";
    else
    cout<<"Found at "<<pos+1;
    return 0;
}

0 comments:

Post a Comment