This assignment will give you a chance to perform some simple tasks with pointers. The tasks are only loosely related to each other. Start the assignment by copying the code below and pasting it into a .cpp file, then add statements to accomplish each of the tasks listed. Don't delete any of the given code or comments. Some of the tasks will only require a single C++ statement, others will require more than one. No documentation is required for this part of the assignment. /* Type your code after each of the commented instructions below (except that the statements for instructions 11 and 21 should be written where the instructions indicate). I have written the first statement for you. */ #include using namespace std; int main() { // 1. Create two integer variables named x and y. int x; // 2. Create an int pointer named p1. // 3. Store the address of x in p1. // 4. Use only p1 (not x) to set the value of x to 99. // 5. Using cout and x (not p1), display the value of x. // 6. Using cout and the pointer p1 (not x), display the value of x. // 7. Store the address of y into p1. // 8. Use only p1 (not y) to set the value of y to -300. // 9. Create two new variables: an int named temp, and an int pointer named p2. Make p2 // point to x. // 10. 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.) // 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. // Place the prototype for this function above the main function, and the definition // below main(). // 12. 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). // 13. 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 // // x is: 0 // y is: 99 // 14. Create an int array named 'a' with two elements. Make p2 point to the first element // of a. // 15. Use only p2 and x (not a) to initialize the first element of a with the value of x. // 16. Use only p2 and y (not a) to initialize the second element of a with the value of y. // Leave p2 pointing to the first element of a. Don't use pointer arithmetic. // Hint: don't forget that pointers and arrays are the same thing. // 17. Using cout and p2 only, display the address of the first element in a. // 18. Using cout and p2 only, display the address of the second element in a. Leave p2 // pointing to the first element of a. Don't use pointer arithmetic. // 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], then do not use "a" again. After this // the swapping steps should look very similar to step 10. Don't use a swap function.) // 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 pointers to integers as arguments, and // then swaps the contents of the two integers. Do not use any reference parameters. Do // not use C++'s swap() function. Place the function prototype for swap() above the main() // function, and place the definition of swap() below main(). // 22. Invoke your swap() function with the addresses of x and y (using the address-of // operator in the arguments), then print their values. (x should be 99, y should be 0). // 23. 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). } /* end of function main() */ Your output should look exactly like this (the blanks should be replaced with the correct values): # 5: x contains: __ # 6: x contains: __ #13: x is: __ and y is: __ #17: The address of the first element is ____________ #18: The address of the second element is ____________ #20: The first element in a[] is __ and the second element in a[] is __ #22: Now, x contains: 99 and y contains: 0 #23: Now, a[0] contains: 0 and a[1] contains: 99

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

This assignment will give you a chance to perform some simple tasks with pointers. The tasks are only loosely related to each other. Start the assignment by copying the code below and pasting it into a .cpp file, then add statements to accomplish each of the tasks listed. Don't delete any of the given code or comments. Some of the tasks will only require a single C++ statement, others will require more than one.

No documentation is required for this part of the assignment.

/* Type your code after each of the commented instructions below (except that the statements for instructions 11 and 21 should be written where the instructions indicate). I have written the first statement for you. */ #include <iostream> using namespace std; int main() { // 1. Create two integer variables named x and y. int x; // 2. Create an int pointer named p1. // 3. Store the address of x in p1. // 4. Use only p1 (not x) to set the value of x to 99. // 5. Using cout and x (not p1), display the value of x. // 6. Using cout and the pointer p1 (not x), display the value of x. // 7. Store the address of y into p1. // 8. Use only p1 (not y) to set the value of y to -300. // 9. Create two new variables: an int named temp, and an int pointer named p2. Make p2 // point to x. // 10. 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.) // 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. // Place the prototype for this function above the main function, and the definition // below main(). // 12. 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). // 13. 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 // // x is: 0 // y is: 99 // 14. Create an int array named 'a' with two elements. Make p2 point to the first element // of a. // 15. Use only p2 and x (not a) to initialize the first element of a with the value of x. // 16. Use only p2 and y (not a) to initialize the second element of a with the value of y. // Leave p2 pointing to the first element of a. Don't use pointer arithmetic. // Hint: don't forget that pointers and arrays are the same thing. // 17. Using cout and p2 only, display the address of the first element in a. // 18. Using cout and p2 only, display the address of the second element in a. Leave p2 // pointing to the first element of a. Don't use pointer arithmetic. // 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], then do not use "a" again. After this // the swapping steps should look very similar to step 10. Don't use a swap function.) // 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 pointers to integers as arguments, and // then swaps the contents of the two integers. Do not use any reference parameters. Do // not use C++'s swap() function. Place the function prototype for swap() above the main() // function, and place the definition of swap() below main(). // 22. Invoke your swap() function with the addresses of x and y (using the address-of // operator in the arguments), then print their values. (x should be 99, y should be 0). // 23. 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). } /* end of function main() */

Your output should look exactly like this (the blanks should be replaced with the correct values):

# 5: x contains: __ # 6: x contains: __ #13: x is: __ and y is: __ #17: The address of the first element is ____________ #18: The address of the second element is ____________ #20: The first element in a[] is __ and the second element in a[] is __ #22: Now, x contains: 99 and y contains: 0 #23: Now, a[0] contains: 0 and a[1] contains: 99

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education