Question

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. Please write your code in C

Homework Answers

Answer #1

The required statements are given below:

//Declare two int: x and y
int x, y;

//Declare two pointers p and q capable of pointing to int
int *p, *q;

//Make p point to y Make q point to x
p = &y;
q = &x;

//Assign 3 and 6 to x and y without mentioning x and y
*q = 3;
*p = 6;

//Redirect p to point to the same location q points to (two ways) Make q point to y
p = q;
q = &y;

//Swap the values of x and y without mentioning x and y
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;

The complete program using the above statement for testing is given below:

#include <stdio.h>

int main()
{
//Declare two int: x and y
int x, y;
  
//Declare two pointers p and q capable of pointing to int
int *p, *q;
  
//Make p point to y Make q point to x
p = &y;
q = &x;
  
//Assign 3 and 6 to x and y without mentioning x and y
*q = 3;
*p = 6;
  
//Redirect p to point to the same location q points to (two ways) Make q point to y
p = q;
q = &y;
  
//Swap the values of x and y without mentioning x and y
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
  
//display the value of x and y
printf("x = %d \ny = %d", x, y);

return 0;
}

OUTPUT:

x = 6
y = 3

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program to do the following: Declare and assign values to int variables x,...
Write a C++ program to do the following: Declare and assign values to int variables x, y Declare an int variable z; and store the sum of x and y in it Declare a pointer called pz and point it in the heap direction (using new) Store the z value in the heap location using the pz pointer Print the content of the pz pointer Print the pointer (the address)
In this lab, we want to get some practice with pointers -- specifically, the basic syntax...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays. Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows: Part A Declare a pointer-to-int, initialized to nullptr. Declare an int variable, initialized to some value of your choice....
java •Write a method product(int x, int y) which returns the product of multiplying x *...
java •Write a method product(int x, int y) which returns the product of multiplying x * y but doing it recursively. Recall that multiplication can be implemented as repeated addition. For example, 4 * 7 is the same as adding 7, 4 times: 7 + 7 + 7 + 7. •Write a main method that asks for two numbers, and returns their product by calling the product method. Compile and test your code in NetBeans and then on Hackerrank
The problem is to maximise utility u(x, y) =2*x +y s.t. x,y ≥ 0 and p*x...
The problem is to maximise utility u(x, y) =2*x +y s.t. x,y ≥ 0 and p*x + q*y ≤ w, where p=13.4 and q=3.9 and w=1. Here, * denotes multiplication, / division, + addition, - subtraction. The solution to this problem is denoted (x_0, y_0) =(x(p, q, w), y(p, q, w)). The solution is the global max. Find ∂u(x_0,y_0)/∂p evaluated at the parameters (p, q, w) =(13.4, 3.9, 1). Write the answer as a number in decimal notation with at...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
1.) P(2, 4) lies on the graph of y=x4-2x-4 Q is the point (x, x4-2x-4) Write...
1.) P(2, 4) lies on the graph of y=x4-2x-4 Q is the point (x, x4-2x-4) Write an equation for the slope of the secant line through P and Q as a function of x. 2.) Using the formula above, find the slopes fo 1.9, 1.99, 1.999, 2.001, 2.01, and 2.1 (round 4 decimal places) 3.) What is the slope of the tangent line to the curve y=x4-2x-4 at P(2, 4) Can you please explain in detail how to get the...
A Bernoulli differential equation is one of the form dy/dx+P(x)y=Q(x)y^n (∗) Observe that, if n=0 or...
A Bernoulli differential equation is one of the form dy/dx+P(x)y=Q(x)y^n (∗) Observe that, if n=0 or 1, the Bernoulli equation is linear. For other values of n, the substitution u=y^(1−n) transforms the Bernoulli equation into the linear equation du/dx+(1−n)P(x)u=(1−n)Q(x). Consider the initial value problem xy′+y=−8xy^2, y(1)=−1. (a) This differential equation can be written in the form (∗) with P(x)=_____, Q(x)=_____, and n=_____. (b) The substitution u=_____ will transform it into the linear equation du/dx+______u=_____. (c) Using the substitution in part...
(a) Two particles of fixed negative charge -q are placed on a y axis at fixed...
(a) Two particles of fixed negative charge -q are placed on a y axis at fixed positions of y = d and y = -d. (i) The electric field is probed at a point, P positioned at x = ad on the x-axis, where a is a variable. Illustrate and discuss the magnitudes and directions of electric field components at point P and also illustrate the net electric field at that point, of magnitude E. (ii) Starting from the expression...
(1 point) The point P(1/5,10) lies on the curve y=2/x . Let Q be the point...
(1 point) The point P(1/5,10) lies on the curve y=2/x . Let Q be the point (x,2/x). a.) Find the slope of the secant line PQ for the following values of x. If x=1.5/5, the slope of PQ is: If x=1.05/5, the slope of PQ is: If x=0.95/5, the slope of PQ is: If x=0.5/5, the slope of PQ is: Based on the above results, guess the slope of the tangent line to the curve at P(1/5,10)
Let A and B be true, X, Y, and Z false. P and Q have unknown...
Let A and B be true, X, Y, and Z false. P and Q have unknown truth value. Please, determine the truth value of the propositions in problem 1. Please, show the process of calculation by using the letter ‘T’ for ‘true,’ ‘F’ for ‘false,’ and ‘?’ for ‘unknown value’ under each letter and operator. Please underline your answer (truth value under the main operator) and make it into Bold font 1.  [ ( Z ⊃ P ) ⊃ P ]...