Swap. Final exam preparation question *question is in the answer section*** | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Swap. Final exam preparation question *question is in the answer section***

How do you move three statements that switches the position when compared using a function called swap and done by pointers? I'm doing this in c++

2nd Dec 2017, 11:09 AM
F7917795
6 Answers
+ 3
Perhaps these are the three statements: int temp = a; a = b; b = temp; Now simply create a swap function like this: void swap(int* a, int* b) { int* temp = a; a = b; b = temp; } And use it on a normal vector like this: vector<int> v1 = {1,2,3,4}; swap(&v1[1],&v1[2]);
3rd Dec 2017, 6:04 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@F7917795 Well, since C++11, they can. But if you are in a C++03 system, it is not valid.
3rd Dec 2017, 7:00 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
I'm not too sure what the practice question means. We learned bubble sort and we learned about MaRe and MiCo. We pretty much just have to create a vector by an initialization list (done) Give the length of this vector using the size() function (done) Make a function using pointers to display the vectors (done...I think) And now the question is "in your sorting there is a group of three statements that switches positions when compared. They must be moved to a new function which is to be named swap(), and this must be done with a pointer." I'm stuck on that last planning question. There's a whole bunch of questions on how my prof wants us to display the results
2nd Dec 2017, 10:47 PM
F7917795
0
Can you demonstrate 'switching' if possible?
2nd Dec 2017, 2:22 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
The display your results is written like this 1) create a second vector identical to the original 2) print either of the vector 3) execute MaRe on the original vector 4) print the sorted original vector 5) execute MiCo on the second vector 6) print the sorted vector
2nd Dec 2017, 10:48 PM
F7917795
0
I was looking through my textbook and it said that vectors can't accept initialization lists
3rd Dec 2017, 6:59 AM
F7917795