C/C++ Reversing Vector in C++: Which Loop is Correct?

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary
The discussion centers on the correct method for reversing a vector, evaluating four different code snippets. Code A is deemed incorrect as it reverses the vector twice, while Code C generates an out-of-bounds error by attempting to access an index equal to the vector's size. Code D fails to fully reverse the vector, as it only overwrites the first half with the reversed second half without altering the latter. Code B is identified as the correct approach, effectively reversing the vector by iterating only through half of its elements, ensuring that each element is swapped with its corresponding counterpart. The importance of tracing code execution with sample vectors is highlighted as a valuable skill for understanding these operations.
ineedhelpnow
Messages
649
Reaction score
0
which loop is correct for reversing a vector? please help.
A.
for (i = 0 ; i < NUM_ELEMENTS ; ++i ) {
tmpValue = revVctr .at (i ) ;
revVctr .at (i ) = revVctr .at (NUM_ELEMENTS - 1 - i ) ;
revVctr .at (NUM_ELEMENTS - 1 - i ) = tmpValue ;
}

B.

for (i = 0 ; i < (NUM_ELEMENTS / 2 ) ; ++i ) {
tmpValue = revVctr .at (i ) ;
revVctr .at (i ) = revVctr .at (NUM_ELEMENTS - 1 - i ) ;
revVctr .at (NUM_ELEMENTS - 1 - i ) = tmpValue ;
}

C.
for (i = 0; i < NUM_ELEMENTS; ++i) {
revVctr.at(i) = revVctr.at(NUM_ELEMENTS - i);
}

D.
for (i = 0 ; i < NUM_ELEMENTS ; ++i ) {
revVctr .at (i ) = revVctr .at (NUM_ELEMENTS - 1 - i ) ;
}
 
Technology news on Phys.org
An ideal way to solve this would be to take a sample vector with, say, 5 different elements and trace all four code snippets on this vector. Tracing a code on paper is an important skill to master. If you are short on time or have questions how to do this, please let us know. I just want to point out that in fragment C, when [m]i == 0[/m], we have [m]NUM_ELEMENTS - i == NUM_ELEMENTS[/m], so [m]revVctr.at(NUM_ELEMENTS - i)[/m] will produce the array-out-of-bounds error, if I understand correctly.
 
the answer was B. (which i guessed correctly) otherwise i believe it would reverse twice.
 
You are right. Code A reverses the vector twice, code C produces an error and code D overwrites the first half by the reversed second half but does not change the second half.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
852
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K