template<typename T> int largest(T* myarray, int n)

How would you write a program that uses the above template function to find the largest element in an int array and in a float array??? :confused:

Printable View

- 03-31-2005micha_mondelic++ Templates
template<typename T> int largest(T* myarray, int n)

How would you write a program that uses the above template function to find the largest element in an int array and in a float array??? :confused: - 03-31-2005Sang-draxCode:
`int array[] = {2, 5, 1, 5, 7};`

int largestValue = largest(array, 5);

- 03-31-2005AH_Tze
Where is the problem?

Can you implement it using a non-template function? If so, getting that code written and posted is a good first step. - 03-31-2005micha_mondeli
how would you implement the function to return the index of the largest element in an array with n elements?

- 03-31-2005CodeMonkey
Depends on the function code, dude.

- 03-31-2005AH_Tze
The logic is not too hard.

The general concept is to start by assuming the first element is the largest.

Then compare the current maximum value the next element. If the next element is larger, save its position instead. Go through the array, element by element in this manner. The final position in your max value variable should be the answer you're looking for.