To be written in C (not C++, C#, etc.) In this assignment, your smart array will
ID: 3886810 • Letter: T
Question
To be written in C (not C++, C#, etc.)
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!
SmartArray.h
This header file contains the struct definition and functional prototypes for the smart array functions you will be implementing. You should #include this file from your SmartArray.c file, like so:
#include "SmartArray.h"
Think of SmartArray.h as a public interface to the SmartArray data structure. It contains only the functions that the end user (i.e., the programmer (re-)using your code) should call in order to create and use a SmartArray. You do not want the end user to call your auxiliary functions directly, so you do not put those functional prototypes in SmartArray.h. That way, the end user doesn’t need to worry about all your auxiliary functions in order to use an SmartArray; everything just works. (And you don’t have to worry about the end user mucking everything up by accidentally calling auxiliary functions that he or she shouldn’t be messing around with!)
The basic struct you will use to implement the smart arrays (defined in SmartArray.h) is as follows:
typedef struct SmartArray { char **array; // pointer to array of strings int size; // number of elements in array int capacity; // length of array (maximum capacity) } SmartArray;
The SmartArray struct contains a double char pointer that can be used to set up a 2D char array (which is just an array of char arrays, otherwise known as an array of strings). array will have to be allocated dynamically at runtime. It will probably be the bane of your existence for the next week or so.
The struct also has size and capacity variables, which store the number of elements in the array (initially zero) and the current length (i.e., maximum capacity) of the array, respectively.
Function Requirements
In the source file you submit, SmartArray.c, you must implement the following functions. You may implement any auxiliary functions you need to make these work, as well. Please be sure the spelling, capitalization, and return types of your functions match these prototypes exactly. In this section, I often refer to malloc(), but you’re welcome to use calloc() or realloc() instead, as you see fit.
SmartArray *createSmartArray(int length);
Description: Dynamically allocate space for a new SmartArray. Initialize its internal array to be of length length or DEFAULT_INIT_LEN, whichever is greater. (DEFAULT_INIT_LEN is defined in SmartArray.h.) Properly initialize pointers in the array to NULL, and set the size and capacity members of the struct to the appropriate values.
Output: “-> Created new SmartArray of size <N>.” (Output should not include the quotes. Terminate the line with a newline character, ‘ ’. <N> should of course be the length of the new array, without the angled brackets.)
Returns: A pointer to the new SmartArray, or NULL if any calls to malloc() failed.
SmartArray *destroySmartArray(SmartArray *smarty);
Description: Free any dynamically allocated memory associated with the SmartArray struct and return NULL.
Returns: NULL pointer.
SmartArray *expandSmartArray(SmartArray *smarty, int length);
Description: Dynamically allocate a new array of length length. Copy the contents of smarty’s old array into the new array. Free any memory associated with the old smartyarray that is no longer in use, then set smartyarray to point to the newly created array. Be sure all pointers are properly initialized. Update the size and capacity of the SmartArray (if applicable).
Note: If length is less than or equal to smarty’s current array capacity, or if the smarty pointer is NULL, you should NOT modify the SmartArray at all. In that case, just return from the function right away without producing any output.
Output: “-> Expanded SmartArray to size <N>.” (Output should not include the quotes. Terminate the line with a newline character, ‘ ’. <N> should be the new length of the array, without the angled brackets. Do NOT produce any output if you the array is not expanded.)
Returns: A pointer to the SmartArray, or NULL if any calls to malloc() failed.
Explanation / Answer
SmartArray.h
#ifndef SmartArray_H_
#define SmartArray_H_
#define DEFAULT_INIT_LEN 5
typedef struct SmartArray
{
char **array; // pointer to array of strings
int size; // number of elements in array
int capacity; // length of array (maximum capacity)
} SmartArray;
SmartArray *createSmartArray(int length);
SmartArray *destroySmartArray(SmartArray *smarty);
SmartArray *expandSmartArray(SmartArray *smarty, int length);
#endif
SmartArray.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SmartArray.h"
SmartArray *createSmartArray(int length)
{
//Dynamically allocate space
SmartArray *S = malloc(sizeof(SmartArray));
int i;
S->array = NULL;
if (length >= DEFAULT_INIT_LEN)
{
S->array = malloc(sizeof(char*)*length);
if (S->array != NULL)
{
printf("SmartArray is created of size: %d ",length);
for (i=0; i<length; i++)
S->array[i] = NULL;
S->size = 0;
S->capacity = length;
return S;
}
else
{
printf(" malloc is failed!!! ");
return NULL;
}
}
else
{
S->array = malloc(sizeof(char*)*DEFAULT_INIT_LEN);
if (S->array != NULL)
{
printf("SmartArray is created of size: %d ",DEFAULT_INIT_LEN);
for (i=0; i<DEFAULT_INIT_LEN; i++)
S->array[i] = NULL;
S->size = 0;
S->capacity = DEFAULT_INIT_LEN;
return S;
}
else
{
printf(" malloc failed!!!! ");
return NULL;
}
}
}
SmartArray *destroySmartArray(SmartArray *smarty)
{
int i;
for (i=0; i<smarty->capacity; i++)
free(smarty->array[i]);
free(smarty->array);
smarty->size = 0;
smarty->capacity = 0;
free(smarty);
smarty = NULL;
return smarty;
//printf(" SmartArray is destroyed");
}
SmartArray *expandSmartArray(SmartArray *smarty, int length)
{
char **new_array = NULL;
int arr_size = sizeof(smarty->array) / sizeof(smarty->array[0]);
if (smarty == NULL)
return NULL;
if (length <= smarty->capacity)
return NULL;
new_array = realloc(smarty->array, sizeof(smarty->array[0]) * length);
if (new_array != NULL)
{
smarty->array = new_array;
printf("SmartArray expanded to size: %d", length);
smarty->size = arr_size;
smarty->capacity = length;
return smarty;
}
else
{
printf(" malloc failed!!!! ");
return NULL;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.