$25
In this programming assignment, you will implement smart arrays (arrays that expand automatically whenever they get too full). This is an immensely powerful and awesome data structure, and it will ameliorate several problems we often encounter with arrays in C
1. Overview
A smart array is an array that grows to accommodate new elements whenever it gets too full. As with normal arrays in C, we have direct access to any index of the smart array at any given time. There are four main advantages to using smart arrays, though:
1. We do not need to specify the length of a smart array when it is created. Instead, it will automatically expand when it gets full. This is great when we don’t know ahead of time just how much data we’re going to end up holding in the array.
2. We will use get() and put() functions to access individual elements of the array, and these functions will check to make sure we aren’t accessing array positions that are out of bounds. (Recall that C doesn’t check whether an array index is out of bounds before accessing it during program execution. That can lead to all kinds of whacky trouble!)
3. If our arrays end up having wasted space (i.e., they aren’t full), we can trim them down to size.
4. In C, if we have to pass an array to a function, we also typically find ourselves passing its length to that function as a second parameter. With smart arrays, the length will get passed automatically with the array, as they’ll both be packaged together in a struct.
While some languages offer built-in support for smart arrays (such as Java’s ArrayList class), C does not. That’s where you come in. You will implement basic smart array functionality in C, including:
1. automatically expanding the smart array’s capacity when it gets full;
2. adding new elements into arbitrary positions in the smart array, or at the end of the smart array;
3. providing safe access to elements at specific positions in the smart array;
4. gracefully signaling to the user (i.e., the programmer (re-)using your code) when he or she attempts to access an index in the smart array that is out of bounds (instead of just segfaulting);
5. … and more!
In this assignment, your smart array will be designed to hold arrays of strings. A complete list of the functions you must implement, including their functional prototypes, is given below in Section 3, “Function Requirements”). You will submit a single source file, named SmartArray.c, that contains all required function definitions, as well as any auxiliary functions you deem necessary. In SmartArray.c, you should #include any header files necessary for your functions to work, including SmartArray.h (see Section 2, “SmartArray.h”).
Note that you will not write a main() function in the source file you submit! Rather, we will compile your source file with our own main() function(s) in order to test your code. We have attached example source files that have main() functions, which you can use to test your code. We realize this is completely new territory for most of you, so don’t panic. We’ve included instructions on compiling multiple source files into a single executable (e.g., mixing your SmartArray.c with our SmartArray.h and testcase01.c) in Sections 4 and 5 (“Compilation and Testing”).
Although we have included sample main() functions to get you started with testing the functionality of your code, we encourage you to develop your own test cases, as well