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

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Parameter Vector
AI Thread Summary
The discussion centers on a coding issue related to a function designed to swap the first and last elements of a vector. The initial implementation incorrectly loops through the entire vector, resulting in unexpected output. The suggested solution involves a more efficient approach: storing the last element in a temporary variable, assigning the first element's value to the last position, and then placing the stored value into the first position. This method simplifies the process and ensures the correct output, which should be {40, 20, 30, 10} when starting with {10, 20, 30, 40}.
Teh
Messages
47
Reaction score
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
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. :)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top