Why does this version of the swap function fail to work? Is there a fix?

```
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
```

a) Of course it works! Just look at it. It clearly swaps the two parameters!
b) It fails because the programmer forgot to make the parameters call-by-reference.
c) It fails OK, and we can fix it we can just reverse the order of the lines.
d) It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them.
e) To fix this, we must save the lhs value in a local variable before making the first assignment indicated, then instead of the second line, assign the rhs the value of the local variable:
int local = lhs;
lhs = rhs;
rhs = local;


d) It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them., and e) To fix this, we must save the lhs value in a local variable before making the first assignment indicated, then instead of the second line, assign the rhs the value of the local variable:
int local = lhs;
lhs = rhs;

a) is argumentum ad baculum – argument by the stick. Shout it loud and it is the truth.. b) is incorrect, both parameters are call-by-reference. c) reversing the order of the assignments does not fix the fundamental problem of irretrievably destroying data.

Computer Science & Information Technology

You might also like to view...

Which of the following is not a valid way to pass arguments to a function in C++?

a. By reference with reference arguments. b. By value. c. By reference with pointer arguments. d. By value with pointer arguments.

Computer Science & Information Technology

A ListBox’s __________ property keeps track of the values in the ListBox.

a) Items b) Values c) Entries d) List

Computer Science & Information Technology

The ________ distribution is a discrete probability distribution that is used to model the number of successful trials based on the total number of trials and the rate of success

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Which of the following tools would a technician use to check functionality of a physical network connection?

A. Butt set B. Cable tester C. Protocol analyzer D. Voltage event recorder

Computer Science & Information Technology