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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vector
AI Thread 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.
 
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...

Similar threads

Replies
1
Views
2K
Replies
10
Views
2K
Replies
23
Views
3K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
36
Views
448
Replies
22
Views
3K
Back
Top