Sale!

CSC 340.03+.04 MIDTERM EXAM 02 solved

Original price was: $35.00.Current price is: $28.00.

Category:

Description

5/5 - (1 vote)

 PART A – 50 Points

A.1 – 10 pts

Please explain in detail Scott Meyers’s point: Use weak_ptr for shared_ptr like pointers that can dangle.

A.2 – 10 pts

How are Smart Pointer functions move(), reset(), and release() different from each other? Please also explain in detail which function is most dangerous and why?

A.3 – 16 Points

… class Name { public: Name() {} Name(string name) { this->name = name; } ~Name() { cout << this->name << “: Destructor called.” << endl; } string getName() const { return this->name; } private: string name{ “N/A” }; }; void passByMove(const unique_ptr uPtr_M) { cout << “@uPtr_M: ” << uPtr_M << endl; cout << “getName(): ” << uPtr_M->getName() << endl; } void passByRef(const unique_ptr& uPtr_R) { cout << “@uPtr_R: ” << uPtr_R << endl; cout << “getName(): ” << uPtr_R->getName() << endl; }

void passByShare(const shared_ptr sPtr_S) { cout << “@sPtr_S: ” << sPtr_S << endl; cout << “getName(): ” << sPtr_S->getName() << endl; cout << “use_count(): ” << sPtr_S.use_count() << endl; } Name* passByValue(const unique_ptr uPtr_V) { cout << “@uPtr_V: ” << uPtr_V << endl; cout << “getName(): ” << uPtr_V->getName() << endl; return uPtr_V.get(); } int main() { cout << passByValue(make_unique(“Goofy”)) << endl; unique_ptr uPtr{ make_unique(“Mickey”) }; passByRef(uPtr);

cout << “name_uPtr: ” << uPtr << endl; passByMove(move(uPtr)); cout << “name_uPtr: ” << uPtr << endl; uPtr = make_unique(“Minnie”); shared_ptr sPtr{ uPtr.release() }; passByShare(sPtr); cout << “END of Program” << endl; return 0; }

How many lines does this program output? ___________ Please give the output of the program. Use @A, @B, @C, @D, and nullptr to represent memory addresses. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 CSC 340.03+.04 MIDTERM EXAM 02 SPRING 2022 TA 6 A.4 – 4 pts Please explain in detail how to manually destroy an existing Smart Pointer control block. A.5 – 10 Points … int funcB(int); int funcA(int n) { if (n <= 1) return 217; else return n + funcB(n – 2); } int funcB(int n) { if (n <= 2)

{ return 3; } else { if (n > 4) { return n * funcA(n – 5); } else { return n – funcB(n – 1); } } } int main() { cout << funcA(13); return 0; } What is the output of this program? Please show our work.

PART B – 40 Points

B.1 – 20 Points

… class Name { public: Name() {} private: string name{ “CS” }; }; shared_ptr func() { unique_ptr obj{ make_unique() }; // #1 Insert Code } int main() { // #2 Insert Code } [ #1 Insert Code]: Write code to keep the object which obj owns alive outside of the scope of function func. Hint: The code should also support our task in #2 Insert Code. [ #2 Insert Code]: Write code to test if the object owned by obj is alive in the scope of function main. If it is, please output its address. If not, please output “Object destroyed.”

B.2 – The Problem, 5 Points Similar to what we did in ASMT 4, we are to carry out the three following steps. We use 1. Copy constructor to create a new bag using a bag passed in. However, please note that the order in which this constructor stores the items is the reverse of the original order (in which the items appear in the passed-in bag).

2. Function addVector, which takes a vector as the only parameter, to add the entries from the vector to the new bag. 3. Function removeLastThree, which takes an item as the only parameter, to remove the last three occurrences of the item in the bag. It is OK to make assumptions. Please state our assumptions, if any. Please explain in detail how each step works and what the new bag contains after each of the steps is executed. – Please use linked Nodes diagrams in our answer. – Please use the data below in our explanation. 1. Passed-in bag: ‘e’, ‘l’, ’e’, ’c’, ’t’, ’r’, ’i’, ’c’, ’a’, ’l’ 2. Passed-in vector: ’e’, ’n’, ’g’, ’i’, ’n’, ’e’, ’e’, ’r’ 3. Passed-in item: ‘e’ Copy constructor (reverse order) function addVector CSC 340.03+.04 MIDTERM EXAM 02 SPRING 2022 TA 9 function removeLastThree

B.3 – The solution, 15 Points Please code the Copy Constructor (the reverse) and the removeLastThree function described in B2 without using the existing functions.

PART C – 10 Points

C.1 – 5 pts

What did the IDEO team do during the Problem Formulation phase? What was the most important question? Can we use the same approach in Software Development?

C.2 – 5 pts

Your programming team lead/leader asks you to use raw pointer to implement a memory bound function. You know that using Smart Pointers is the way to go. And you heard that the leader does not know Smart Pointers. What would you do and say? Please provide your answer in detail. 11 PLEASE DO NOT DETACH THIS PAGE FROM YOUR EXAM. You may get partial credit for what you write on this page.