Please write these problems in C++ format 10. Write a function named reverse tha
ID: 3626513 • Letter: P
Question
Please write these problems in C++ format
10. Write a function named reverse that reverses the order of all elements in a vector of
strings passed in as a parameter to the function.
For example, when the function is called, if the vector argument stores the values,
"a", "cat", "ball", "at", "zed", "to" in this order, then when the function returns, the argument should store those same values but now in the following order:
"to", "zed", "at", "ball", "cat", "a"
11. Write a function named rotate_left that passes in a vector of strings and a single int
value. The function should shift to the left every value in the vector by n slots, with n being the integer value passed in. Also, the first n elements will then become the new last n elements (thus the name rotate rather than shift).
For example, when the function is called, if the vector argument stores the values,
"a", "zed", "tom", "cat", "boo", "con"
in this order originally, and the integer value passed in is 2, then when the function returns,the values in the vector will now be in the following order:
"tom", "cat", "boo", "con", "a", "zed"
Notice that the first 2 values "a" and "zed" are now the last 2 values in the vector.
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
void reverse(vector<string>& v)
{
int k=v.size()-1;
for(int i=0; i<v.size()/2; i++)
{
string temp = v[i];
v[i] = v[k];
v[k]=temp;
k--;
}
}
int main()
{
string a[]={"a", "cat", "ball", "at", "zed", "to"};
vector<string> v(a,a+6);
cout << "before reverse" <<endl;
for(int i=0; i<v.size(); i++)
cout<<v[i]<<" ";
reverse(v);
cout << " after reverse" <<endl;
for(int i=0; i<v.size(); i++)
cout<<v[i]<<" ";
return 0;
}