- #1
Teh
- 47
- 0
What i am doing work because i can only get 40 30 30 40 as output?
Write a function SwapVectorEnds() that swaps the first and last elements of its vector parameter. Ex: sortVector = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The vector's size may differ from 4.
Testing with original sortVector = {10, 20, 30, 40}
Expected output:
40 20 30 10
Your output:
40 30 30 40
Write a function SwapVectorEnds() that swaps the first and last elements of its vector parameter. Ex: sortVector = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The vector's size may differ from 4.
Code:
#include <iostream>
#include <vector>
using namespace std;
/* Your solution goes here */void SwapVectorEnds(vector<int>& sectorVector){
int i = 0;
for ( i = 0; i < sectorVector.size() - 1; ++i) {
sectorVector.at(i) = sectorVector.at(sectorVector.size() - 1 - i);
}
return;
}
int main() {
vector<int> sortVector(4);
int i = 0;
sortVector.at(0) = 10;
sortVector.at(1) = 20;
sortVector.at(2) = 30;
sortVector.at(3) = 40;
SwapVectorEnds(sortVector);
for (i = 0; i < sortVector.size(); ++i) {
cout << sortVector.at(i) << " ";
}
cout << endl;
return 0;
}
Expected output:
40 20 30 10
Your output:
40 30 30 40