Problem 12
What is the output of the following C++ code? int *p = new int; int *q = new int; *p = 26; *q = 10; cout << 2 * (*p) << " " << (*q + 3) << endl; p = q; *p = 42; cout << *p << " " << *q << endl; q = new int; *p = 25; *q = 18; cout << *p << " " << *q << endl;
Problem 13
What is the output of the following C++ code? (Assume that decimal numbers are output with two decimal places.) double *test1 = new double; double *test2 = new double; double *average; average = test1; *test1 = 45.00; *test2 = 90.00; test1 = test2; test2 = new double; *test2 = 86.00; *average = ((*test1) + (*test2)) / 2; cout << *test1 << " " << *test2 << " " << *average << endl;
Problem 14
What is wrong with the following C++ code? double *deposit; //Line 1 double *intRate; //Line 2 double interest; //Line 3 deposit = new double; //Line 4 *deposit = 25000; //Line 5 interest = (*deposit) * (*intRate); //Line 6 cout << interest << endl; //Line 7
Problem 15
What is wrong with the following C++ code? double *firstPtr = new double; //Line 1 double *nextPtr = new double; //Line 2 *firstPtr = 62; //Line 3 nextPtr = firstPtr; //Line 4 delete firstPtr; //Line 5 delete nextPtr; //Line 6 firstPtr = new double; //Line 7 *firstPtr = 28; //Line 8
Problem 16
What is the output of the following C++ code? int *p; int *q = new int; p = q; *q = 75; delete p; p = new int; *p = 62; q = new int; q = p; *q = 26; cout << *p << " " << *q << endl;
Problem 17
What is stored in list after the following code executes? int list[7] = {10, 8, 15, 14, 16, 24, 36}; int *ptr = list; *ptr = *ptr + 2; ptr = ptr + 2; *ptr = (*ptr) – *(ptr – 1); ptr++; *ptr = 2 * (*ptr) – 3;
Problem 18
What is the output of the following C++ code? int num; int *listPtr; int *temp; listPtr = new int[5]; num = 8; temp = listPtr; for (int j = 0; j < 5; j++) { *listPtr = num; num = num + 2; listPtr++; } listPtr = temp; for (int k = 0; k < 5; k++) { *temp = *temp + 3; temp++; } for (int k = 0; k < 5; k++) { cout << *listPtr << " "; listPtr ++; } cout << endl;
Problem 19
Suppose that numPtr is a pointer of type int and gpaPtr is a pointer of type double. Further suppose that numPtr = 1050 and gpaPtr = 2000. Also suppose that the size of the memory allocated for an int value is 4 bytes and the size of the memory allocated for a double value is 8 bytes. What are the values of numPtr and gpaPtr after the statements numPtr = numPtr + 2; and gpaPtr = gpaPtr + 3; execute?
Problem 22
What is the output of the following C++ code? int *tempList; int num = 3; tempList = new int[7]; tempList[6] = 4; for (int j = 5; j >= 0; j--) tempList[j] = tempList[j + 1] + j * num; for (int j = 0; j < 7; j++) cout << tempList [j] << " "; cout << endl;
Problem 23
Consider the following statement: int *num; a. Write the C++ statement that dynamically creates an array of 10 components of type int and num contains the base address of the array. b. Write a C++ code that inputs data into the array num from the standard input device. c. Write a C++ statement that deallocates the memory space of array to which num points.