The answers are in red. Please explain the answer using C++ logic Use the follow
ID: 3626522 • Letter: T
Question
The answers are in red. Please explain the answer using C++ logic
Use the following function definition for the next 2 questions
void swap( int &a, int &b )
{
int temp = a;
a = b;
b = temp;
}
7. Which of the following statements correctly calls the swap
function defined above to swap the values at the front and back of a vector of integers named v?
(Assume v stores at least 2 integers.)
a. swap(0, v.size());
b. v.swap(0, v.size());
c. v[0] = v[v.size()];
d. swap(v[1], v[v.size()]);
e. swap(v[0], v[v.size() - 1]);
f. swap(v, v.size() - 1);
8. Assuming an integer variable named x has been declared and initialized to a
positive value less than 10, which of the following statements correctly calls the
swap function defined above to swap the values at index x of a vector integers named v with the value in v stored immediately after index x?
(Assume v has a size of at least 20.)
a. swap(x, x + 1);
b. v.swap(x, x + 1);
c. v[x] = v[x] + 1;
d. swap(v[x], v[x + 1]);
e. swap(v[x], v[x] + 1);
f. swap(v + x, v + x + 1);
9. Assuming v is a vector of integers and n is a variable of type int that have been
declared and initialized with positive integers, which of the following statements has a
syntax error?
a. vector v1(n);
b. vector v2 = v;
c. v[n] = 10;
d. v[10] = n;
e. cout << v[n] + n;
f. cout << v;
Explanation / Answer
in order to swap the contents of vector u have to pass contents of vector.
only in option e you are sending contents of vector
swap(v[0],v[v.size()-1]) is correct choice.
to swap values at location index u have pass vector value at that location and ...
d. swap(v[x], v[x + 1]); is correct choice.
v is Vector of integers
u cant print by statement cout << v;
which is a syntax error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.