CS 12400-MW Final Exam Review The final exam is on Wednesday, May 3, at 8:00 A.M. Please note the time, which is different than our normal meeting time. The final exam is over the whole semester. Be sure to review the review problems for each of the first two midterm exams and also review the two exams themselves. The sections covered since the second midterm exam are from Chapters 7 and 9 in the textbook, https://learn.zybooks.com/zybook/PurdueNorthwest-HammondCS124KraftSpring2023 Sections 7.14, 7.15. Sections 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 9.10, 9.16, 9.17. The topics covered since the second midterm exam are: 1) structures, structures and pointers, structures and arrays, automatic structures vs. dynamic structures. memory diagrams of structures. 2) classes, instance variables (fields), instance methods, constructors, interface, implementation, this keyword, objects (instances of classes), pointers to objects, dot vs. arrow notation, automatic objects vs. dynamic objects, memory diagrams of objects. Below are review problems for the material we have covered since the second midterm exam. For the final exam you should also study your copies of Exam 1 and Exam 2 plus the review problems for those two exams. Problem 1.) Structures can contain structures. Here is an example. struct A { int x; int y; }; struct B { double x; A a; }; (a) Declare a variable of type B and show how to initialize it. (b) Write the definition of a function bool isEqual(B b1, B b2); so that the function returns true when b1 and b2 contain the exact same values in all of their fields. (c) Rewrite the function from part (b) so that it has this declaration. bool isEqual(B* b1, B* b2); Problem 2.) We can create an array of structures. Complete the following for-loop so that it sets each c field to the maximum of it companion a and b fields. struct triple { int a; int b; int c; }; int main() { // Initialize only the a and b fields of each triple. triple ts[] = {{2, 5}, {7, 3}, {8, 7}, {1, 4}, {5, 5}}; // Set the c field of each triple to the max of its a and b. for (int i = 0; i < 5; ++i) { } return 0; } After you get the for-loop to work, modify the code so that the array ts is dynamically allocated. Problem 3.) A structure can contain an array. Complete the following code so that it sets the min and max fields to the minimum and maximum values from the array field. struct triple { int data[10]; int min; int max; }; int main() { // Initialize only the array in the triple. triple t = { {5, 4, 7, -1, 12, 0, -4, -7, 14, 9} }; // Compute the min and max fields of t. t.min = t.max = for (int i = 0; i < 10; ++i) { } return 0; } After you get the for-loop to work, modify the code so that the array ts is dynamically allocated. Problem 4.) Structures can contain pointers. Here is an example. Draw a picture of C++ memory after this code fragment executes. Your picture should look like the memory drawings made by the C++ Tutor, https://pythontutor.com/cpp.html struct A { int x; int y; }; struct B { double *x; A *a; }; int main() { double *x = new double(2.34); A a = {5, 6}; B b = {x, &a}; return 0; } Problem 5) Suppose I have defined a class called Thing. class Thing { public: Thing(){n = 5;} private: int n; }; Explain in detail what each of these three lines of code do. Don't be terse. Explain the individual parts of each line of code. Thing* rose1 = new Thing(); Thing rose2; Thing rose3 = Thing{}; Problem 6) Consider the following class definition. class Thing { public: void setX(int x) { this->x = x; } void setY(double y) { this->y = y; } int getX() { return x; } double getY() { return y; } int methodA() { return 2*x; } double methodB() { return 3*y; } private: int x = 0; double y = 0.0; }; Suppose that we have the following declarations. Thing a; Thing b; Explain why each of the following statements is either OK or not OK. b.x = 5; int u = a.getY(); double v = b.getX(); a.setX(3.2); b.getY(); double w = b.setY(4.5); int x = Thing.methodA(); int y = a.methodB(5); a = b.methodA(); a = b; Problem 7) Draw a detailed diagram for C++'s memory when the following code completes. Your diagram should be similar to the stack/heap memory diagrams made by the C++ Tutor web site. class Thing { public: Thing(int x){ n = x; } private: int n; }; class Box { public: Box(Thing* thing) { t = thing; } Thing getThing(){ return *t; } Thing* getThing_p(){ return t; } private: Thing* t; }; int main() { Thing t1{3}; Box b1(&t1); Box* b2 = new Box(&t1); Thing t2 = b2->getThing(); Box* b3 = new Box( new Thing(5) ); Box b4( b3->getThing_p() ); } Problem 8) In the Point class below, the translateHorizontalBy() method is intended to move the point left or right by the amount specified by the parameter dx. The method does not work as intended. Explain what is wrong and show how to correct it. class Point { public: Point(); Point(int x, int y); void translateHorizontalBy(int dx); int x; int y; }; void Point::translateHorizontalBy(int dx) { return x + dx; } Problem 9) In the following Rectangle class, implement the method inside(Point q) which should return true if the parameter Point is inside of the rectangle, and return false otherwise. class Point { public: Point(); Point(int x, int y); int x; int y; }; class Rectangle { public: Rectangle(Point p, int width, int height); bool inside(Point p); private: Point upper_left_corner; int width; int height; }; Point::Point() { x = 0; y = 0; } Point::Point(int x, int y) { this->x = x; this->y = y; } Rectangle::Rectangle(Point p, int width, int height) { upper_left_corner = p; this->width = width; this->height = height; } bool Rectangle::inside(Point q) { // your code here } main() { Rectangle r = {{1,1}, 2, 2}; bool b1 = r.inside( Point{0,0} ); // returns false bool b2 = r.inside( Point{2,0} ); // returns true } Problem 10) In the following Circle class, implement the method overlap(Circle c) which should return true if the parameter Circle overlaps (intersects) "this" Circle, and return false otherwise. Two circles overlap if the sum of their radii is greater than the distance between their centers. class Point { public: Point(); Point(int x, int y); int x; int y; }; class Circle { public: Circle(int r, Point p); bool overlap(Circle c); int r; Point p; }; Point::Point() { x = 0; y = 0; } Point::Point(int x, int y) { this->x = x; this->y = y; } Circle::Circle(int r, Point p) { this->r = r; this->p = p; } bool Circle::overlap(Circle c) { // your code here } main() { Circle c = {1, {1,1}}; bool b1 = c.overlap( Circle{1, {3,3}} ); // returns false bool b2 = c.overlap( Circle{1, {0,0}} ); // returns true }