In: Computer Science
Declare two pointers p and q capable of pointing to int
Make p point to y
Make q point to x
Assign 3 and 6 to x and y without mentioning x and y
Redirect p to point to the same location q points to (two ways)
Make q point to y
Swap the values of x and y without mentioning x and y
Declare two pointers p and q capable of pointing to int
Make p point to x
Make q point to y
Assign 3 to x and the same value plus one to y without mentioning y
Swap p and q
Make q point to y
Swap the values of p and q point to.
3. Declare three int: x,y and z
Declare two pointers p,q and r capable of pointing to int
Make p point to y.
Make q point to x.
Make r point to z.
Assign 5, 10 and 20 to x, y and z respectively without mentioning x ,y and z
Swap the values of x,y and z without mentioning x , y and z in the following way: x gets the value of z, y gets the previous value of x and z gets the previous value of y.
Swap their values.
5. Please, write a loop to print an array a with pointers. You can use a variable “size” for array size
6. Please, write a loop to print an array a with pointers backwards. You can use a variable “size” for array size
Since it's no mentioned here that whether we have to write an algo or in some language so I am writing in C++. You can use the code accordingly.
FIRST:
#include <iostream>
using namespace std;
int main()
{
//Declaring two variables
int x, y;
int *p, *q; //declaring 2 pointers
p = &y; //p pointing to y
q = &x; //q pointing to x;
//assigning 3 and 6 to x and y without mentioning x an y
*q = 3;
*p = 6;
//redirect p to point to same location q points to
p = q; //first way
p = &x; //second way
q = &y; //now q points to y
//swapping the value of x and y without mentioning x and y
int temp;
temp = *p;
*p = *q;
*q = temp;
return 0;
}
Second :
#include <iostream>
using namespace std;
int main()
{
//Declaring two variables
int x, y;
int *p, *q; //declaring 2 pointers
p = &x; //p pointing to x
q = &y; //q pointing to y
//assigning 3 to x and same value + 1 without mentioning y
x = 3;
*q = x + 1;
//swap p and q
int *temp;
temp = p;
p = q;
q = temp;
q = &y; //q pointing to y
//swap the values of p and q
int t;
t = *p;
*p = *q;
*q = t;
return 0;
}
Third:
#include <iostream>
using namespace std;
int main()
{
//Declaring three variables
int x, y, z;
int *p, *q, *r; //declaring 3 pointers
p = &y; //p pointing to y
q = &x; //q pointing to x
r = &z; //r pointing to sizeof
//assigning 5,10 and 20 to x, y and z resp without mentioning x, y
and z
*q = 5;
*p = 10;
*r = 20;
//swap values of x, y and z
int temp;
temp = *q;
*q = *r; //x gets the value of z
*r = *p; //z gets the previous value of y
*p = temp; //y gets the previous value of x
return 0;
}
FOURTH:
#include <iostream>
using namespace std;
int main()
{
//dyamically allocate members for two variables
int *a = new int(10);
int *b = new int(20);
//swap their values
temp = *a;
*a = *b;
*b = temp;
return 0;
}
FIFTH:
#include <iostream>
using namespace std;
int main()
{
int ptr1[5]; // integer array declaration
int *ptr2[5];// integer array of pointer declaration
int size = sizeof(ptr1)/sizeof(ptr1[0]);
for(int i=0;i<size;i++)
{
cin >> ptr1[i];
ptr2[i]=&ptr1[i];
}
//PRINTING VALUES
for(int i=0;i<size;i++)
{
cout << *ptr2[i] << endl;
}
}
SIXTH:
#include <iostream>
using namespace std;
int main()
{
int ptr1[5]; // integer array declaration
int *ptr2[5];// integer array of pointer declaration
int size = sizeof(ptr1)/sizeof(ptr1[0]);
for(int i=0;i<size;i++)
{
cin >> ptr1[i];
ptr2[i]=&ptr1[i];
}
//PRINTING VALUES
for(int i=size - 1;i >= 0;i--)
{
cout << *ptr2[i] << endl;
}
}