943,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 848
  • C++ RSS
Nov 23rd, 2009
0

Linear Search Template

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kfg20 is offline Offline
1 posts
since Nov 2009
Nov 23rd, 2009
0
Re: Linear Search Template
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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 24th, 2009
0
Re: Linear Search Template
Do you know how to declare a template function?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008
Nov 24th, 2009
2
Re: Linear Search Template
>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:
C++ Syntax (Toggle Plain Text)
  1. template <typename T>
  2. class foo {
  3. public:
  4. // Compiles...aroo?
  5. void broken() { asdf }
  6. };
  7.  
  8. int main()
  9. {
  10. foo<int> bar;
  11. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reset left side of +=
Next Thread in C++ Forum Timeline: C++ Programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC