logo CodeStepByStep logo

recursionMystery4X

Language/Type: C++ recursion
Author: Marty Stepp (on 11/04)

For each of the calls to the following recursive function below, indicate the final state of the string that was passed, as well as the value that was returned by the function:

int recursionMystery4X(string& s, int i, int j, char k) {
    if (i >= j) {
        return i;
    } else if (s[i] < k) {
        return recursionMystery4X(s, i + 1, j, k);
    } else if (s[j] > k) {
        return recursionMystery4X(s, i, j - 1, k);
    } else {
        int temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        return recursionMystery4X(s, i + 1, j - 1, k);
    }
}
// a)
//          0123456
string s = "OXIDIZE";
mysteryX(s, 0, 6, 'K')

// b)
//          01234567890123456
string s = "TCTTCGTCCGAACCAGA";
mysteryX(s, 0, 16, 'F')
s after call a)
value returned from call a)
s after call b)
value returned from call b)

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.