I'm working on posting some exam practice questions here. Michael 11:22am, Wednesday ------------------------------------------------------------------ Maps: M0. Write a clear English description of how maps are used in C++. M1. Write a C++ declaration that declares a map where the indexes are strings and the values are double numbers. M2. Put the values 10.1, 12.3 and 5.2 into your map with the keys "Moe", "Larry", and "Curly" . M3. Declare an iterator for your map. M4. Write a for-loop that steps through your map and prints each index/value combination. ------------------------------------------------------------------ ------------------------------------------------------------------ File I/O F0. Write a clear English description of how ifstream and ofstream variables are used in C++. F1. Write C++ code that will open an output file called numbers.txt and write the numbers 0 to 10000 into the file with one number per line. F2. Write C++ code that will open an input file called number.txt. The first line of the file is a number, n, that tells how many subsequent lines are in the file. Each of the other n lines contains a doulbe number. Your code should read those n numbers and then print (to cout) the value of the largest number that was read. F3. Tell me one way that a program can check to see whether a file was successfully opened. F4. How does a program close a file when the file is no longer needed? ------------------------------------------------------------------ Functions ------------------------------------------------------------------ F0. Consider your Bird class from Homework 4. Write a function prototype for a function that has three parameters: a single Bird, an array of Birds, and an int. F1. Write a function that has two parameters: an array of int called x and a number n that tells how many elements are in x. Here are some possible things that I could ask your function to do: a. Reverse the elements in x b. Interchange x[0] with the smallest element of x, leaving all other elements unchanged. c. Return the sum of the ints in x. d. Return the average of the ints in x. e. Return the smallest number in x. F2. Write a function that has one parameter that is a map. The function prints each of the string/int pairs that are currently in the map. F2. Write a function that has one parameter that is a map. The return value of the function is the number of entries that have 0 for the int part. F3. Explain the difference between a value parameter and a reference parameter. Use an explicit example to clarify your explanation. ------------------------------------------------------------------ Homework ------------------------------------------------------------------ H1. Write a small piece of code that waits for a WM_LBUTTONDOWN to occur, and then gets the coordinates of the click and prints those coordinates to the screen. H2. Give some examples of the kind of command string that can be sent to mciSendString. ------------------------------------------------------------------ Review Questions from the Previous Exam ------------------------------------------------------------------ The first few questions involve these functions, one of which has a value parameter and the other has a reference parameter: int quiz(int number) { number = number + 1; return 3*number; } int foo(int& number) { number = number + 1; return 3*number; } R1. What will this main program print? int main( ) { int a = 1; int b = 2; a = quiz(b); cout << a; return EXIT_SUCCESS; } Answer: 9 R2. What will this main program print? int main( ) { int a = 1; int b = 2; a = quiz(b); cout << b; return EXIT_SUCCESS; } Answer: 2 R3. What will this main program print? int main( ) { int a = 1; int number = 1; a = quiz(number); cout << number; return EXIT_SUCCESS; } Answer: 1 R3. What will this main program print? int main( ) { int a = 1; int number = 1; a = quiz(number); cout << number; return EXIT_SUCCESS; } Answer: 1 R4. What will this main program print? int main( ) { int a = 1; int number = 1; a = foo(number); cout << number; return EXIT_SUCCESS; } Answer: 2 R5. Here is a function prototype and a main program that calls the function. Notice that the variables in the main program have the same names as the function's parameters, but they appear in a different order: void test(int x, int y); int main( ) { int x = 1; int y = 2; test(y, x); ... } When the function is called which parameter is initialized from x? Which from y? Answer: x (in test) gets its initial value from y (in main), and y (in test) gets its initial value from x (in main). R6. Consider the following function definition: void bashful(int count) { if (count > 5) return; cout << "Hello." << endl; } What is the output from bashful(5) and bashful(6)? Answer: bashful(5) prings "Hello.", but bashful(6) prints nothing. R7. Consider this definition: int data[90]; What are the first and final values of data? Answer: data[0] and data[89]. R8. What type of parameter is always treated as a reference parameter, even if it has no & after the data type? Answer: An array. R9. What is the major purpose of double buffering? Answer: To prevent the eye from detecting flickering. R10.Suppose that I have defined a function with this prototype: bool has_target(int data[], int n, int target); The function returns true if the target appears at least once in the first n elements of data. Also suppose that s and t are both arrays with 100 elements. Write a function call that will return true if and only if the final element of t appears somewhere in s. Answer: has_target(s, 100, t[99]) R11.Suppose that a program contains a Boolean expression (x.d[3] == 10). What can you tell me about the data type of x? Answer: It is a struct of some sort that has a member variable d which is an array of some kind of element that can be compared to integers. R12.Write a complete function implementation for a function that has no parameters and returns an int. The first time the function is called, it's return value is 1; the second time the function is called, it's return value is 2; the Nth time the function is called, it's return value is N. Do not use any global variables. Answer: (Using a static variable) int f() { static int answer = 0; ++answer; return answer; } R13.Which of these expressions could not be a valid arithmetic expression using only the parts of C++ that we have learned? A. x[x.y] B. x[y.x] C. x[y.x].z D. x[y.x].z[4] Answer: A is not valid because it would require x to be both an array and a struct. R14.Describe what happens if you try to write to data[100] when the data array has only 90 elements. Answer: At run time, the program will access the memory where data[100] would have been had the array been larger. This could cause the program to crash right away, to crash later on, or to corrupt other data in the program. R15.Implement a function that has two parameters: an array x of integers and an int value n that tells how many elements are in x. The function returns true if the array contains two elements that are next to each other and equal. Answer: bool same(int x[], int n) { int i; for (i = 1; i < n; ++i) { if (x[i-1] == x[i]) return true; } return false; } R16.Write a struct called Concrete that has two doulbe member variables and one bool member variable. Answer: struct Concrete { double cement_percent; double drying_time; bool approved; } R17.Declare an array of 100 Concrete objects. Concrete data[100]; R18.Write a function that can be used to set every element in a Concrete array to have the values 10,180,false. Answer: void set(Concrete data[], int size) { int i; for (i=0; i