List of all available Websheets
Viewing cpp/references/reverse_ref by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Write a function that reverses a vector that is passed in by reference. E.g., if <tt>vec</tt> contains 2, 0, 1, 5 in that order, after calling <tt>reverse(vec)</tt>, it should contain 5, 1, 0, 2 in that order. <br> Call the <tt>swap</tt> function defined previously to help you. |
Public permissions | |
Engine | |
Template / Reference solution |
using namespace std; // assume this is defined for you void swap(int& a, int& b)\fake[;]\ \hide[ { int tmp = a; a = b; b = tmp; } ]\ // for testing: print contents of vector void print_vec(vector<int> V)\fake[;]\ \hide[ { for (int i=0; i<V.size(); i++) cout << V[i] << " "; cout << endl; } ]\ // complete this function to reverse a vector void reverse(\[vector<int>& v]\) { \[ for (int i=0; i<v.size()/2; i++) swap(v[i], v[v.size()-i-1]); ]\ } int main() { vector<int> test; int x; while (cin >> x) test.push_back(x); cout << "Before reverse: "; print_vec(test); reverse(test); cout << "After reverse: "; print_vec(test); } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [ {"stdin": "1 0 3"}, {"stdin": "4 8 15 16 23 42"}, {"stdin": "9 6 7 1 1 1 1"} ] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.