Question: Write a program in C++ code. Start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements to accomplish
Write a program in C++ code.
Start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one.
Create two integer variables named x and y
Create an int pointer named p1
Store the address of x in p1
Use p1 to set the value of x to 99
Using cout and x, display the value of x
Using cout and the pointer p1, display the value of x
Store the address of y into p1
Use p1 to set the value of y to -300
Create two new variables: an int named temp, and an int pointer named p2
Use temp, p1, and p2 to swap the values in x and y (this will take a few statements)
Write a function with the following signature: void noNegatives(int *x). The function should accept the address of an int variable. If the value of this integer is negative then it should set it to zero
Invoke the function twice: once with the address of x as the argument, and once with the address of y
Use p2 to display the values in x and y (this will require both assignment statements and cout statements)
Create an int array with two elements. The array should be named a
Use p2 to initialize the first element of a with the value of x
Use p2 to initialize the second element of a with the value of y
Using cout, display the address of the first element in a
Using cout display the address of the second element in a
Use p1, p2, and temp to swap the values in the two elements of array a. (first point p1 at a[0], then point p2 at a[1]. After this the swapping steps should look very similar to step 10.)
Display the values of the two elements. (The first element should be 99, the second 0).
Write a function named swap that accepts two integer pointers as arguments, and then swaps the two integers that the pointers point to. This function must be pass by pointer, i.e. int *, not pass by reference, i.e. int &.
Invoke your swap function with the addresses of x and y, then print their values.(x should be 99, y should be 0).
Invoke your swap function with the address of the two elements in array a, then print their values. (a[0] should be 0, a[1] should be 99)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
