template in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 15
Reputation: UmH is an unknown quantity at this point 
Solved Threads: 0
UmH UmH is offline Offline
Newbie Poster

template in c++

 
0
  #1
Oct 9th, 2009
hi

Write an overload function "searchArray" so that it takes an
array "ary" of int or char, and additional argument,
namely "valu" with type as same as the "ary". A call to this
function will return the index of "valu" within "ary" or -1
if it does not exist.


and this is what i could write for now ..
  1. int searchArray ( int ary[ ],int a){
  2. for ( int i=0;i<=ary.length;i++){
  3. if (ary[i]== a)
  4. return i;
  5. else
  6. return -1;
  7. }
  8.  
  9. }

it's not complete ..
any one can help me and make it 100% work

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
1
  #2
Oct 10th, 2009
for the general search function, take the else return -1 out of the for loop. That return statement should only execute if you complete the for loop and don't find the search value.

int ary[] does not have a length member. You will have to pass the array length as an additional parameter.

If you google C++ templates you'll find a wealth of information on how to use them - as well as, maybe, reading your textbook? Take a stab at it and show us your work then.
Last edited by vmanes; Oct 10th, 2009 at 1:50 am.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC