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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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