Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, we have an exam coming up soon from a professor that barely ever teaches the right material and was wondering if I could get

Hello, we have an exam coming up soon from a professor that barely ever teaches the right material and was wondering if I could get a little extra help with this study guide. Thanks!

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Assume V is a list double>, and contains the values 42.5, 11.9, and 18.2 (in that order). Consider the following C++ code fragment: auto X = V.begin(); auto Y = V.end(); auto E1 = *X; auto E2 = *Y; auto PI = 3.14; 1) What is X? Circle the best answer. [ 2 pts] a) X is 42.5 b) X is a pointer to 42.5 c) X is an iterator denoting 42.5 d) X is a pointer to an iterator to 42.5 2) What is E1? Circle the best answer. [2 pts ] a) E1 is 42.5 c) E1 is an iterator denoting 42.5 d) E1 is undefined because *X is illegal b) E1 is a pointer to 42.5 3) What is E2? Circle the best answer. [2 pts ] a) E2 is 18.2 c) E2 is an iterator denoting 18.2 d) E2 is undefined because *Y is illegal b) E2 is a pointer to 18.2 4) Is the type of the variable Pl known at compile-time, or does C++ determine the type at run-time once the assignment takes place? Circle your answer: compile-time / run-time 5) Suppose the following statement is from a Python program: avg = sum / N; Python is a dynamic programming language. Suppose sum has the value 21 and N has the value 2.0. a. Suppose avg is computed. Draw the state of memory (stack and heap), showing how sum, N, and avg are stored. b. Suppose sum has the value 21 but N has the value xyz. Then we execute the code to compute avg. This causes a "type-mismatch" error at run-time --- describe how this error is detected. 6) Why should you learn more than one programming language? 7) What are some of the differences between modern C++ and C? List at least 3 things that you will find in C++ code but not C code. 8) What advantages do references offer over pointers? 9) What does RAll stand for? What does the paradigm mean for C++ developers? 10) For what reason do we use lambda functions? Why do we use lambda functions instead of traditional functions? 11) For what purposes were iterators introduced into the STL? Why do we use iterators instead of references? 12) Consider the following class definition in C++, which describes a bank customer: class Customer private: int ID; double Balance; public: Customer(int id, double balance) : ID(id), Balance(balance) { } double getBalance() const return Balance; Now consider the following main() program: void F(std::vector& V) Customer C3 = V[0]; // STOP HERE int main() std::vector. Using a range-based for (foreach) loop, output the ID for every customer that has a negative balance. Avoid the copying of objects, and do not access private fields. b) Continuation of previous problem... Recall that std::count_if(B, E, P) takes 2 iterators B and E and a predicate function P, and returns the # of elements in the iteration space for which P returns true. Call std::count_if to compute the # of customers in V with a negative balance, and then output this count. You must use a lambda expression in your call to std::count_if; avoid the copying of objects, and do not access private fields directly. 14) Sometimes you just want to apply a function to every element of a container. Define a function named apply that applies a function F to every element of the iteration space (B, E). Note that the function F doesn't return a value; for example, F *could be a function that just outputs its parameter to the screen. Ftakes an element by reference, the function would be able to modify the object being referred to. template void apply(typename Container Type::iterator B, typename Container Type::iterator E, std:: function F) class Customer 15) Suppose V is a std::vector, containing N>O customers. Show how you would call the apply function defined above to add amt to the balance of every customer, where amt is input from the keyboard. Use a lambda expression in the call to apply; avoid the copying of objects, and do not access private fields directly. private: int ID; double Balance; public: Customer (int id, double balance) ID(id), Balance(balance) { } double amt; cin >> amt; int getID() const { return ID; } double getBalance() const { return Balance; } void setBalance (double newBalance) { Balance = newBalance; } 16) Recall that the container std::map is implemented as a balanced search tree. Suppose the variable M is a map containing N (key, value) pairs. Each in case, give you answer using big-O notation. [6 pts, 2pts/each] a) Suppose we search M using std::find_if. What is the average time complexity of this approach? b) Suppose we search M using a range-for loop (aka foreach). What is the average time complexity of this approach? c) Suppose we search M using the member function find(key), which does a key-based search. What is the average time complexity of this approach? 17) Recall that std::sort (B, E, P) takes 2 iterators B and E and a predicate function P which itself takes two parameters of the type being iterated over, which returns true if the two elements are already in order (the first coming earlier in the list), and sorts elements in the iteration space for using the comparison function P to determine order. Suppose V is a vector, assuming the Customer class defined in question 9. Sort V by calling std::sort to sort the customers in ascending order by their ID. Use a lambda expression; avoid the unnecessary copying of objects. Keep in mind you cannot directly access private data members of the Customer class. 18) Assume the Customer class defined in question 12. Write code that will output the ID of the first Customer with a balance above $1000, and None found if there are no customers in the vector which have at least the input number of employees. 19) Consider the following code void makePurchase(Customer c, double price) c.Balance - = price; Customer Craig(15,50.00); makePurchase(Craig, 15.00); std::cout , and contains the values 42.5, 11.9, and 18.2 (in that order). Consider the following C++ code fragment: auto X = V.begin(); auto Y = V.end(); auto E1 = *X; auto E2 = *Y; auto PI = 3.14; 1) What is X? Circle the best answer. [ 2 pts] a) X is 42.5 b) X is a pointer to 42.5 c) X is an iterator denoting 42.5 d) X is a pointer to an iterator to 42.5 2) What is E1? Circle the best answer. [2 pts ] a) E1 is 42.5 c) E1 is an iterator denoting 42.5 d) E1 is undefined because *X is illegal b) E1 is a pointer to 42.5 3) What is E2? Circle the best answer. [2 pts ] a) E2 is 18.2 c) E2 is an iterator denoting 18.2 d) E2 is undefined because *Y is illegal b) E2 is a pointer to 18.2 4) Is the type of the variable Pl known at compile-time, or does C++ determine the type at run-time once the assignment takes place? Circle your answer: compile-time / run-time 5) Suppose the following statement is from a Python program: avg = sum / N; Python is a dynamic programming language. Suppose sum has the value 21 and N has the value 2.0. a. Suppose avg is computed. Draw the state of memory (stack and heap), showing how sum, N, and avg are stored. b. Suppose sum has the value 21 but N has the value xyz. Then we execute the code to compute avg. This causes a "type-mismatch" error at run-time --- describe how this error is detected. 6) Why should you learn more than one programming language? 7) What are some of the differences between modern C++ and C? List at least 3 things that you will find in C++ code but not C code. 8) What advantages do references offer over pointers? 9) What does RAll stand for? What does the paradigm mean for C++ developers? 10) For what reason do we use lambda functions? Why do we use lambda functions instead of traditional functions? 11) For what purposes were iterators introduced into the STL? Why do we use iterators instead of references? 12) Consider the following class definition in C++, which describes a bank customer: class Customer private: int ID; double Balance; public: Customer(int id, double balance) : ID(id), Balance(balance) { } double getBalance() const return Balance; Now consider the following main() program: void F(std::vector& V) Customer C3 = V[0]; // STOP HERE int main() std::vector. Using a range-based for (foreach) loop, output the ID for every customer that has a negative balance. Avoid the copying of objects, and do not access private fields. b) Continuation of previous problem... Recall that std::count_if(B, E, P) takes 2 iterators B and E and a predicate function P, and returns the # of elements in the iteration space for which P returns true. Call std::count_if to compute the # of customers in V with a negative balance, and then output this count. You must use a lambda expression in your call to std::count_if; avoid the copying of objects, and do not access private fields directly. 14) Sometimes you just want to apply a function to every element of a container. Define a function named apply that applies a function F to every element of the iteration space (B, E). Note that the function F doesn't return a value; for example, F *could be a function that just outputs its parameter to the screen. Ftakes an element by reference, the function would be able to modify the object being referred to. template void apply(typename Container Type::iterator B, typename Container Type::iterator E, std:: function F) class Customer 15) Suppose V is a std::vector, containing N>O customers. Show how you would call the apply function defined above to add amt to the balance of every customer, where amt is input from the keyboard. Use a lambda expression in the call to apply; avoid the copying of objects, and do not access private fields directly. private: int ID; double Balance; public: Customer (int id, double balance) ID(id), Balance(balance) { } double amt; cin >> amt; int getID() const { return ID; } double getBalance() const { return Balance; } void setBalance (double newBalance) { Balance = newBalance; } 16) Recall that the container std::map is implemented as a balanced search tree. Suppose the variable M is a map containing N (key, value) pairs. Each in case, give you answer using big-O notation. [6 pts, 2pts/each] a) Suppose we search M using std::find_if. What is the average time complexity of this approach? b) Suppose we search M using a range-for loop (aka foreach). What is the average time complexity of this approach? c) Suppose we search M using the member function find(key), which does a key-based search. What is the average time complexity of this approach? 17) Recall that std::sort (B, E, P) takes 2 iterators B and E and a predicate function P which itself takes two parameters of the type being iterated over, which returns true if the two elements are already in order (the first coming earlier in the list), and sorts elements in the iteration space for using the comparison function P to determine order. Suppose V is a vector, assuming the Customer class defined in question 9. Sort V by calling std::sort to sort the customers in ascending order by their ID. Use a lambda expression; avoid the unnecessary copying of objects. Keep in mind you cannot directly access private data members of the Customer class. 18) Assume the Customer class defined in question 12. Write code that will output the ID of the first Customer with a balance above $1000, and None found if there are no customers in the vector which have at least the input number of employees. 19) Consider the following code void makePurchase(Customer c, double price) c.Balance - = price; Customer Craig(15,50.00); makePurchase(Craig, 15.00); std::cout

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

Step: 3

blur-text-image

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

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions