Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why does this version of the swap function fail to work? Is there a fix? ( Choose 2 ) void swap ( int & lhs

Why does this version of the swap function fail to work? Is there a fix? (Choose 2)
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
Group of answer choices
It fails OK, and we can fix it we can just reverse the order of the lines.
a) 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;
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.
It fails because the programmer forgot to make the parameters call-by-reference.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions

Question

14.5 Describe how accidents at work can be prevented.

Answered: 1 week ago

Question

For what purposes are departmental reports useful to management?

Answered: 1 week ago

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago