I am new to C++ and I need some help getting this code started for this problem. I am asked to do this :
Write a function template version of linearSearch, that can be used to search an array of elements of any type.
Write the function prototype , the function template, and a main function to test it. Test it for an integer array , a double array,and a char array , Also test it when searching for a value that is not in the array.
Any help would be apperciated as this is due in 2 days.

Thank You

Dani AI

Generated

is right that getting a plain, working routine first makes the templating step trivial. : the template header is template <typename T>. Below is a concise prototype + function template and a main that tests an int, a double, and a char array — with both a found and a not-found case. 's reminder about template instantiation is worth noting: some template-related errors only show up for the types you actually instantiate.

#include <iostream>

template <typename T>
int linearSearch(const T arr[], int n, const T& key); // prototype

int main() {
    int ia[]   = {1, 3, 5, 7, 9};
    double da[] = {1.5, 2.5, 3.5};
    char ca[]  = {'x', 'y', 'z'};

    int idx = linearSearch(ia,  sizeof(ia)/sizeof(ia[0]), 5);
    std::cout << "int 5 -> index " << idx << '\n';
    idx = linearSearch(ia,  sizeof(ia)/sizeof(ia[0]), 2);
    std::cout << "int 2 -> index " << idx << '\n';

    idx = linearSearch(da, sizeof(da)/sizeof(da[0]), 2.5);
    std::cout << "double 2.5 -> index " << idx << '\n';
    idx = linearSearch(da, sizeof(da)/sizeof(da[0]), 9.9);
    std::cout << "double 9.9 -> index " << idx << '\n';

    idx = linearSearch(ca, sizeof(ca)/sizeof(ca[0]), 'y');
    std::cout << "char 'y' -> index " << idx << '\n';
    idx = linearSearch(ca, sizeof(ca)/sizeof(ca[0]), 'a');
    std::cout << "char 'a' -> index " << idx << '\n';

    return 0;
}

template <typename T>
int linearSearch(const T arr[], int n, const T& key) {
    for (int i = 0; i < n; ++i)
        if (arr[i] == key) return i;
    return -1; // not found
}

Notes and troubleshooting:

  • The element type must support operator==. Pointer types compare addresses, not contents.
  • Arrays decay to pointers when passed to functions — always pass the size or use std::vector/std::array instead.
  • Floating-point comparisons can be fragile; consider a tolerance for real-number searches.
  • If searching character strings (C-strings), compare contents (e.g., std::string or strcmp) rather than raw char* pointers.
  • You can also use std::find from <algorithm> with iterators as a ready-made alternative.

This covers the prototype, a template implementation, and a simple test harness for the three requested types plus not-found cases.

Recommended Answers

All 3 Replies

One way I would approach this, is not to worry about templating the function at all until you get a function that does what you want it to do. Once you got that down, you can easily go back and do all the necessary templating.

With that said, just concentrate on writing a basic array sorting function. I would google for c++ bubblesort.

Btw, you have failed to in virtually every aspect, so I suggest you don't come back until such time as you show some sort of effort on your part.

Do you know how to declare a template function?

>One way I would approach this, is not to worry about templating the
>function at all until you get a function that does what you want it to do.

Good advice. This is especially useful when writing large template classes due to the "feature" of not instantiating templates that aren't actually used. While some compilers are smart enough to catch it (given the right switches), it's possible to write uncompilable code that doesn't get caught at compile-time:

template <typename T>
class foo {
public:
  // Compiles...aroo?
  void broken() { asdf }
};

int main()
{
  foo<int> bar;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.