Question
Dr. Sam needs a function that will take two sequences of equal length as input and interchange their last element with each other. If any
Dr. Sam needs a function that will take two sequences of equal length as input and interchange their last element with each other. If any one of the sequences is empty, the function does nothing and returns -1. If it could successfully swap the last element of the two sequences, then it returns 0.
Which one of the following function definitions will not disappoint Dr. Sam? Dr. Sam also mentions that the two input sequences can be both lists, or both vectors, or a vector and a list. He assures you that the data contained inside is of the same type.
// Code snippet 1:
template
if(!(s1.size() && s2.size())) return -1;
int temp;
temp = s1.back();
s1.pop_back();
s1.push_back(s2.back());
s2.pop_back();
s2.push_back(temp);
return 0;
}
// The above function is called in the following manner:
// interchange_last(l1, v1); OR interchange_last(v1, l1);
//Code snippet 2:
template
if(!(s1.size() && s2.size())) return -1;
T1 tmp;
tmp.push_back(s1.back());
s1.pop_back();
s1.push_back(s2.back());
s2.pop_back();
s2.push_back(tmp.back());
return 0;
}
// The above function is called in the following manner:
// interchange_last(l1, v1); OR interchange_last(v1, l1);
// Code snippet 3:
template
if(!(s1.size() && s2.size())) return -1;
T tmp;
tmp.push_back(s1.back());
s1.pop_back();
s1.push_back(s2.back());
s2.pop_back();
s2.push_back(tmp.back());
return 0;
}
// The above function is called in the following manner:
// interchange_last(l1, v1); OR interchange_last(v1, l1);
//Code snippet 4:
template
if(!(s1.size() && s2.size())) return -1;
T2 tmp;
tmp.push_back(s2.back());
s2.pop_back();
s2.push_back(s1.back());
s1.pop_back();
s1.push_back(tmp.back());
return 0;
}
// The above functions is called in the following manner:
// interchange_last(l1, v1); OR interchange_last(v1, l1);
Select appropriate choices given below: (
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started