Create a new project named lab6_1. Read the steps for each portion of this lab. Diagram the memory as outlined in the steps (don’t need to turn this part in). Then, write the code to verify your written work. Declare int variables x and y and int* pointer variables p and q.  Set x to 2, y to 8, p to the address of x, and q to the address of y.  Then print the following information: The address of x (use the address operator) and the value of x. For example, that output statement may look like:  cout << "&x = " << &x << ", x = " << x << endl; The value of p and the value of *p. The address of y (use address operator) and the value of y. The value of q and the value of *q. The address of p (not its contents, don’t dereference). The address of q (not its contents, don't dereference). How are the addresses of x, y, p, and q related? List them out and find the distances between each address. You can include these answers in your program as a block comment. Comment out the previous code. Now, declare int variables x, y, temp and int* pointer variables p, q, temp_ptr.  Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. Print the message: Swapping values. Then, use the following swap code: temp = x; x = y; y = temp; Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment. Comment out the previous code. Declare int variables x, y, temp and int* pointer variables p, q, temp_ptr.  Set x, y, temp to three distinct values.  Set p, q, temp_ptr to the addresses of x, y, temp respectively. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. Print the message: Swapping pointers. Then, use the following swap code:  temp_ptr = p; p = q; q = temp_ptr; Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter10: Pointers
Section10.3: Pointer Arithmetic
Problem 5E
icon
Related questions
Question
  1. Create a new project named lab6_1. Read the steps for each portion of this lab. Diagram the memory as outlined in the steps (don’t need to turn this part in). Then, write the code to verify your written work.
    1. Declare int variables x and y and int* pointer variables p and q.  Set x to 2, y to 8, p to the address of x, and q to the address of y.  Then print the following information:

      1. The address of x (use the address operator) and the value of x.

        For example, that output statement may look like: 

        cout << "&x = " << &x << ", x = " << x << endl;
      2. The value of p and the value of *p.

      3. The address of y (use address operator) and the value of y.

      4. The value of q and the value of *q.

      5. The address of p (not its contents, don’t dereference).

      6. The address of q (not its contents, don't dereference).
      7. How are the addresses of x, y, p, and q related? List them out and find the distances between each address. You can include these answers in your program as a block comment.

    2. Comment out the previous code. Now, declare int variables x, y, temp and int* pointer variables p, q, temp_ptr.  Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively.

      1. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
      2. Print the message: Swapping values.

      3. Then, use the following swap code:
        1. temp = x;
        2. x = y;
        3. y = temp;
      4. Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
      5. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.
    3. Comment out the previous code. Declare int variables x, y, temp and int* pointer variables p, q, temp_ptr.  Set x, y, temp to three distinct values.  Set p, q, temp_ptr to the addresses of x, y, temp respectively.
      1. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
      2. Print the message: Swapping pointers.

      3. Then, use the following swap code: 

        1. temp_ptr = p;
        2. p = q;
        3. q = temp_ptr;
      4. Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.

      5. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Software Development Approaches
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr