Final Exam PIC 10B, Spring 2016 Name: Student ID: Section: Discussion 3A (2:00 2:50 with Kelly) Discussion 3B (3:00 3:50 with Andre) I attest that the work presented in this exam is my own. I have not used any outside resources that might afford me an unfair advantage in taking this exam. Signature:
Directions: You have 180 minutes to answer the following ten (10) questions. In the code provided, you may assume that all necessary header files are included (e.g., #include <iostream>, #include <string>, etc.). Each question is worth 10 points. Problem Score 1 /10 2 /10 3 /10 4 /10 5 /10 6 /10 7 /10 8 /10 9 /10 10 /10 Total /100
Problem 1. (10 pts) Consider the Fraction class, whose partial declaration was given by 1 class Fraction { 2 public : 3 Fraction ( int num, int den ); 4... 5 int num ; // numerator of the Fraction 6 int den ; // denominator of the Fraction 7 } Use operator overloading to declare and define the operator < so that the following code will compile and run: 1 Fraction a, b; 2... 3 if(a < b) { 4 std :: cout << " a is smaller " << std :: endl ; 5 }
Problem 2. (10 pts) You are writing software for a car dealership and you need to represent the various parts of the business in your code. In particular, the car dealership deals with the following objects, each of which will be represented by a class: 1 Vehicle, VehiclePart, SportsCar, RacingTire, Truck, Car, Tire, SteeringWheel (a) Draw an inheritance diagram that shows the relationships between the classes listed above. (4 pts)
(b) The class Vehicle has a member function void test drive(), which allows potential buyers to take the vehicle for a test drive. For a SportsCar, the buyer can reserve time on a test track. Use polymorphism to declare the the function test drive() in the Car class so that the code 1 Car * ferrari = new SportsCar (); 2 ferrari -> test_drive (); calls the SportsCar version of test drive() rather than the Car version. (2 pts) Declare the class SportsCar as derived class from Car, and override the test drive() function. The overridden test drive() function should (1) call the base class (Car) version of test drive(), then print the message "Reserving the test track for your sports car.". (4 pts)
Problem 3. (10 pts) You are given a file data.dat which contains entries consisting of names and ages. For example, the contents of data.dat could be Eve 24 Bob 27 Alice 22 Dave 25 You may assume that each line contains one name followed by one or more whitespace characters followed by a positive integer. The first letter of each name is capitalized, and the names are unique. (a) Write a piece of code that reads data.dat and stores the contents to a std::map where the keys are the names, and the values are the ages. (5 pts)
(b) Write a piece of code that prints the contents of the std::map created in part (a) to std::cout such that (1) the names are in alphabetical order and (2) all the ages are aligned in a second column. For example after reading data.dat as on the previous page, your program should print Alice 22 Bob 27 Dave 25 Eve 24 You may assume that no name contains more than 10 characters. (5 pts)
Problem 4. (10 pts) Consider the definition of the Dragon class: 1 class Dragon { 2 public : 3 Dragon () { 4 std :: cout << " You made a dragon!" << std :: endl ; 5 } 6 7 Dragon () { 8 std :: cout << " You slayed a dragon!" << std :: endl ; 9 } 10 }; (a) What is the output of the following code? (7 pts) 1 void make_ dragons () { 2 Dragon first ; 3 Dragon * second = new Dragon (); 4 } 5 6 int main () { 7 make_ dragons (); 8 return 0; 9 } (b) What is the error with the function void make dragons()? (3 pts)
Problem 5. (10 pts) Consider an std::vector<int> v of size n which is known to contain only values 1 through n (values may appear more than once in v, and some values may not appear at all). (a) Write a function void print missing values(std::vector<int>& v) which prints the values 1 through n which do not appear in v using the following procedure: for each i = 1, 2,..., n, see if v contains i using linear search. If i is found, do nothing; otherwise print i. (4 pts)
(b) Use Big O notation to describe the runtime of print missing values implemented as described in part (a). (2 pts) (c) Describe (without writing any code) how you could improve the runtime of print missing values to O(n log n). (2 pts) (d) Is it possible to implement print missing values in time O(n)? How or why not? (2 pts)
Problem 6. (10 pts) Consider an std::vector<int> v. (a) Assuming that v is sorted (smallest to largest), define a function 1 size_t find ( std :: vector <int >& v, int val, size_t min, size_t max ); which employs binary search to return the index of val if val is contained in v between indices min and max. The function find should return -1 if val is not found. The function find may be either defined recursively or iteratively. (8 pts) (b) Use Big O notation to describe the run time of find when v contains n (sorted) entries. You do not need to justify your answer. (2 pts)
Problem 7. (10 pts) Consider the (simplified) TreeNode class below. 1 class TreeNode { 2 public : 3 TreeNode ( int val ); 4 5 TreeNode * parent ; 6 TreeNode * left_ child ; 7 TreeNode * right_ child ; 8 int value ; 9 }; Implement a function 1 TreeNode * insert ( TreeNode * root, int val ); which searches a Binary Search Tree (BST) for val. If val is found in the BST, insert returns a pointer to the TreeNode containing val. If val is not found, insert should create a new TreeNode with value equal to val, and insert it into the BST so as to maintain the BST properties. You may assume that the TreeNode* root passed to insert is not nullptr.
Problem 8. (10 pts) (a) Starting from an empty Binary Search Tree (BST), draw the BST after inserting in order 3, 1, 8, 5, 4, 7, 6, 9, 2. (4 pts)
(b) For the BST you created in part (a), write the output of printing the contents of the BST during an in-order traversal. (2 pts) (c) For the BST you created in part (a), write the output of printing the contents of the BST during an pre-order traversal. (2 pts) (d) For the BST you created in part (a), write the output of printing the contents of the BST during an post-order traversal. (2 pts)
Problem 9. (10 pts) Consider the function void puzzle(int n) defined by 1 void puzzle ( unsigned int n) { 2 if(n == 1) { 3 std :: cout << "1" << std :: endl ; 4 return ; 5 } 6 7 std :: cout << n << ", "; 8 9 if(n % 2 == 0) { 10 puzzle (n /2); 11 } 12 13 else { 14 puzzle (3 * n + 1); 15 } 16 } (a) Write the output of puzzle for the following inputs (8 pts): puzzle(2): puzzle(3): puzzle(7): puzzle(21): (b) Which best describes the run-time of puzzle(n) when n = 2 k? (circle one) (2 pts) 1. O(1) 2. O(log k) 3. O(k) 4. O(k 2 ) 5. O(2 k ) Fun fact: it is unknown whether the function puzzle(int n) eventually stops for all positive integers n!
Problem 10. (10 pts) Arithmetic expressions involving integers and the operations + and * can be represented using a binary tree as follows. Each internal node (i.e., a node which is not a leaf) stores a single operator, while each leaf stores a single value. For example the tree * + 3 1 2 encodes the expression (1 + 2) * 3. A binary tree constructed in this way is known as an expression tree. To evaluate an expression tree, start at the root, and evaluate the left and right subtrees recursively. Apply the operator at the root to the values returned from the left and right children, and return the new value. Evaluation of a leaf simply returns its value. Consider the class ExpTreeNode defined below: 1 class ExpTreeNode { 2 public : 3 ExpTreeNode (); 4 virtual int evaluate () { return 0;} 5 6 ExpTreeNode * parent ; 7 ExpTreeNode * left_ child ; 8 ExpTreeNode * right_ child ; 9 }; (a) Define and implement a class derived from ExpTreeNode called ValueNode which additionally stores an int value. For ValueNode, redefine the evaluate() function to return the node s value. (2 pts)
(b) Define and implement a class derived from ExpTreeNode called OperatorNode which stores a char op, the operator associated with the node (+ or *). Redefine the evaluate() function for an OperatorNode so that the node evaluates its left and right children, applies the operator specified by op, and returns the result. (4 pts)
(c) What is the result of evaluating the expression tree below? (2 pts) * + + 2 4 1 + 3 5 (d) Write the contents of the tree from part (c) using a post-order traversal. Evaluate the resulting expression as an expression in postfix (reverse Polish) notation. (2 pts) (e) Do you recognize the result of an in-order traversal of the expression tree from (c)? It may be helpful if you include an open parenthesis before processing left children and a closing parenthesis after processing right children... (0 pts)
(blank page)