Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program: Pointer Manipulation The following are the procedure to follow: A). Thi

ID: 3766703 • Letter: P

Question

Program: Pointer Manipulation

The following are the procedure to follow:

A). This program must have a main routine and two functions.

B). Declare in main: int x=10, int y=20, *ptr1=&x, *ptr2=&y.

C). Functions will be called from main as necessary and parameters will be passed.

D). One function will be called nums and receive two integer pointer parameters, &x and &y. In the function, declare local int v = x+y, set y-=x, then return v.

E). The second function will be called nums1 and will have two integer variables, p and q, in the parameter list. Use p and q in your program for continuity. Inside the function, set q-=p, then declare local int variable r = p+q. Variable r is returned to main.

F). Write data to the screen.

G). For part 2, still in main, do a manipulation of pointers. Read in values of a, b, and c from the keyboard. For grading reference, use a(5), b(8), c(0). Define *ptr=&b. Variable c = a+b, *ptr = b-a. Write the data *ptr, a, b, c, to the screen and to a file.

Please give some explanation with the code using only C++ Visual Studio 2010 or later version. Thanks.

Explanation / Answer

Here is the explanation along with the code for you. If you need any further clarifications, just get back to me.

#include <iostream>
using namespace std;

int nums(int &x, int &y)   //Function nums() is taking the addresses of x and y.
{
int v;           //v is declared a local integer variable.
v = x + y;       //The summation of x and y, i.e, 30 is assigned to v.
y -= x;           //The value of x is subtracted from y and is assigned to y, i.e, y is assigned with 10.
return v;           //The value of v i.e., 30 is returned.
}

int nums1(int p, int q)       //Function nums1() is taking the values of x and y, ie., p = 10, q = 10.
//Note that q value is not 10, as it is modified in the previous function, as it received pointer parameters.
{
q -= p;               //The value of p is subtracted from q, and assigned to q, i.e., q = 0.
int r = p + q;       //The summation of p and q, i.e., 10 is assigned to r.
return r;               //The value of r i.e., 10 is returned.
}
//Assume in main() x address is 10101,
//And y address is 20202.
int main()
{
int x=10;       //x is assigned a value 10.
int y=20;       //y is assigned a value 20.
int *ptr1=&x;   //ptr1 is assigned with the address of x, i.e., 10101.
int *ptr2=&y;   //ptr2 is assigned with the address of y, i.e., 20202.
cout<<"The result of the function call nums(x, y) is : "<<nums(x, y)<<endl;   //30 is printed.
cout<<"The result of the function call nums1(x, y) is: "<<nums1(x, y)<<endl;   //10 is printed.
  
//Part-2.
int a, b, c;
cout<<"Enter the values of a, b, and c: ";
cin>>a>>b>>c;       //Assume the entered values are, a = 5, b = 8, and c = 0.
int *ptr = &b;   //The address of b (assume it to be 30303, is assigned to ptr.)
c = a+b;           //The summation of a and b is assigned to c, i.e., c = 13.
*ptr = b-a;       //a is subtracted from b and the result is stored at the location where ptr is pointing to, i.e., b = 3.
cout<<"*ptr = "<<*ptr<<endl;   //This is b value, i.e., 3 is printed.
cout<<"a = "<<a<<endl;       //a value, i.e., 5 is printed.
cout<<"b = "<<b<<endl;       //b value, again 3 is printed.
cout<<"c = "<<c<<endl;       //c value, i.e., 13 is printed.
}