Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

q16) CONSIDER THE FOLLOWING CODE: void Question() { string a = \"edcba\"; string

ID: 3810044 • Letter: Q

Question

q16)

CONSIDER THE FOLLOWING CODE:
void Question()
{
      string a = "edcba";
      string b = "fghij";
      cout << "Before:" << endl;
      for (int i = 0; i < a.length(); i++)
            cout << a[i] << " ";
      cout << endl;
      for (int i = 0; i < a.length(); i++)
            cout << b[i] << " ";
      MoveThem(a, b, a.length());
      cout << endl;
      cout << "After:" << endl;
      for (int i = 0; i < a.length(); i++)
            cout << a[i] << " ";
      cout << endl;
      for (int i = 0; i < a.length(); i++)
            cout << b[i] << " ";
}

/////////////////////////////////////////////////////////
void MoveThem(string &x, string &y, int n)
{
      string temp;
      for (int i = 0; i < n; i++)
      {
            temp = x[i];
            x[i] = y[i];
            y[i] = temp[0];
      }
}

Select one:

a. Before:
e d c b a
f g h i j
After:
j i h g f g
e d c b a

b. Before:
e d c b a
f g h i j
After:
f g h i j
e d c b a

c. Before:
e d c b a
f g h i j
After:
f g h i j
a b c d e

d. Before:
e d c b a
f g h i j
After:
e d c b a
f g h i j

please explain how you get the answer. Thank you

Explanation / Answer

The MoveThem(x,y,n) method is simply swapping the string x with the reverse of string y.

The states of both the strings are saved and then the swapping takes place. To better understand this, put a breakpoint on each time the method is called, so that you can see the current state of the Before And After strings.