Wednesday, May 11, 2022

linear search in array using templates

 #include <iostream>

using namespace std;
template <class t1>
int search(t1 arr[], int size, t1 n)
{
    for (int i = 0; i < size; i++)
    {
        if (arr[i] == n)
        {
            return i;
        }
       
    }
    return -1;
}
int main()
{
    float arr[] = {1, 2, 3};
    int size = (sizeof(arr) / sizeof(arr[0]));
    cout<<"enter element to be searched\n";
    float m;
    cin>>m;
    int n = search<float>(arr, size, m);
    if (n==-1)
    {
        cout<<"not found\n";
    }
    else
    cout<<"found at index "<<n<<endl;
   
}

No comments:

HackCache ’26: A 48-Hour Online Hackathon Open to Every Student

  Hackathons are often associated with computer science students, experienced developers, and teams that already know exactly what they are...

Popular Posts