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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary

Discussion Overview

The discussion revolves around identifying the correct loop for reversing a vector in C++. Participants analyze four different code snippets (A, B, C, and D) to determine which one effectively reverses a vector without errors.

Discussion Character

  • Debate/contested

Main Points Raised

  • Post 1 presents four code snippets for reversing a vector, inviting participants to evaluate their correctness.
  • Post 2 suggests tracing the code snippets on a sample vector to understand their behavior, highlighting a potential out-of-bounds error in snippet C when i == 0.
  • Post 3 claims that snippet B is the correct answer, implying that it avoids reversing the vector twice.
  • Post 4 agrees with Post 3, stating that snippet A reverses the vector twice, snippet C produces an error, and snippet D only partially reverses the vector.

Areas of Agreement / Disagreement

There is some agreement that snippet B is correct, while snippets A, C, and D have issues. However, the discussion does not reach a consensus on the definitive correctness of each snippet.

Contextual Notes

Participants note potential errors and behaviors of the code snippets, but there are no formal resolutions to the correctness of each approach.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about vector manipulation and debugging code snippets.

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.
 

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
2K
  • · Replies 36 ·
2
Replies
36
Views
1K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K