This lab is due by Wednesday, December 09, 2020, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.
The goal of this lab is to gain familiarity with using the concepts learned on pointers, addresses, and dynamic memory.
Pointer Instructions
Create a new folder SetXC and a new project LXC with
main.cpp
that does the following:
- Declare an integer named
iNumwith initial value9. - Declare a double named
dNumwith initial value14.25. - Declare two pointers to integers named
iPtr1andiPtr2. - Declare a pointer to a double named
dPtr. - Assign the address of
iNumtoiPtr1. - Output the address of
iNumand be sure to identify to the user what you are displaying. There are two ways you can do this; you should do both, to convince yourself they are the same. - Use
iPtr1to display the contents ofiNum. - Try to assign the address of
dNumtoiPtr1. What error message do you see? Comment out this bad line of code, but include the error message as a comment directly below. - Assign the address of
dNumtodPtr. - Output the address and then the contents of
dNum(usingdPtr). - Directly change the value of
iNumto7. - Use
iPtr1to output the contents ofiNum. - Assign
iPtr2to have the same value asiPtr1. Do not referenceiNum; instead use the address stored iniPtr1. - Output the address in
iPtr2. This should be the same as displayed in step 6. - Output the value pointed to by
iPtr2. - Using
iPtr2, change the contents ofiNumto12. - Output the contents of
iNumthree times, first usingiPtr1, then usingiPtr2, theniNumdirectly. In each case, identify what the user is seeing appropriately.
Dynamic Memory Instructions
The next steps will illustrate the use of dynamic memory. You can
continue adding to the same
main.cpp
.
- Declare another pointer to an integer named
iPtr3. - Declare an integer named
num. Have the user input the value fornum, which represents the number of items in an array. - Using the
newoperator, giveiPtr3enough memory to containnumintegers. - Write a loop that prompts the user for
numvalues and stores the values entered in theiPtr3array. - Write a loop that sums the values in the array.
- Display the sum of the values.
- Using the
deleteoperator, return the memory iniPtr3back to the available memory heap.
Lab Submission
You will submit your solution to this lab with the rest of SetXC. Detailed instructions for doing this are posted in Assignment XC.
This lab is due by Wednesday, December 09, 2020, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.