Question: Write code to define these five items: an int an array of ints with 1 0 0 elements a dynamically allocated int a dynamically allocated

Write code to define these five items:
an int
an array of ints with 100 elements
a dynamically allocated int
a dynamically allocated array of ints, with 100 elements
a set of 100 dynamically allocated ints, using an array of 100 pointers
Then, add to that code:
in part 1, set the int to the value 33
part 2, set the element with index 10 of the array to 34
part 3, set the dynamically allocated object to 35
part 4, the element with index 10 of the dynamically allocated array to 36
part 5, set the 11th dynamically allocated int (that would be referenced via index 10 in
the pointer array) to value 37
For example, here is the complete answer for (1):
int i;
i=33;
Imagine that you need to define n number of dynamically allocated ints, where n is some number. To do this, you would need n int pointers. You could name the pointers p1, p2, p3, and so on up to pn, but this gets really inconvenient as n gets larger. So instead of this, we use
a pointer array. You decide ahead of time what the maximum number of individually allocated ints is going to be, say it's 1,000. You define an array of 1,000 int pointers. None are used initially, so they are all initialized to nullptr. As you allocate ints, you use pointers in the pointer
array. You'll need to keep track of which pointer(s) in the array have been used so that you don't accidentally destroy one.
Here's a picture of what this looks like in memory. This particular structure has a pointer array with five elements. Three elements have been used to allocate ints, two are unused and retain their initial values of nullptr .
 Write code to define these five items: an int an array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!