diff --git a/arrays/arrays.c b/arrays/arrays.c index d691278..bd71456 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -21,11 +21,24 @@ typedef struct Array { *****/ Array *create_array (int capacity) { // Allocate memory for the Array struct + Array *arr = malloc(sizeof(Array)); + if (!arr) { + fprintf(stderr, "Error: Memory allocation failed for array.\n"); + exit(1); // Exit program if malloc fails + } // Set initial values for capacity and count + arr->capacity = capacity; + arr->count = 0; // Allocate memory for elements + arr->elements = malloc(capacity * sizeof(char *)); + if (!arr->elements) { + fprintf(stderr, "Error: Memory allocation failed for elements.\n"); + exit(1); // Exit if memory for elements cannot be allocated + } + return arr; } @@ -33,31 +46,36 @@ Array *create_array (int capacity) { * Free memory for an array and all of its stored elements *****/ void destroy_array(Array *arr) { - // Free all elements + for (int i = 0; i < arr->count; i++) { + free(arr->elements[i]); + } + // Free array elements + free(arr->elements); - // Free array - + // Free array structure + free(arr); } + /***** * Create a new elements array with double capacity and copy elements * from old to new *****/ void resize_array(Array *arr) { + int new_capacity = arr->capacity * 2; + char **new_elements = realloc(arr->elements, new_capacity * sizeof(char *)); + if (!new_elements) { + fprintf(stderr, "Error: Memory reallocation failed.\n"); + exit(1); + } - // Create a new element storage with double capacity - - // Copy elements into the new storage - - // Free the old elements array (but NOT the strings they point to) - - // Update the elements and capacity to new values - + // Update elements and capacity + arr->elements = new_elements; + arr->capacity = new_capacity; } - /************************************ * * ARRAY FUNCTIONS @@ -70,10 +88,14 @@ void resize_array(Array *arr) { * Throw an error if the index is out of range. *****/ char *arr_read(Array *arr, int index) { - - // Throw an error if the index is greater or equal to than the current count + // Throw an error if the index is out of range + if (index < 0 || index >= arr->count) { + fprintf(stderr, "Error: Index %d out of range.\n", index); + return NULL; // Return NULL if index is invalid + } // Otherwise, return the element at the given index + return arr->elements[index]; } @@ -83,48 +105,81 @@ char *arr_read(Array *arr, int index) { * Store the VALUE of the given string, not the REFERENCE *****/ void arr_insert(Array *arr, char *element, int index) { - - // Throw an error if the index is greater than the current count + // Throw an error if the index is out of range + if (index < 0 || index > arr->count) { + fprintf(stderr, "Error: Index %d out of range for insertion.\n", index); + return; + } // Resize the array if the number of elements is over capacity + if (arr->count == arr->capacity) { + resize_array(arr); + } // Move every element after the insert index to the right one position + for (int i = arr->count; i > index; i--) { + arr->elements[i] = arr->elements[i - 1]; + } - // Copy the element (hint: use `strdup()`) and add it to the array + // Copy the element (using `strdup()`) and add it to the array + arr->elements[index] = strdup(element); + if (!arr->elements[index]) { + fprintf(stderr, "Error: Memory allocation failed for element.\n"); + exit(1); + } // Increment count by 1 - + arr->count++; } + /***** * Append an element to the end of the array *****/ void arr_append(Array *arr, char *element) { - // Resize the array if the number of elements is over capacity - // or throw an error if resize isn't implemented yet. + if (arr->count == arr->capacity) { + resize_array(arr); + } // Copy the element and add it to the end of the array + arr->elements[arr->count] = strdup(element); + if (!arr->elements[arr->count]) { + fprintf(stderr, "Error: Memory allocation failed for element.\n"); + exit(1); + } // Increment count by 1 - + arr->count++; } + /***** - * Remove the first occurence of the given element from the array, - * then shift every element after that occurence to the left one slot. + * Remove the first occurrence of the given element from the array, + * then shift every element after that occurrence to the left one slot. * * Throw an error if the value is not found. *****/ void arr_remove(Array *arr, char *element) { + int found = 0; // Flag to check if the element is found + for (int i = 0; i < arr->count; i++) { + if (strcmp(arr->elements[i], element) == 0) { + found = 1; + free(arr->elements[i]); + + // Shift elements to the left + for (int j = i; j < arr->count - 1; j++) { + arr->elements[j] = arr->elements[j + 1]; + } + + arr->count--; + break; + } + } - // Search for the first occurence of the element and remove it. - // Don't forget to free its memory! - - // Shift over every element after the removed element to the left one position - - // Decrement count by 1 - + if (!found) { + fprintf(stderr, "Error: Element '%s' not found.\n", element); + } } @@ -136,7 +191,7 @@ void arr_print(Array *arr) { for (int i = 0 ; i < arr->count ; i++) { printf("%s", arr->elements[i]); if (i != arr->count - 1) { - printf(","); + printf(", "); } } printf("]\n"); @@ -146,7 +201,6 @@ void arr_print(Array *arr) { #ifndef TESTING int main(void) { - Array *arr = create_array(1); arr_insert(arr, "STRING1", 0); @@ -154,6 +208,7 @@ int main(void) arr_insert(arr, "STRING2", 0); arr_insert(arr, "STRING3", 1); arr_print(arr); + arr_remove(arr, "STRING3"); arr_print(arr); @@ -161,4 +216,4 @@ int main(void) return 0; } -#endif +#endif \ No newline at end of file diff --git a/arrays/arrays.exe b/arrays/arrays.exe new file mode 100644 index 0000000..b92eb1c Binary files /dev/null and b/arrays/arrays.exe differ