Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider our CSEVector class from lecture. Assume I have created an instance of

ID: 3809740 • Letter: C

Question

Consider our CSEVector class from lecture. Assume I have created an instance of CSEVector named vector and inserted n integers into vector, for some n > 0. Let foo be the following function: bool foo (const CSEVector& in) {if(in.size() > 0 && in[0] % 2 == 0) {return true;} return false;} What is the runtime of calling foo(vector); ? Now suppose I created a copy of the function foo without const or reference qualifiers, creating a new function foobar: bool foobar (CSEVector in) {if(in.size() > 0 && in[0] % 2 == 0) {return true;} return false;} Does the runtime for foobar (vector); differ? Explain or why not. If changed, provide analysis for the new runtime.

Explanation / Answer

a. The code within foo( ) is only checking only whether the first element is even, no matter how many elements are in the vector. So the runtime of this function is constant and independent of the input size. i.e its runtime is O(1). Also the function is called by reference. So there is no other overhead otherthan runtime of instruction within which is O(1).

b. In case of foobar() also , the statements inside are O(1) for the same reason above. But the function is called by pass by value. Which means there will be copying of data into the local function argument named 'in' from the calling function. This copying of data would need time dependending on the size of vector. So this copying is O(n). So effectively, the function call foobar() would be O(n)+O(1).

Please do rate the answer if it helped. Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote