Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

.II KEKAL SELAMAT... 46 II 6:16 AM

Answered: 1 week ago