Question

In: Computer Science

Declare two int: x and y Declare two pointers p and q capable of pointing to...

  1. Declare two int: x and y

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

  1. Declare two int: 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.

  1. Dynamically allocate memory for two integers.

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

Solutions

Expert Solution

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;
}
}


Related Solutions

Declare two int: x and y Declare two pointers p and q capable of pointing to...
Declare two int: x and y 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 int: x and y Declare two pointers p...
Consider the homogeneous second order equation y′′+p(x)y′+q(x)y=0. Using the Wronskian, find functions p(x) and q(x) such...
Consider the homogeneous second order equation y′′+p(x)y′+q(x)y=0. Using the Wronskian, find functions p(x) and q(x) such that the differential equation has solutions sinx and 1+cosx. Finally, find a homogeneous third order differential equation with constant coefficients where sinx and 1+cosx are solutions.
Verify the green theorem for: P (x, y) = 2xy Q (x, y) = x +...
Verify the green theorem for: P (x, y) = 2xy Q (x, y) = x + y C is the curve formed by the line segments from (0,0) to (3,0), from (3,0) to (2,1) and from (2,1) to (0,0) For the line integral, you can solve through the parameterization analysis please
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
Given the function u(p,q,r)=((p-q)/(q-r)), with p=x+y+z,q=x-y+z, and r=x+y-z, find the partial derivatives au/ax=, au/ay=, au/az=
Given the function u(p,q,r)=((p-q)/(q-r)), with p=x+y+z,q=x-y+z, and r=x+y-z, find the partial derivatives au/ax=, au/ay=, au/az=
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations....
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations. i) If we know a solution y = φ(x) of this equation, then any other solution can be written in the form y(x) = φ(x)+ 1/v(x), where v(x) is an unknown function which satisfies a certain linear equation. Using the fact that φ and y both solve the above Riccati equation, find the differential equation that v satisfies. ii) Consider the equation 3y’ +...
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations....
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations. i) If we know a solution y = φ(x) of this equation, then any other solution can be written in the form y(x) = φ(x)+ 1/v(x), where v(x) is an unknown function which satisfies a certain linear equation. Using the fact that φ and y both solve the above Riccati equation, find the differential equation that v satisfies. ii) Consider the equation 3y’ +...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
(1 point) In general for a non-homogeneous problem y′′+p(x)y′+q(x)y=f(x) assume that y1,y2 is a fundamental set...
(1 point) In general for a non-homogeneous problem y′′+p(x)y′+q(x)y=f(x) assume that y1,y2 is a fundamental set of solutions for the homogeneous problem y′′+p(x)y′+q(x)y=0. Then the formula for the particular solution using the method of variation of parameters is yp=y1u1+y2u2 where u′1=−y2(x)f(x)W(x) and u′2=y1(x)f(x)W(x) where W(x) is the Wronskian given by the determinant W(x)=∣∣∣y1(x)y′1(x)y2(x)y′2(x)∣∣∣ So we have u1=∫−y2(x)f(x)W(x)dx and u2=∫y1(x)f(x)W(x)dx. NOTE When evaluating these indefinite integrals we take the arbitrary constant of integration to be zero. In other words we have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT