17.4k views
4 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.

1 Answer

3 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 Jcwmoore
by
4.1k points