157k views
4 votes
Write a recursive function that has an argument that is an array of characters and two arguments that are bounds on array indexes. The function should reverse the order of those entries in the array whose indexes are between the two bounds. For example, if the array is a[0]

User Rosangela
by
3.9k points

1 Answer

2 votes

Answer:

The function is as follows:

void revArray(char arr[], unsigned int low, unsigned int high) {

if (low >= high){

return; }

swap(arr[low], arr[high]);

revArray(arr, low + 1, high - 1); }

Step-by-step explanation:

This declares the function. It receives a char array and the lower and upper bounds to be reversed

void revArray(char arr[], unsigned int low, unsigned int high) {

This is the base case of the recursion (lower bound greater than or equal to the upper bound).

if (low >= high){

return; }

This swaps alternate array elements

swap(arr[low], arr[high]);

This calls the function for another swap operation

revArray(arr, low + 1, high - 1); }

User Naaman Newbold
by
4.9k points