Good afternoon,
I was recently assigned a project that implements a recursive function in order to reverse the contents of a vector.
Here are the instructions:
// Write a recursive function reverse that takes a vector of integers (by reference) and a starting and ending position in the vector. It then
reverses the contents of the vector (contents in opposite order) from start to end. The idea behind a recursive solution is that to reverse an entire vector you swap the values at the start and end, then you reverse the remaining inner part of the vector.
Note, you wll always pass the same vector to the recursive call, but you will manipulate start and end to represent the sucessfully smaller inner vector. start and end will also be used to form the base case. The following checks the behavior of reverse and provides examples:
std::vector<int> empty, empty_expected;
reverse(empty, 0, -1);
assert(empty == empty_expected);
std::vector<int> one{ 1 }, one_expected{ 1 };
reverse(one, 0, 0);
assert(one == one_expected);
std::vector<int> two{ 1, 2 }, two_expected{ 2, 1 };
reverse(two, 0, 1);
assert(two == two_expected);
std::vector<int> odd{ 1, 2, 3, 4, 5, 6, 7 }, odd_expected{ 7, 6, 5, 4, 3, 2, 1 };
reverse(odd, 0, odd.size() - 1);
assert(odd == odd_expected);
std::vector<int> even{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, even_expected{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
reverse(even, 0, even.size() - 1);
assert(even == even_expected);
Heres the function I currently have (I understand this might not even be close, but I guess that shows you just how lost I am):
1 2 3 4 5 6 7 8
|
[code]int reverse(std::vector<int> &, int start, int end) {
while (odd.size() >= 0) {
for (int i = odd.size() - 1; i >= 0; i--) {
std::vector<int> = odd[i];
}
}
return 0;
}
| |
I'm especially thrown off by the part in the instructions that says, "Note, you will always pass the same vector to the recursive call...". However, in the examples shown with asserts, it appears a different vector is always passed to the recursive call.
Thank you in advanced for your time and input.