Exercise 1: Declare 2 int variables x and y and 2 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:
Exercise 2: Declare 3 int
variables x, y,
z and 3 int*
pointer variables p, q,
r. Set x,
y, z
to three distinct values. Set p,
q, r
to the addresses of x, y,
z respectively.
/*
// Author:Zachary //
// //
// Date: 11/10/14 23:34 //
// //
// Input: None //
*/
#include<iostream>
#include <algorithm> // std::swap
using namespace std;
void Exone ()
//Exercise 1: Declare 2 int variables x and y and 2 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://
{
int x,y;
int *p,*q;
x=2;
y=8;
p=&x;
q=&y;
cout<<"--------------------Exercise 1:------------------"<<endl;
cout<<"-------------------------------------------------"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"The address of x is "<<&x<<" and the value of x is "<<x<<endl;//(1) The address of x and the value of x.
cout<<"The value of p is "<<p<<" and the value of *p is "<<*p<<endl;//(2) The value of p and the value of *p.
cout<<"The address of y is "<<&y<<" and the value of y is "<<y<<endl;//(3) The address of y and the value of y.
cout<<"The value of q is "<<q<<" and the value of *q "<<*q<<endl;//(4) The value of q and the value of *q.
cout<<"The address of p (not its contents!) is "<<&p<<endl;//(5) The address of p (not its contents!).
cout<<"The address of q (not its contents!) is "<<&q<<endl;//(6) The address of q (not its contents!).
}
void Extwo()
//Exercise 2: Declare 3 int variables x, y, z and 3 int* pointer variables p, q, r. //
//Set x, y, z to three distinct values. Set p, q, r to the addresses of x, y, z respectively.//
{
{
int x,y,z;
int *p,*q,*r;
x=1;
y=2;
z=3;
p=&x;
q=&y;
r=&z;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"--------------------Exercise 2:------------------"<<endl;
cout<<"-------------------------------------------------"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
//(1) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
cout<<"value of x is "<<x<<endl;
cout<<"value of y is "<<y<<endl;
cout<<"value of z is "<<z<<endl;
cout<<"value of p is "<<p<<endl;
cout<<"value of q is "<<q<<endl;
cout<<"value of r is "<<r<<endl;
cout<<"value of *p is "<<*p<<endl;
cout<<"value of *q is "<<*q<<endl;
cout<<"value of *r is "<<*r<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
//(2) Print the message: Swapping values.
cout<<"!!!!!!!SWAPPING VALUES!!!!!!"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
//(3) Execute the swap code: z = x; x = y; y = z;
std::swap(z,x);
std::swap(x,y);
std::swap(y,z);
//(4) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
cout<<"value of x is "<<x<<endl;
cout<<"value of y is "<<y<<endl;
cout<<"value of z is "<<z<<endl;
cout<<"value of p is "<<p<<endl;
cout<<"value of q is "<<q<<endl;
cout<<"value of r is "<<r<<endl;
cout<<"value of *p is "<<*p<<endl;
cout<<"value of *q is "<<*q<<endl;
cout<<"value of *r is "<<*r<<endl;
}
}
int main()
{
Exone();
Extwo();
return 0;
}//end main