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
iNum
with initial value9
. - Declare a double named
dNum
with initial value14.25
. - Declare two pointers to integers named
iPtr1
andiPtr2
. - Declare a pointer to a double named
dPtr
. - Assign the address of
iNum
toiPtr1
. - 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 ofiNum
. - Try to assign the address of
dNum
toiPtr1
. 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
todPtr
. - Output the address and then the contents of
dNum
(usingdPtr
). - Directly change the value of
iNum
to7
. - Use
iPtr1
to output the contents ofiNum
. - Assign
iPtr2
to 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 ofiNum
to12
. - Output the contents of
iNum
three times, first usingiPtr1
, then usingiPtr2
, theniNum
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 fornum
, which represents the number of items in an array. - Using the
new
operator, giveiPtr3
enough memory to containnum
integers. - Write a loop that prompts the user for
num
values and stores the values entered in theiPtr3
array. - Write a loop that sums the values in the array.
- Display the sum of the values.
- Using the
delete
operator, return the memory iniPtr3
back 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.