IntArray
struct to add an additional fields,
capacity. For our updated
IntArray
the
length
represents the number of items actually in the list. The
capacity
represents how many items the list could hold before we have to make it bigger.
makeIntArray()
and
makeZeroedIntArray()
so that they set both the
length
and the
capacity
to the given
size.
resizeIntArray()
so it changes the
capacity
of the IntArray, but
not its length.
resizeIntArray(), should print a bunch of empty arrays. The other tests should be unchanged.
IntArray makeArray()
that takes no arguments and returns an empty (length == 0) IntArray with an initial capacity of 5.
(9 points) Write a function
void append(IntArray* ar, int newElement)
to add the given element to the end of the given array. You will need to adjust the length. If the array has reached its capacity, use your
resizeInArray()
function to double the array's capacity.
Doubling gives us room for future growth. If we just grew by one we would have to keep growing with every additional append.
int* getMemoryLocation(IntArray* ar)
that returns the address of the internal array.
int getCapacity(IntArray* ar)
int getLength(IntArray* ar)
that do what their names suggest.
(13 points) Create a driver function that will read a bunch of integers from the console and store them in an IntArray
created using
makeArray()
and extending using
append().
After each integer is read, you should print the memory address of the internal array, its current length, its current capacity, and the integer contents of the array (on a single line).
For example, a run might look like:
stored at 003D3D80, with length = 1 and capacity = 5, contents = [3] stored at 003D3D80, with length = 2 and capacity = 5, contents = [3,7] stored at 003D3D80, with length = 3 and capacity = 5, contents = [3,7,8] stored at 003D3D80, with length = 4 and capacity = 5, contents = [3,7,8,2] stored at 003D3D80, with length = 5 and capacity = 5, contents = [3,7,8,2,4] stored at 003D5DF0, with length = 6 and capacity = 10, contents = [3,7,8,2,4,0] stored at 003D5DF0, with length = 7 and capacity = 10, contents = [3,7,8,2,4,0,12] stored at 003D5DF0, with length = 8 and capacity = 10, contents = [3,7,8,2,4,0,12,4] stored at 003D5DF0, with length = 9 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4] stored at 003D5DF0, with length = 10 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4,7] stored at 003D5E20, with length = 11 and capacity = 20, contents = [3,7,8,2,4,0,12,4,4,7,9]
ints.txt" inside your Eclipse project.
int index(IntArray* ar, int value)
that searches the given array for the given value and returns the value's index if found. Make your code do something sensible if the value is not found.
printIntArray(), but instead of printing, returns a string. Your function's signature should be
char* toString(IntArray* ar)
Add code that uses your function to demonstrate that it works.