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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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