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
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:
Code:int array[] = {2, 5, 1, 5, 7};
int largestValue = largest(array, 5);
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.
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.