CS 12400 (MW) Exam 2 Review The exam is on Wednesday, April 5. The exam is over Chapters 6 and 7 from the textbook, https://learn.zybooks.com/zybook/PurdueNorthwest-HammondCS124KraftSpring2023 Here are the sections that we covered from those two chapters. Sections 6.1, 6.3, 6.7, 6.9, 6.10, 6.11, 6.12, 6.13, 6.14. Sections 7.1, 7.2, 7.3, 7.4, 7.6, 7.7, 7.8, 7.9, 7.10, 7.11, 7.12. Here is a list of topics that we covered. 1) C++ arrays. 2) Array algorithms. 3) Arrays as function parameters. 4) Two-dimensional arrays. 5) Pointers and the "address of" operator. 6) Pointers vs. C++ references. 7) Pointers and arrays, a) pointer arithmetic, b) arrays decay to pointers, c) the "const" keyword (read-only arrays). 8) C and C++ strings. 9) Dynamic memory allocation, a) the "new" operator, b) dynamic arrays, c) the "delete" and "delete[]" operators, d) functions that return (dynamic) arrays. 10) Arrays of pointers. Here are a few review problems. 1.) Write a definition for a function that takes an integer array as a parameter (along with its size) and fills the array with -1's. 2.) Write a definition for a function that takes two integer arrays as parameters (along with their sizes) and copies the values of the shorter array into the longer array. The function should not assume that either the first or the second array parameter is the shorter one. If the arrays have equal size, copy the first array parameter into the second array parameter. 3.) Given the array declaration int my_array[4][4]; what element of the array does the following expression refer to? Briefly explain your answer. my_array[1][7]; 4.) Consider the following code fragment. int n = 5; int *p1 = &n; int a[] = {21, 22, 23}; int *p2 = &( a[1] ); int *p3; int **p4 = &p3; *p4 = a + 2; *p2 = -1; **p4 = -2; Draw a picture that represents the state of all the memory locations used by the code fragment when the fragment has finished executing. Your picture should look like the memory drawings made by the C++ Tutor, https://pythontutor.com/visualize.html 5.) Consider the following program segment. char a[] = "Mary had a little lamb"; char *p = a; int count = 0; while (*p == ' ') ++p; while (*p != '\0') { ++count; while (*p != ' ' && *p != '\0') ++p; while (*p == ' ') ++p; } (a) With the given value for a, what is the value of count at the end of the outer while-loop? For an arbitrary string stored in array a, what is the value of count at the end of the outer while-loop? (b) What is the purpose of the first inner while-loop? (c) What is the purpose of the second inner while-loop? (d) What is the purpose of the very first while-loop (the one just before the outer while-loop)? 6.) Suppose we want to write a function that takes as an input a one dimensional "partially filled array." Why would it be a good idea for the function to also have two other integer parameters, for example: int doSomething(int partiallyFilledArray[], int m, int n); 7.) It is easy to create an array in C++. int arrA[10]; Why do we need this more complicated way to create arrays? int *arrB = new int[10]; 8.) Write the definition of a function with the following declaration. char* reverse(const char str[]); The goal of this function is to return (a pointer to) a C string that contains the reverse of the C string contained in its parameter str. Your function definition will need to do four steps. First, compute the length of the C string str. Second, create a dynamic array, of the proper size, that will hold the reversed string. Third, copy the contents from str into the dynamic array in reverse order (make sure your new C string really is a C string). Finally, return a pointer to the dynamic array. Your function should explicitly write the code to do these steps without making use of, or calling any, library functions. 9.) Each of the following two for-loops has a problem. What is the problem for each loop? How might you fix each loop? int arr1[] = {5, 4, 3, 2, 1, 0}; const int * arr2 = arr1; for (int i = 0; i < 6; ++i) { arr2[i] += 10; } for (int * const p = arr1; p < arr1 + 6; ++p) { *p += 10; } 10.) Suppose you have a function declared as int doSomething(const int inputArray[]); and in the body of the function doSomething there is a line like this. int k = doSomeMore(inputArray[i]); What must be true about the declaration of the function doSomeMore in order for this line to compile? Briefly explain why. 11.) Given the following declarations, int i = 3; int *ip = new int(-3); int a[10]; what are the types of each of the following expressions? In other words, for each expression, if you needed to declare a variable that could hold the value of the expression, what type would the variable need to have? (a) i (b) &i (c) ip (d) *ip (e) &ip (f) a (g) ip[0] (h) ip + 3 12.) Consider the following code fragment. int *p1; int *p2; p1 = new int; p2 = new int; *p1 = 10; *p2 = 20; cout << *p1 << " " << *p2 << endl; *p1 = *p2; cout << *p1 << " " << *p2 << endl; *p1 = 30; cout << *p1 << " " << *p2 << endl; p1 = p2; // Draw a picture at this point. cout << *p1 << " " << *p2 << endl; *p1 = 40; cout << *p1 << " " << *p2 << endl; (a) What is the output produced by this code fragment? (b) For the line "p1 = p2;", draw a picture that represents at that point in the code fragment the state of all the memory locations used by the code fragment. Your picture should look like the memory drawings made by the C++ Tutor, https://pythontutor.com/cpp.html