This assignment will give you a chance to perform some simple tasks with pointer
ID: 3673702 • 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 time sign to 99 Using cout and x (not p1), display the value of x Using count 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 no Negatives(int* x). The function should accept the address of an int variables. 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 count statements). You can use x and y in assignment statements, but not in your count statement this should produce the output x is:0 y is:99 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 q with of a the value of y Using count, display the address the address of the first element in a Using count 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 reference parameters. Invoke your swap function with the address 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
#include<iostream>
using namespace std;
void noNegatives(int *x){
if(*x<0)
*x = 0;
}
void swap(int *x,int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main(){
int x,y;
int *p1;
p1 = &x;
*p1 = 99;
cout<<"x = "<<x<<endl;
cout<<"x = "<<*p1<<endl;
p1 = &y;
*p1 = -300;
int temp;
int *p2;
p2 = &x;
temp = *p1;
*p1 = *p2 ;
*p2 = temp;
cout<<"x = "<<x<<" y = "<<y<<endl;
noNegatives(&x);
noNegatives(&y);
p2 = &x;
cout << "x is: "<<*p2<<endl;
p2 = &y;
cout << "y is: "<<*p2<<endl;
int a[2];
p2 = a;
*p2 = x;
*(p2+1) = y;
cout<<"a[0] = "<<a[0]<<endl;
cout<<"a[1] = "<<a[1]<<endl;
p1 = a;
p2 = a+1;
temp = *p1;
*p1 =*p2;
*p2 = temp;
cout<<"a[0] = "<<a[0]<<endl;
cout<<"a[1] = "<<a[1]<<endl;
swap(&x,&y);
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
swap(a,a+1);
cout<<"a[0] = "<<a[0]<<endl;
cout<<"a[1] = "<<a[1]<<endl;
}