How to swap the first and last elements of a vector using a function in C++?

In summary, the code creates a function called SwapVectorEnds() that swaps the first and last elements of a given vector. The vector's size may vary. The function first stores the value of the last element in a variable, then replaces the first element's value with the last element's value, and finally replaces the first element's value with the stored value from the last element. This effectively swaps the first and last elements of the vector. The function is then called in the main program and the resulting swapped vector is outputted.
  • #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.
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;
}
Testing with original sortVector = {10, 20, 30, 40}



Expected output:
40 20 30 10



Your output:
40 30 30 40
 
Technology news on Phys.org
  • #2
You are looping over the entire vector, which isn't necessary. What I would do is first store the last element in a variable, put the first element's value into the last element, and then put the value of the variable we stored the last element's initial value into the first element. :)
 

FAQ: How to swap the first and last elements of a vector using a function in C++?

1. How can I modify a vector parameter in my experiment?

To modify a vector parameter, you can use various methods such as vector addition, scalar multiplication, and vector projection. These methods allow you to change the direction, magnitude, or both of a vector to fit your experimental needs.

2. Can I modify a vector parameter without changing its magnitude?

Yes, you can modify a vector parameter without changing its magnitude by using vector projection. This method allows you to change the direction of a vector while keeping its magnitude constant.

3. How do I modify a vector parameter in a specific direction?

To modify a vector parameter in a specific direction, you can use vector projection or vector decomposition. These methods allow you to break down a vector into its components and then modify the components in the desired direction.

4. Is it possible to modify a vector parameter in real-time?

Yes, it is possible to modify a vector parameter in real-time. You can use programming languages such as Python or MATLAB to create a script that continuously updates the vector parameter based on your experimental conditions.

5. Can I modify a vector parameter in a 3D space?

Yes, you can modify a vector parameter in a 3D space using the same methods as in a 2D space. However, in a 3D space, you will also need to consider the z-axis in addition to the x-axis and y-axis.

Similar threads

Replies
1
Views
1K
Replies
22
Views
3K
Replies
8
Views
9K
Replies
5
Views
2K
Replies
23
Views
2K
Replies
10
Views
2K
Back
Top