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 int interchange_last(T1 &s1, T2 &s2){

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 int interchange_last(T1 &s1, T2 &s2){

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 int interchange_last(T &s1, T &s2){

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 int interchange_last(T1 &s1, T2 &s2){

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!