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???
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???
Code:int array[] = {2, 5, 1, 5, 7}; int largestValue = largest(array, 5);
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
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.
how would you implement the function to return the index of the largest element in an array with n elements?
Depends on the function code, dude.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
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.