Problem 2
State whether the following are true or false. If the answer is false, explain why. a. The address operator & can be applied only to constants and to expressions. b. A pointer that is declared to be of type void * can be dereferenced. c. Pointers of different types can never be assigned to one another without a cast operation.
Problem 3
For each of the following, write C++ statements that perform the specified task. Assume that double-precision, floating-point numbers are stored in eight bytes and that the starting address of the array is at location 1002500 in memory. Each part of the exercise should use the results of previous parts where appropriate. a. Declare an array of type double called numbers with 10 elements, and initialize the elements to the values 0.0, 1.1, 2.2, ..., 9.9. Assume that the symbolic constant SIZE has been defined as 10. b. Declare a pointer nPtr that points to a variable of type double. c. Use a for statement to print the elements of array numbers using array subscript notation. Print each number with one position of precision to the right of the decimal point. d. Write two separate statements that each assign the starting address of array numbers to the pointer variable nPtr. e. Use a for statement to print the elements of array numbers using pointer/offset notation with pointer nPtr. f. Use a for statement to print the elements of array numbers using pointer/offset notation with the array name as the pointer. g. Use a for statement to print the elements of array numbers using pointer/subscript notation with pointer nPtr. h. Refer to the fourth element of array numbers using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr. i. Assuming that nPtr points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location? j. Assuming that nPtr points to numbers[ 5 ], what address is referenced by nPtr after nPtr -= 4 is executed? What is the value stored at that location?
Problem 5
Perform the task specified by each of the following statements: a. Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer. d. Write the function prototype for the function in part (c). e. Write two statements that each initialize character array vowel with the string of vowels, "AEIOU".
Problem 7
$$\begin{array}{l} \text { char } \operatorname{si}[50]=" j a c k" \\ \text { char } \operatorname{s2}[5 \theta]=" j i i l^{\prime \prime} \\ \text { char } s 3[50] ; \end{array}$$ What (if anything) prints when each of the following statements is performed? If the statement contains an error, describe the error and indicate how to correct it. Assume the following variable declarations: a. cout \(<<\) strcpy \((s 3, s 2)<<\) end \(l\) \(\mathbf{b}\) cout \(<<\) straat \(\left(\text { strcat }\left(\text { strcpy }(\mathrm{s} 3, \mathrm{s} 1),^{\prime \prime} \text { and }^{\prime \prime}\right), \mathrm{s} 2\right)\) \(<<\) end 1 \(\mathbf{c}\) cout \(<<\operatorname{str} \operatorname{len}(\mathrm{s} 1)+\operatorname{str} \operatorname{len}(\mathrm{s} 2)<<\mathrm{end} \mathrm{l}\) d. cout \(<<\operatorname{str} \operatorname{len}(\mathrm{s} 3)<<\) end 1
Problem 8
State whether the following are true or false. If false, explain why. a. Two pointers that point to different arrays cannot be compared meaningfully. b. Because the name of an array is a pointer to the first element of the array, array names can be manipulated in precisely the same manner as pointers.
Problem 9
For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory. a. Declare an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5. b. Declare a pointer vPtr that points to an object of type unsigned int. c. Use a for statement to print the elements of array values using array subscript notation. d. Write two separate statements that assign the starting address of array values to pointer variable vPtr. e. Use a for statement to print the elements of array values using pointer/offset notation. f. Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer. g. Use a for statement to print the elements of array values by subscripting the pointer to the array. h. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation. i. What address is referenced by vPtr + 3? What value is stored at that location? j. Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location
Problem 10
For each of the following, write a single statement that performs the specified task. Assume that long integer variables value1 and value2 have been declared and value1 has been initialized to 200000. a. Declare the variable longPtr to be a pointer to an object of type long. b. Assign the address of variable value1 to pointer variable longPtr. c. Print the value of the object pointed to by longPtr. d. Assign the value of the object pointed to by longPtr to variable value2. e. Print the value of value2. f. Print the address of value1. g. Print the address stored in longPtr. Is the value printed the same as value1's address
Problem 11
Perform the task specified by each of the following statements: a. Write the function header for function zero that takes a long integer array parameter bigIntegers and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for function add1AndSum that takes an integer array parameter oneTooSmall and returns an integer. d. Write the function prototype for the function described in part (c)
Problem 24
(Quicksort) You have previously seen the sorting techniques of the bucket sort and selection sort. We now present the recursive sorting technique called Quicksort. The basic algorithm for a single-subscripted array of values is as follows: a. Partitioning Step: Take the first element of the unsorted array and determine its final location in the sorted array (i.e., all values to the left of the element in the array are less than the element, and all values to the right of the element in the array are greater than the element). We now have one element in its proper location and two unsorted subarrays. b. Recursive Step: Perform Step 1 on each unsorted subarray. Each time Step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are created. When a subarray consists of one element, that subarray must be sorted; therefore, that element is in its final location. The basic algorithm seems simple enough, but how do we determine the final position of the first element of each subarray? As an example, consider the following set of values (the element in bold is the partitioning elementit will be placed in its final location in the sorted array): 37 2 6 4 89 8 10 12 68 45 a. Starting from the rightmost element of the array, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 12, so 37 and 12 are swapped. The values now reside in the array as follows: 12 2 6 4 89 8 10 37 68 45 Starting from the left of the array, but beginning with the element after 12, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. The first element greater than 37 is 89, so 37 and 89 are swapped. The values now reside in the array as follows: 12 2 6 4 37 8 10 89 68 45 Starting from the right, but beginning with the element before 89, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 10, so 37 and 10 are swapped. The values now reside in the array as follows: 12 2 6 4 10 8 37 89 68 45 Starting from the left, but beginning with the element after 10, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. There are no more elements greater than 37, so when we compare 37 with itself, we know that 37 has been placed in its final location of the sorted array. Once the partition has been applied to the array, there are two unsorted subarrays. The subarray with values less than 37 contains 12, 2, 6, 4, 10 and 8. The subarray with values greater than 37 contains 89, 68 and 45. The sort continues with both subarrays being partitioned in the same manner as the original array. Based on the preceding discussion, write recursive function quickSort to sort a single subscripted integer array. The function should receive as arguments an integer array, a starting subscript and an ending subscript. Function partition should be called by quickSort to perform the partitioning step
Problem 30
Write a program that uses function strcmp to compare two strings input by the user. The program should state whether the first string is less than, equal to or greater than the second string