4 Replies - 6506 Views - Last Post: 22 June 2011 - 09:47 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Template question : can you please check my answer ?

Posted 22 June 2011 - 06:42 AM

Hi guys ,

I have a test soon in C++ , and here is a little question and I wrote an answer
however I cannot check its correctness . Can you please give me a feedback ?

Question : write the full declaration of a free function (global) named find_it , with 3 
arguments in its prototype : 
1.array of some general type 
2.size of the array 
3.some element we would like to search for 
The function return the index of element , if the element is indeed exists at the array , 
otherwise returns -1 . 



template <class T>
int find_it(T *array ,int size ,T element )
{
	int index = 0;
	while (index < size)
		if (array[index]<T> == element)
			return index;
		else
			index++;
	return -1;
}



10x,Ron

This post has been edited by passer_by: 22 June 2011 - 06:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Template question : can you please check my answer ?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: Template question : can you please check my answer ?

Posted 22 June 2011 - 07:00 AM

Quote

however I cannot check its correctness .


Why can't you test it? As written this will not compile for me.

Quote

main.cpp||In function ‘int find_it(T*, int, T)’:|
main.cpp|6|error: expected primary-expression before ‘>’ token|
main.cpp|6|error: expected primary-expression before ‘==’ token|


Jim
Was This Post Helpful? 0
  • +
  • -

#3 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: Template question : can you please check my answer ?

Posted 22 June 2011 - 07:04 AM

View Postjimblumberg, on 22 June 2011 - 07:00 AM, said:

Quote

however I cannot check its correctness .


Why can't you test it? As written this will not compile for me.

Quote

main.cpp||In function ‘int find_it(T*, int, T)’:|
main.cpp|6|error: expected primary-expression before ‘>’ token|
main.cpp|6|error: expected primary-expression before ‘==’ token|


Jim

yeah,the "<T>" have to go
Was This Post Helpful? 0
  • +
  • -

#4 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 428
  • Joined: 28-September 08

Re: Template question : can you please check my answer ?

Posted 22 June 2011 - 07:50 AM

View Postpasser_by, on 22 June 2011 - 08:04 AM, said:

View Postjimblumberg, on 22 June 2011 - 07:00 AM, said:

Quote

however I cannot check its correctness .


Why can't you test it? As written this will not compile for me.

Quote

main.cpp||In function ‘int find_it(T*, int, T)’:|
main.cpp|6|error: expected primary-expression before ‘>’ token|
main.cpp|6|error: expected primary-expression before ‘==’ token|


Jim

yeah,the "<T>" have to go


Yeah it should be...
/* Removal of <T> */
    if (array[index] == element)
/***/

/* The line below is in the calling function. */
   int num = find_it(y, n, -2); // y is an array and n is 11.
   cout << "Found at element " << num << endl;



Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: Template question : can you please check my answer ?

Posted 22 June 2011 - 09:47 AM

Also the code will not work with C-strings.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1