Build a resizeable array of integers that can grow on demand and compile and make it available as a library. Make sure you use the .h and .c file structure , and make use of #ifndef and similar preprocessor directives to make sure your .h files can be included multiple times without problems. Make use of the following struct. typedef struct { int *array; size_t used; size_t size; } Array; Use this struct to store a resizeable array of integers.
In “used,” store the index up to which entries of array are being used. In “size,” keep the size of “array”. Implement the following functions using proper dynamic memory allocation functions. Double the size of “array” when more room is needed. Decrease the size of the array to half when the array is less than half full. The array size should be changed automatically as part of the insert void insertIntoArray(Array *a, int element) void removeLast(Array *a) // removes last element if size > 0, does nothing otherwise void freeArray(Array *a) void initArray(Array *a, size_t initialSize) For the initArray() function, assume that the user first allocates an Array struct and then calls insertArray as follows: struct Array myArray; initArray(&myArray, 10); 2- Suppose that you are asked to build a data structure that holds a set of strings. Modify the data structure you built in Problem 1 to store char *’s in the resizeable array instead of integers. Provide functions for making a set empty, inserting and removing an element into the set, and checking whether an element is in the set.
Make sure that your set does not include duplicate copies of the same string (char * with the same contents, use strcmp to compare strings). Define a struct MySet so that sets can be manipulated as in the following example. MySet *s; . . . initSet(s); insert(s, “Hello”); insert(s, “world”); insert(s, “Hello”); del(s, “world”); isInSet(s, “Hello”); // returns 1 if in set, otherwise 0 emptySet(s); // empties s by removing all the strings in it … 3- Redo problem 2, but this time implement a set of pointers to Account struct’s. typedef struct{ int acccountNo; char *nameOfHolder; double accountBalance; } Account; The set should make sure that there are no two Account struct’s with the same account number in the set. Assume there are two accounts A1 and A2 with different account numbers. The following manipulation should work. MySet *sAcc; . . . initSet(sAcc); insert(sAcc, &A1); insert(sAcc, &A2); insert(sAcc,&A1); del(sAcc, &A2); isInSet(sAcc, &A1); // returns 1 if in set, otherwise 0 emptySet(sAcc); // empties s by removing all accounts in it … 4- Write two functions, readArrayFromFile(Array *a, char *filename); writeArrayToFile(Array *a, char *filename); that store the contents of an Array struct in Problem 1 into a file and load the contents of the file into the Array. For readArrayFromFile, assume that the Array *a argument has 0 elements. Assume that the array with contents, 1, 2, 1, 3, 1 in an array of size 7 is represented with a file as follows: Array size 7 Number of array elements 5 Elements 1 2 1 3 1
Get Free Quote!
445 Experts Online