Dynamic Array - Get and Set Elements
We can put data into the array, so the next step will be getting data out of the array.
Get
We are getting close to a functional dynamic array. The data is all in the array, but we can’t access it. The get
function will let us read the data from the array.
Here is the prompt that I used to get this function started.
We need a default value, as this is a template function, so we do not know what a valid default value looks like.
The code inside get
should be quite small, and for me Copilot did ok with this. I added in comments to // Check if the index is out of bounds
and when it is, I noted // The index is out of bounds, so return the default value
.
We can extend main to now print out the values from the valid indexes, but we should also test printing out invalid indexes. Try coming up with prompts to get Copilot to help you with this. You want to make sure that you can see the data from within the array now, and that accessing invalid data does not crash but does output the indicated default value.
Set
The last function for this part is set
. This is used to update a value in the array. You can use the following to help prompt Copilot.
When checking this, think about possible issues. The main things will be the array index, but also the possibility that array
may be a nullptr
.
Once you have this implemented, update main so that you change the values in the array and then re-print them. When you have this working, you have a usable dynamic array. In the test we have got it working with an array of int
, but it will also work with an array of any other type name.