简体   繁体   中英

C++ recursive function that reverses the order of an array's indexes between two bounds

I am trying to write a recursive function that has three arguments; an array and two array indexes. The function should reverse the order of the values between the two indexes. I would like to understand what is happening instead of just being told an answer.

Here is my code so far:

#include <iostream>
using namespace std;

char switchAroo(char a[], int b1, int b2);

int main()
{
    char a[6] {'A', 'B', 'C', 'D', 'E', '\0'};
    cout << a;
    switchAroo(a, 2, 5);

    return 0;
}

char switchAroo(char a [], int b1, int b2)
{
    char temp;
    if (b1 == b2)
        cout << "The array after switchAroo is " << a << endl;
    else
    {
        temp = a[b1];
        a[b1] = a[b2];
        a[b2] = temp;
        b1++;
        b2--;
        return switchAroo(a, b1, b2);
    }
}

I am getting the following warning code:

warning C4715: 'switchAroo' : not all control paths return a value

When b1==b2, you don't return anything, hence the warning. Since that's your base case, it seems like you'd want to. However, since nowhere do you actually use what gets returned, maybe switchAroo shouldn't return anything?

Your termination condition is wrong. In the first call b1 is 2 and b2 is 5 , the second call it's 3 and 4 respectively, then 4 and 3 , and so on. The condition should be eg

if (b1 >= b2)

You might want to change the argument declaration to unsigned int to prevent the function from being called with negative integer literals.

Also, since you pass 2 and 5 as b1 and b2 for the first call, you actually are switching place of the third and sixth element in the array, meaning that the string termination is moved so you now have a two-character string.

When dealing with recursive functions, it might actually be a good idea to try an design them on paper first, including some example calls with a small set of inputs.

What do you think you are returning from this function? Just ditch the return value

void switchAroo(char a [], int b1, int b2)
{
    char temp;
    if (b1 == b2)
        cout << "The array after switchAroo is " << a << endl;
    else
    {
        temp = a[b1];
        a[b1] = a[b2];
        a[b2] = temp;
        b1++;
        b2--;
        switchAroo(a, b1, b2);
    }
}

BTW the function is bugged, think about what happens with switchAroo(a, 0, 1);

You are switching the null terminator by entering 5. Also there is no need for a return value for switchAroo.

#include <iostream>

using namespace std;

void switchAroo(char a[], int b1, int b2);

int main()
{
    char a[6] = {'A', 'B', 'C', 'D', 'E', '\0'};
    cout << a << endl;
    switchAroo(a, 2, 4);
    cout << a << endl;

    cin.get();

    return 0;
}

void switchAroo(char a [], int b1, int b2)
{
    if (b1 >= b2)
        return;

    swap(a[b1], a[b2]);
    switchAroo(a, b1 + 1, b2 - 1);
}

First, let's clarify the warning. switchAroo function must return a char. You return only in else branch. So, if the condition is true (ie b1==b2) you don't return anything. Also, you don't need to return something in this function.

Speaking about 'what's happening', you need to swap all elements between index i and index j of array a. Let's say you have a function that swaps 2 variables. You need to call that function for every pair of indexes x, y, with i<=x

The recursive approach is similar. First, you have a stopping condition (the correct stopping condition is b1 >= b2, as explained in previous answers). If that condition is not satisfied, you still have at least one pair of elements to swap (that includes b1, b2 pair). So, you swap b1 b2, and find another pair. If there is a valid pair, the pair b1+1, b2-1 is also valid (you start from the "outside" of the interval to the "inside" so the remaining pairs are closer to the middle of the interval). You call switchAroo to swap b1+1, b2-1 pair. If this pair is not valid, you reached the middle of the interval, and this means that you swapped everything you wanted. This last call has the stopping condition true, so the recursion stops here.

I hope I was clear enough and you understood how it works. The bugs you had in code are fixed in previous answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM