203k views
1 vote
Write a RECURSIVE function to look for a target key in a list (array) of integer numbers. Code the function in C++/ Java ONLY. (No points for a non-recursive function)

1 Answer

5 votes

Final answer:

A recursive function is a function that calls itself within its own definition. Here's an example of a recursive function in C++.

Step-by-step explanation:

A recursive function is a function that calls itself within its own definition. Here's an example of a recursive function in C++:

int search (int arr[], int size, int key) {

if (size == 0) {

return -1.

}

if (arr[size 1] == key) {

return size 1.

}

return search (arr, size-1, key).

}

This recursive function takes an array of integers, the size of the array, and a target key as parameters. It starts searching from the end of the array and compares each element with the key. If it finds a match, it returns the index of the element. If the end of the array is reached without finding a match, it returns -1.

User Gafi
by
8.3k points

No related questions found