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: 3766492 • 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

#include <cstdlib>
#include <iostream>

using namespace std;
int nums(int &x,int &y) // passing parameters by reference So original value will also get change
{
       
int v=x+y;
y-=x;
return v;     
       
}
int nums1(int p,int q) // passing parameters by value so original parameters will not change
{

q-=p;
int r;
r=p+q;
return r;

}
int main(int argc, char *argv[])
{
    int x=10, y=20, *ptr1=&x, *ptr2=&y;
    int *ptr;
    int a,b,c;
    int z=nums(x,y); //calling nums and nums1 function
    int k=nums1(x,y);
    cout<<"x="<<x<<endl;
    cout<<"y="<<y<<endl;
    cout<<"*ptr1="<<*ptr1<<endl; // ptr1 and ptr2 are pointers so printing value using *(dereferencing operator)
    cout<<"*ptr2="<<*ptr2<<endl;
    cout<<"nums(&x,&y)="<<z<<endl;
     cout<<"nums1(p,q)="<<k<<endl;
    cout<<"Enter a"<<endl;
    cin>>a;
    cout<<"Enter b"<<endl;
    cin>>b;
    cout<<"Enter c"<<endl;
    cin>>c;
     ptr=&b;
     c=a+b;
     *ptr=b-a;
     cout<<"*ptr="<<*ptr<<endl;
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<"c="<<c<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}