Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 88 additions & 33 deletions arrays/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,61 @@ 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;
}


/*****
* 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
Expand All @@ -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];
}


Expand All @@ -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);
}
}


Expand All @@ -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");
Expand All @@ -146,19 +201,19 @@ void arr_print(Array *arr) {
#ifndef TESTING
int main(void)
{

Array *arr = create_array(1);

arr_insert(arr, "STRING1", 0);
arr_append(arr, "STRING4");
arr_insert(arr, "STRING2", 0);
arr_insert(arr, "STRING3", 1);
arr_print(arr);

arr_remove(arr, "STRING3");
arr_print(arr);

destroy_array(arr);

return 0;
}
#endif
#endif
Binary file added arrays/arrays.exe
Binary file not shown.