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

  1. write a loop to print an array a with pointers backwards. You can use a variable “size” for array size.

Please write the code in C

Solutions

Expert Solution

1.

Code:

#include<stdio.h>
int main()
{
   int x,y,*p,*q,temp;
   /*Declaring the variables*/
   p=&y;/*p points to y*/
   q=&x;/*q points to x*/
   *p=6;/*y=6*/
   *q=3;/*x=3*/
   temp=p;
   p=q;
   q=temp;
   /*changing the pointers*/
   printf("*p=%d\n",*p);
   printf("*q=%d",*q);
   /*Printing the poiters*/
  
}

Output:

2.

Code:

#include<stdio.h>
int main()
{
   int x,y,*p,*q,temp;
   /*Declaring the variables*/
   p=&x;/*p points to x*/
   q=&y;/*q points to y*/
   *p=3;/*x=3*/
   *q=*p+1;/*y=x+1*/
   temp=p;
   p=q;
   q=temp;
   /*interchanging the pointer references*/
   printf("*p=%d\n",*p);
   printf("*q=%d",*q);
   /*Printintg the values*/      
}

Output:

3.

Code:

#include<stdio.h>
int main()
{
   int x,y,z,*p,*q,*r,temp,temp1;/*Declarig the variables*/
   p=&y;/*p points to y*/
   q=&x;/*q points to x*/
   r=&z;/*r points to z*/
   *q=5;/*x=5*/
   *p=10;/*y=10*/
   *r=20;/*z=20*/
   temp=*q;
   *q=*r;/*x get the z value*/
   temp1=*p;
   *p=temp;/*y get the x previous value*/
   *r=temp1;/*z get the y value*/
   printf("x=%d\ny=%d\nz=%d",x,y,z);
   /*Printing x y z */  
}

Output:

4.

Code:

#include<stdio.h>
int main()
{
   int *a=(int*) malloc(sizeof(int));
   int *b=(int*) malloc(sizeof(int));
   /*Dynamically alocaing 2 integers*/
   int temp;
   *a=5;
   *b=10;
   /*assigning values*/
   temp=*a;
   *a=*b;
   *b=temp;
   /*swapping*/
   printf("*a=%d \n*b=%d",*a,*b);
   /*printing the *a and *b*/  
}

Output:

5.

Code:

#include<stdio.h>
main()
{
   int a[5]={1,2,3,4,5};
   int i,size=5;
   /*Declaring the vriables*/
   printf("Priting the array: ");
   for(i=0;i<size;i++)
   {
       printf("%d ",*(a+i));
       /*Printing the array*/
   }
   printf("\nPrinting elements in backword direction: ");
   for(i=size-1;i>=0;i--)
   {
       printf("%d ",*(a+i));
       /*printing in reverse order*/
   }
}

Output:


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