Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 11

What is the output of the following C++ code? int *p; int *q; p = new int; q = new int; *p = 27; *q = 35; cout << *p << " " << *q << endl; *q = *p; *p = 73; cout << *p << " " << *q << endl; p = new int; *p = 36; q = p; cout << *p << " " << *q << endl;

Problem 12

What is wrong with the following code? int *p; //Line 1 int *q; //Line 2 p = new int; //Line 3 *p = 43; //Line 4 q = p; //Line 5 *q = 52; //Line 6 delete q; //Line 7 cout << *p << " " << *q << endl; //Line 8

Problem 13

What is the output of the following code? int x; int *p; int *q; p = new int[10]; q = p; *p = 4; for (int j = 0; j < 10; j++) { x = *p; p++; *p = x + j; } for (int k = 0; k < 10; k++) { cout << *q << " "; q++; } cout << endl;

Problem 16

What is the output of the following code? int *secret; int j; secret = new int[10]; secret[0] = 10; for (j = 1; j < 10; j++) secret[j] = secret[j - 1] + 5; for (j = 0; j < 10; j++) cout << secret[j] << " "; cout << endl;

Problem 17

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.

Problem 18

Consider the following C++ code: int *p; p = new int[10]; for (int j = 0; j < 10; j++) p[i] = 2 * j - 2; Write the C++ statement that deallocates the memory space occupied by the array to which p points.

Problem 19

Explain the difference between a shallow copy and a deep copy of data.

Problem 20

What is wrong with the following code? int *p; //Line 1 int *q; //Line 2 p = new int[5]; //Line 3 *p = 2; //Line 4 for (int i = 1; i < 5; i++) //Line 5 p[i] = p[i - 1] + i; //Line 6 q = p; //Line 7 delete [] p; //Line 8 for (int j = 0; j < 5; j++) //Line 9 cout << q[j] << " "; //Line 10 cout << endl; //Line 11

Problem 21

What is the output of the following code? int *p; int *q; int i; p = new int[5]; p[0] = 5; for (i = 1; i < 5; i++) p[i] = p[i - 1] + 2 * i; cout << "Array p: "; for (i = 0; i < 5; i++) cout << p[i] << " "; cout << endl; q = new int[5]; for (i = 0; i < 5; i++) q[i] = p[4 - i]; cout << "Array q: "; for (i = 0; i < 5; i++) cout << q[i] << " "; cout << endl;

Problem 22

a. Write a statement that declares sales to be a pointer to a pointer of type double. b. Write a C++ code that dynamically creates a two-dimensional array of five rows and seven columns and sales contains the base address of that array. c. Write a C++ code that inputs data from the standard input device into the array sales. d. Write a C++ code that deallocates the memory space occupied by the two- dimensional array to which sales points.

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks