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

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 ask an intelligent question 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.