Write in C++ please. I\'m using a VirtualBox machine running linux using Geany a
ID: 3809606 • Letter: W
Question
Write in C++ please. I'm using a VirtualBox machine running linux using Geany as an editor.
The goal of this simple assignment is to get a better understanding of: (a) the difference between a variable's address and its value, and (b) pointer variables, introduced in Section 9.1 of the textbook. Write a C++ program, and declare in it three integer variables x, y, and z. Also, declare three integer-pointer variables p, q, and r. Make your program perform the following tasks: 1. Display the prompt "Enter 2 integers: ", and read input values into x and y. 2. Set p and q to the addresses of x and y, respectively. 3. Print the following, with appropriate labels: a) The address of x, and the value of x. b) The address of y, and the value of y. c) The address of p, the value of p, and the value of *p. d) The address of q, the value of q, and the value of *q. 4. Display the message "Swapping integers ". 5. Execute the swap code: z = x; x = y; y = z; 6. Print the following, with appropriate labels: a) The address of x, and the value of x. b) The address of y, and the value of y. c) The address of p, the value of p, and the value of *p. d) The address of q, the value of q, and the value of *q. 7. Display the message "Swapping pointers ". 8. Execute the swap code: r = p; p = q; q = r; 9. Print the following, with appropriate labels: a) The address of x, and the value of x. b) The address of y, and the value of y. c) The address of p, the value of p, and the value of *p. d) The address of q, the value of q, and the value of *q.Explanation / Answer
Hi,
Please find below the code-
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
int *p,*q,*r;
cout << "Enter two Integers" << endl;
cin>>x>>y;
p=&x;
q=&y;
cout << "Address of x " << &x<<endl;
cout << "Value of x " << x<<endl;
cout << "Address of y " << &y<<endl;
cout << "Value of x " << y<<endl;
cout << "Address of p " << &p<<endl;
cout << "Value of p " << p<<endl;
cout << "Value of *p " << *p<<endl;
cout << "Address of q " << &q<<endl;
cout << "Value of q " << q<<endl;
cout << "Value of *q " << *q<<endl;
cout << "Swapping Integers ";
z=x;
x=y;
y=z;
cout << "Address of x " << &x<<endl;
cout << "Value of x " << x<<endl;
cout << "Address of y " << &y<<endl;
cout << "Value of x " << y<<endl;
cout << "Address of p " << &p<<endl;
cout << "Value of p " << p<<endl;
cout << "Value of *p " << *p<<endl;
cout << "Address of q " << &q<<endl;
cout << "Value of q " << q<<endl;
cout << "Value of *q " << *q<<endl;
cout << "Swapping Pointers ";
r=p;
p=q;
q=r;
cout << "Address of x " << &x<<endl;
cout << "Value of x " << x<<endl;
cout << "Address of y " << &y<<endl;
cout << "Value of x " << y<<endl;
cout << "Address of p " << &p<<endl;
cout << "Value of p " << p<<endl;
cout << "Value of *p " << *p<<endl;
cout << "Address of q " << &q<<endl;
cout << "Value of q " << q<<endl;
cout << "Value of *q " << *q<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.