This lab is due by Friday, December 08, 2017 11:59 PM . 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 project with
main.cpp
that does the following:
- Declare an integer named iNum with initial value 9.
- Declare a double named dNum with initial value 14.25.
- Declare two pointers to integers named iPtr1 and iPtr2.
- Declare a pointer to a double named dPtr.
- Assign the address of iNum to iPtr1.
- Output the address of iNum and 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 iPtr1 to display the contents of iNum.
- Try to assign the address of dNum to iPtr1. 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 dNum to dPtr.
- Output the address and then the contents of dNum (using dPtr).
- Directly change the value of iNum to 7.
- Use iPtr1 to output the contents of iNum.
- Assign iPtr2 to have the same value as iPtr1. Do not
reference iNum; instead use the address stored in iPtr1.
- 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 of iNum to 12.
- Output the contents of iNum three times, first using iPtr1,
then using iPtr2, then iNum directly. 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
for num, which represents the number of items in an array.
- Using the new operator,
give iPtr3 enough memory to
contain num integers.
- Write a loop that prompts the user for num values and stores
the values entered in the iPtr3
array.
- Write a loop that sums the values in the array.
- Display the sum of the values.
- Using the delete operator,
return the memory in iPtr3
back to the available memory heap.
Lab Submission
You will submit your solution to this lab with the rest of AXC.
Detailed instructions for doing this are posted in Assignment XC.
This lab is due by Friday, December 08, 2017 11:59 PM . |