CS 12400 (MW) Exam 1 Review The exam is on Wednesday, February 15. The exam is over Chapters 1 - 5 from the textbook, https://learn.zybooks.com/zybook/PurdueNorthwest-HammondCS124KraftSpring2023 The most important ideas from those chapters are in these sections. Sections 1.7, 1.9. Sections 2.2, 2.9, 2.14. Sections 3.3, 3.4, 3.13. Sections 4.6, 4.7, 4.11, 4.14, 4.17 Sections 5.3, 5.5, 5.13, 5.14, 5.15, 5.16, 5.17. Here are a few review problems. 1.) This problem is about function overloading. Suppose that a program declares the following two functions. It is not incorrect to have two functions like this, but it is not a good idea. theAnswer(int x, double y, double z); theAnswer(double x, double y, int z); (a) Which function definition would be used in the following function call? Briefly explain why. double w = theAnswer(3, 4, 5.0); (b) Which function definition would be used in the following function call? Briefly explain why. double w = theAnswer(3.0, 4, 5); (c) What problem can these two definitions lead to? 2.) Suppose the contents of a C++ program are as shown here. /* 1*/ #include /* 2*/ using namespace std; /* 3*/ /* 4*/ int main() /* 5*/ { /* 6*/ int x = f( 123 ); /* 7*/ cout << "The value of f(123) = " << x << endl; /* 8*/ return 0; /* 9*/ } /*10*/ /*11*/ int f(int y){ return y * y + y; } This program does not compile. (a) What is the mistake in this program? (b) Describe two ways to fix the problem. 3.) Java and C++ both have the equality operator ==. (a) In Java, if the variables x, y, and z are of type int, then the following expression will not compile. Explain why. x == y == z (b) In C++, if the variables x, y, and z are of type int, then the following expression will compile, but it does not tell you that x, y and z are all equal to each other. Explain why. x == y == z (c) In C++, if the variable x is of type int, then the following expression will compile, but it does not tell you if x is between 0 and 10. In fact, it always evaluates to true. Explain why. 0 <= x <= 10 4.) What would be the output from the following block of C++ code? Also, in each cout statement, draw an arrow from each variable in the statement to its declaration (that is, not to where the variable got its most recent value, but to where it was declared). { int x = 1; int y = 11; cout << x << " " << y << endl; { y = 12; cout << x << " " << y << endl; int x = 2; cout << x << " " << y << endl; { int x = 3; cout << x << " " << y << endl; int y = 13; } cout << x << " " << y << endl; } cout << x << " " << y << endl; } 5.) The following two code fragments are almost, but not quite, equivalent. (By "equivalent" I mean two code fragments that could replace each other in any program and the program would act exactly the same.) int n = 10; int n = 10; int i = 0; for (int i = 0; i <= n; ++i) while (i <= n) { { cout << i << endl; cout << i << endl; } ++i; } (a) In what way do the two fragments differ? (Hint: Variable scope.) (b) Modify the for-loop fragment so that it is equivalent to the while-loop fragment. (c) Modify the while-loop fragment so that it is equivalent to the (original) for-loop fragment. 6.) What are the differences and similarities between a call-by-value parameter and a call-by-const-reference parameter? Function declarations that illustrate these follow. void call_by_value(int x); void call_by_const_reference(const int& x) 7.) Write a fragment of C++ code that will generate a random integer between 4 and 12 (inclusive) with each integer from 4 to 12 being equally likely. 8.) Write a fragment of C++ code that will generate a random integer between -100 and 200 (inclusive) with each integer from -100 to 200 being equally likely. 9.) Write a fragment of C++ code that will generate a random double between -2.0 and 2.0. 10.) What is the biggest difference between how C++'s cin works and how Java's Scanner works when reading in numbers? 11.) Suppose the following C++ program is given the line of input shown below. What does the program print out. Briefly explain why. /* 1*/ #include /* 2*/ using namespace std; /* 3*/ /* 4*/ int main() /* 5*/ { /* 6*/ int x; /* 7*/ double y; /* 8*/ int z1, z2; /* 9*/ cin >> x; /*10*/ cin >> y; /*11*/ cin >> z1; /*12*/ cin >> z2; /*13*/ cout << x << endl << y << endl << z1 << endl << z2 << endl; /*14*/ return 0; /*15*/ } Input: 12.34 56x78 Remember that you can use this zyDE to test code that needs input. https://learn.zybooks.com/zybook/PurdueNorthwest-HammondCS124KraftSpring2023/chapter/2/section/9?content_resource_id=76657531 12.) (a) What is the purpose of the fail() method on cin? That is, what does the expression cin.fail() return? When and why do you need it? (b) What is the purpose of the clear() method on cin? That is, what does the line of code cin.clear(); do? When and why do you need it? 13.) The following program does not compile. Show three ways to fix it. /*1*/ #include /*2*/ /*3*/ /*4*/ int main() /*5*/ { /*6*/ cout << "Hello."; /*7*/ return 0; /*8*/ } 14.) What does the following recursive function compute? That is, how is the string returned by the function related to the input string? Hint: Hand trace the function with a simple input string, like "cats". What would be an appropriate name for this function? string mystery(string str) { if ( str == "" ) // base case { return ""; } else // recursive case { return mystery( str.substr(1) ) + str.substr(0, 1); } } 15.) What does the following recursive function compute? That is, how is the integer returned by the function related to the input string? Hint: Hand trace the function with an input string like "elephants". What would be an appropriate name for this function? int mystery(string str) { if ( str == "" ) // base case { return 0; } else // recursive case { return 1 + mystery( str.substr(1) ); } } 16.) What does the following recursive function compute? That is, how is the string returned by the function related to the input string? Hint: Hand trace the function with an input string like "Hello and how are you" (spaces are important to this function). string mystery(string str) { if ( str == "" ) // base case { return ""; } else if ( str.substr(0, 1) == " " ) { return mystery( str.substr(1) ); // recursion } else { return str.substr(0, 1) + mystery( str.substr(1) ); // recursion } } 17.) What does the following recursive function compute? That is, how is the boolean returned by the function related to the input string? Hint: Hand trace the function with an input string like "dogs". bool mystery(string str) { if ( str == "" ) // a base case { return false; } else if ( str == "s" ) // another base case { return true; } else // recursive case { return mystery( str.substr(1) ); } } (Done.)