Append a value to array meaning ?

Please explain me the meaning of the below question. I will solve it myself but just want to understand its meaning. What does "appends a given value to this array" imply ?

Create a dynamic array of int with a initial space of 4. Write a function ‘append’ that appends a given value to this array. At any stage, if this function finds the array full it automatically doubles the size of array to accommodate this new value. Also write a function to display all the elements of this array. Write a main to test all these functions
Last edited on
I think it means you're to put a value into the first empty element.

So you begin with an array in which you have put no values. It is size four, but you haven't put any values in it yet. It looks like this:

| x | x | x | x|

where x is an unknown value, because you haven't put anything in it yet. Then, you put the number 7 in. Now it looks like this:

| 7 | x | x | x|

Now, you put in the numbers 3, 5, and 1

| 7 | 3 | 5 | 1|

Now you want to put in the number 9. However, the array is full.
At any stage, if this function finds the array full it automatically doubles the size of array to accommodate this new value.

So you have to double the size of the array, so it looks like this:

| 7 | 3 | 5 | 1| x | x | x | x|

You will have to do this by creating a new array of the new size, and copying into it all the numbers that were in the old array, and then delete the old array.

Now you have an array with eight elements; you can add the number 9:

| 7 | 3 | 5 | 1| 9 | x | x | x|


You will at all times need to keep track of how many elements you've put in (so you know where the next empty space is) and the size of the array (so you know when you need to make a new one, bigger).

Unfortunately, there is no way to look at an array and know if it is full or not, unless you use some kind of special value to mark the end of the array. An array does not know anything about itself, like its size. I can only imagine that you're expected to keep track of the size and how many you have written in main.
Containers in the standard library such as std::string or std::vector keep track of two values: the size and the capacity. The size represents how much of the container is in use. The capacity represents how much space currently is allocated.

In the OP, where it says:
Create a dynamic array of int with a initial space of 4.
that would mean a capacity of 4, and a size of 0.

As each element is appended to this dynamic array, these values will change as follows:
size    capacity
  0         4     Initial empty array
  1         4     After first element appended.
  2         4 
  3         4
  4         4     now completely filled
  5         8     before appending, re-allocate with double capacity
  6         8
  ... and so on.

Last edited on
Please give some hint how to solve this problem. Will while loop be used here
Please give some hint how to solve this problem.

Approach it in small steps. The original question involved several separate parts:

• a dynamic array of int

• a function ‘append’

• a function to display all the elements

• a main to test all these functions

There will definitely be a need for a loop in at least one of these, and very likely several loops by the time you are done.

Myself, I like to write the 'display' function early in the development process, it isn't necessary to have everything completed for this to be possible, and the ability to display what is contained in the array will help with all the other parts of the problem.
Topic archived. No new replies allowed.