In: Computer Science
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks using pointers. The instructions below are a sequence of tasks that are loosely related to each other.
Start the assignment by creating a file names pointerTasks.cpp with an empty main function , then add the statements in main() to complete each of the tasks listed below. Some tasks only require a single C++ statement while others will require more.
For each step, include a comment in the program that indicates which step is being completed in the following statements. Easiest way to do this is by copying and pasting below in the main function first, and then fill in the statements:
// 1. Create two integer variables named x and y // 2. Create an int pointer named p1 // 3. Store the address of x in p1 // 4. Use p1 to set the value of x to 99 // 5. Using cout and x, display the value of x // 6. Using cout and the pointer p1, display the value of x // 7. Store the address of y into p1 // 8. Use p1 to set the value of y to -300 // 9. Create two new variables: an int named temp, and an int pointer named p2 // 10. Use temp, p1, and p2 to swap the values in x and y (this will take a few statements) // 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 integer is negative then it should set it to zero // 12. Call the function twice: once with the address of x as the argument, and once with the address of y // 13. Use p2 to display the values in x and y (this will require both assignment statements and cout statements) // 14. Create an int array with two elements. The array should be named 'a' // 15. Use p2 to initialize the first element of a with the value of x // 16. Use p2 to initialize the second element of a with the value of y // 17. Using cout, display the address of the first element in a // 18. Using cout, display the address of the second element in a // 19. 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.) // 20. Display the values of the two elements. (The first element should be 99, the second 0). // 21. 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 &. // 22. Call your swap function with the addresses of x and y, then print their values. (x should be 99, y should be 0). // 23. Call 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)
I've declared and defined functions namely noNegatives(int* x) and swap(int*,int*) above main function and called them inside the main(). Please find below the complete code for first task. Please note that the comments are as according to what was provided in problem, additional comments are added to make the program more understandable. Copy the code below and save the file as pointerTasks.cpp and run the code to generate .exe file as output.
#include <iostream>
using namespace std;
//Start debugging the code from the main()function
// 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 integer is negative then it should set it to zero
void noNegatives(int *x){
if(*x<0)
*x=0;
}
// 21. 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 &.
void swap(int* x, int* y)
{
int z = *x;
*x = *y;
*y = z;
}
int main(){
// 1. Create two integer variables named x and y
int x,y;
// 2. Create an int pointer named p1
int* p1;
// 3. Store the address of x in p1
p1=&x;
// 4. Use p1 to set the value of x to 99
*p1=99;
// 5. Using cout and x, display the value of x
cout<<"Value of x : "<<x;
// 6. Using cout and the pointer p1, display the value of x
cout<<"\nValue of x using *p1 : "<<*p1;
// 7. Store the address of y into p1
p1=&y;
// 8. Use p1 to set the value of y to -300
*p1=-300;
// 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 (this will take a few statements)
p2=&x;
temp=*p2;
*p2=*p1;
*p1=temp;
// 11. Write a function with the following signature: void noNegatives(int *x)
//The function definition is provided above the main() function at line #7
// 12. Call 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 (this will require both assignment statements and cout statements)
p2=&x;
cout<<"\nValue of x using *p2 : "<<*p2;
p2=&y;
cout<<"\nValue of y using *p2 : "<<*p2;
// 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<<"\nAddress of first element in \'a\' : "<<&a[0];
// 18. Using cout, display the address of the second element in a
cout<<"\nAddress of first element in \'a\' : "<<&a[1];
// 19. 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.)
p1=&a[0];
p2=&a[1];
temp=*p2;
*p2=*p1;
*p1=temp;
// 20. Display the values of the two elements. (The first element should be 99, the second 0).
cout<<"\nFirst element of array \'a\' : "<<a[0];
cout<<"\nSecond element of array \'a\' : "<<a[1];
// 21. 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 &.
//function swa(int*,int*) is defined at line #13 of this program above main() function
// 22. Call your swap function with the addresses of x and y, then print their values. (x should be 99, y should be 0).
swap(x,y); //Function call for swap(int*,int*) defined at line #13
cout<<"\nValue of x : "<<x;
cout<<"\nValue of y : "<<y;
// 23. Call 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)
swap(&a[0],&a[1]);
cout<<"\nFirst element of array \'a\' : "<<a[0];
cout<<"\nSecond element of array \'a\' : "<<a[1];
return 0; //main() ends here
}
//program ends
Output:-
Value of x : 99
Value of x using *p1 : 99
Value of x using *p2 : 0
Value of y using *p2 : 99
Address of first element in 'a' : 0x61ff04
Address of first element in 'a' : 0x61ff08
First element of array 'a' : 99
Second element of array 'a' : 0
Value of x : 99
Value of y : 0
First element of array 'a' : 0
Second element of array 'a' : 99
Note:- Address of variables may be allocated differently on your device
Happy Learning.....
Give it a thumps up if you got the answer....