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:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts