+ 2
You have the right idea, though your function never actually returns a value so would cause a compiler error. You'd also have an infinite loop if it did, as well as a few other issues. Here's how I'd do it (you can probably figure out where you went wrong with the code below): http://code.sololearn.com/c379CafwFEnE/ #include <iostream> using namespace std; int numbers[]{ 8, 2, 5, 1, 22, 63, 45, 23 }; int findHighestNumber(int arr[], int arraySize, int highest = 0, int index = 0) { if (index == arraySize) { return highest; } else { if (arr[index] > highest) { highest = arr[index]; } return findHighestNumber(arr, arraySize, highest, index + 1); } } int main() { int arraySize = sizeof(numbers) / sizeof(numbers[0]); cout << "Highest number in array: " << findHighestNumber(numbers, arraySize) << endl; return 0; }
12th Sep 2016, 2:52 AM
Liam
Liam - avatar