This part of the 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 file named pointerTasks.cpp with an empty main function, then add statements in main() 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. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below into your main function first, and then fill in your 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) 2. The League with DMA Rewrite your League program from Assignment 5 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int wins[], int size) Your modified league program should start out by asking the user how many teams will be entered. It should then dynamically allocate two appropriate arrays, and proceed just like the original League assignment, calling the above three functions. When your program is done using the arrays, don't forget to use delete [] to return their memory to the system. Note: you must use dynamic memory allocation for your arrays, i.e. the new and delete [] operators, to receive credit. The output from your program should look approximately like this (user input in orange bold): How many teams will you enter?: 4 Enter team #1: Padres Enter the wins for team #1: 75 Enter team #2: Dodgers Enter the wins for team #2: 91 Enter team #3: Giants Enter the wins for team #3: 92 Enter team #4: Cubs Enter the wins for team #4: 65 League Standings: Giants: 92 Dodgers: 91 Padres: 75 Cubs: 65
1. Pointer Tasks
This part of the 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 file named pointerTasks.cpp with an empty main function, then add statements in main() 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. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below into your main function first, and then fill in your 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)
2. The League with DMA
Rewrite your League program from Assignment 5 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays.
This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following:
void initializeData(string names[], int wins[], int size)
void sort(string names[], int wins[], int size)
void display(string names[], int wins[], int size)
Your modified league program should start out by asking the user how many teams will be entered. It should then dynamically allocate two appropriate arrays, and proceed just like the original League assignment, calling the above three functions. When your program is done using the arrays, don't forget to use delete [] to return their memory to the system.
Note: you must use dynamic memory allocation for your arrays, i.e. the new and delete [] operators, to receive credit.
The output from your program should look approximately like this (user input in orange bold):
How many teams will you enter?: 4
Enter team #1: Padres
Enter the wins for team #1: 75
Enter team #2: Dodgers
Enter the wins for team #2: 91
Enter team #3: Giants
Enter the wins for team #3: 92
Enter team #4: Cubs
Enter the wins for team #4: 65
League Standings:
Giants: 92
Dodgers: 91
Padres: 75
Cubs: 65
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)