Returning value with template function?

I'm not sure if the title makes sense, however, this code was working when i had the function returning the index number of the highest value. When i then changed the function to return the actual highest number itself it now crashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <string>
using namespace std;


template <class ElementType>
ElementType maxArray(ElementType array[], int start, int end);


int main ()
{
	int num[] = {5, 6, 7, 897, 17, 10, 15, 3};
	cout << maxArray(num, 0, 7) << endl;
	
	char str[] = {'a', 'b', 'c', 'h', 'e', 'f', 'g'};
	cout << maxArray(str, 0, 6) << endl;
}



template <class ElementType>
ElementType maxArray(ElementType array[], int start, int end)
{
	
	if (start == end)
	{
		return array[start];
	}
	
	int mid = (start + end) /2;
	
	int left = maxArray(array, start, mid);
	int right = maxArray(array, mid+1, end);
	
	if (array[left] > array[right])
	{
		return array[left];
	}
	else
	{
		return array[right];
	}
}
Just follow what the code is doing.

(Initial recursion omitted. I'll start at the deepest point of the recursion.)

Line 28:
start == 0
end == 0
return array[0] (5)

Line 33:
assign 5 to left

Line 34:
(omitted)

Line 36:
Let's suppose for simplicity's sake that array[left] > array[right] is true.

Line 37:
return array[left] (array[left] == array[5] == 17)

Line 33:
assign 17 to left

Line 34:
(omitted)

Line 36:
Attempt to access array[left] (array[left] == array[17]) is invalid. Program behavior is undefined from this point on.

Do you see what the problem is?
Ohhh!! I see! Darn. Do you have any suggestions for how I could return ElementType? I use to return the index and it worked but I need it to now return ElementType.
> I use to return the index and it worked but I need it to now return ElementType.

1
2
3
4
5
6
7
8
9
10
// existing working function
template < typename  ElementType >
int index_of_highest_value( const ElementType array[], int start, int end ) ;

// new function
template < typename  ElementType >
ElementType actual_highest_value( const ElementType array[], int start, int end )
{
    return array[ index_of_highest_value( array, start, end ) ] ;
}
Oh ok, I see, however, I'm interested in whether or not this is capable without creating a second function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template < typename  ElementType > // note: inclusive interval [start,end]
ElementType highest_value( const ElementType array[], int start, int end )
{
   // if( start < 0 || start > end ) { error: throw something }

   if( start == end ) return array[start] ;

   int mid = ( start + end ) / 2 ;

   const auto highest_left = highest_value( array, start, mid ) ;
   const auto highest_right = highest_value( array, mid+1, end ) ;

   return highest_left < highest_right ? highest_right : highest_left ;
}
Oh! Awesome, I get it now! Thank you!
Topic archived. No new replies allowed.