57,242 views
32 votes
32 votes
define a function swapfrontend() that has an integer vector parameter passed by reference, and swaps the first and last elements of the vector parameter. the function does not return any value.

User Wukerplank
by
2.8k points

1 Answer

23 votes
23 votes

Answer:

#include <iostream>

#include <vector>

using namespace std;

void swapfrontend(vector<int> &v)

{

int temp = v[0];

v[0] = v[v.size() - 1];

v[v.size() - 1] = temp;

}

int main()

{

vector<int> v;

int num;

cout << "Enter numbers to be inserted in the vector, then enter -1 to stop.\\";

cin >> num;

while (num != -1)

{

v.push_back(num);

cin >> num;

}

swapfrontend(v);

cout << "Here are the values in the vector:\\";

for (int i = 0; i < v.size(); i++)

cout << v[i] << endl;

return 0;

}

User Lonna
by
3.3k points