This assignment will give you a chance to perform some simple tasks with pointer
ID: 3799774 • Letter: T
Question
This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. Start the assignment by creating a .cpp file 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. Make sure that the tasks appear in your code in the same order that they are listed here.
Create two integer variables named x and y
Create an int pointer named p1
Store the address of x in p1
Use only p1 (not x) to set the value of x to 99
Using cout and x (not p1), display the value of x
Using cout and the pointer p1 (not x), display the value of x
Store the address of y into p1
Use only p1 (not y) to set the value of y to -300
Create two new variables: an int named temp, and an int pointer named p2. Make p2 point to x.
Use only temp, p1, and p2 (not x or y) to swap the values in x and y (this will take a few statements. Don't use a swap function)
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 x or y for the argument (not p1 or p2)
Use p2 to display the values in x and y (this will require both assignment statements and cout statements). You can use x and y in assignment statements, but not in your cout statement. this should produce the output
Create an int array named 'a' with two elements. Make p2 point to the first element of a.
Use only p2 and x (not a) to initialize the first element of a with the value of x.
Use only p2 and y (not a) to initialize the second element of a with the value of y
Using cout and p2 only, display the address of the first element in a
Using cout and p2 only, 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], then do not use "a" again. After this the swapping steps should look very similar to step 10. Don't use a swap function.)
Display the values of the two elements. (The first element should be 99, the second 0).
Write a function named 'swap' that accepts two pointers to integers as arguments, and then swaps the contents of the two integers. Do not use any reference parameters.
Invoke your swap function with the addresses of x and y (using the address-of operator), 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)
Explanation / Answer
The pointer references and the whole program step by step is given below.
#include<iostream>
using namespace std;
//Prototypes
void noNegatives(int *x);
int main() {
//1. Create two integer variables named x and y
int x, y;
//2-3. Create an int pointer named p1 and store the addres of x in p1
int *p1 = &x;
//cout << "The pointer p1 value is address of value x using pointer variable " << p1 << endl;
//4. Use p1 to set the value of x to 99
*p1 = 99;
//5. Using cout, display the value of x
cout << "The value of x using x variable is" << x << endl;
//6. Using cout and the pointer p1, display the value of x
cout << "x value using the pointer p1 is " << *p1 << endl;
//7. Store the address of y into p1
p1 = &y;
//8. Use p1 to set the value of y to -300
*p1 = -300;
cout << "y value is " << y << endl;
//9. Create two new variables: an int named temp, and an int pointer named p2
int temp, *p2;
//10. Use temp, p1, and p2 to swap the values in x and y
p1 = &x;
p2 = &y;
temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "Before swapping x value is: " << x << " and y is: " << y << endl;
//12. Invoke the function twice: once with the address of x as the argument,
//and once with the address of y
noNegatives(&x);
noNegatives(&y);
//13. Use p2 to display the values in x and y
p2 = &x;
cout<<"After swap ";
cout << "x = " << *p2 << " and ";
p2 = &y;
cout << "y = " << *p2 << endl;
//14. Create an int array with two elements. The array should be named a
int a[2];
//15. Use p2 to initialize the first element of a with the value of x
p2 = &x;
a[0] = *p2;
//16. Use p2 to initialize the second element of a with the value of y
p2 = &y;
a[1] = *p2;
//17. Using cout, display the address of the first element in a
cout << "The address of first element a[0] = " <<& a[0] << "and the value is "<<a[0]<<endl;
//18. Using cout, display the address of the second element in a
cout << "The address of second element a[1] = " << &a[1] << "and the value is "<<a[1]<<endl;
//19. Use p1, p2, and temp to swap the values in the two elements of a
p1 = &a[0];
p2 = &a[1];
temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "The values of the array elements are :"<<" ";
//20. Display the values of the two elements.
for (int i = 0; i < 2; i++){
cout << "a[" << i << "] = " << a[i] << endl;
}
return 0;
}
//11. 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 int is negative it should set it to zero.
void noNegatives(int *x){
if (*x < 0) *x = 0;
}
//12Function swap
void swap(int *p1, int *p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}