diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..310b92c --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "windowsSdkVersion": "10.0.17763.0", + "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "msvc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/ipch/17673dc72b18d19f/DBG.ipch b/.vscode/ipch/17673dc72b18d19f/DBG.ipch new file mode 100644 index 0000000..081d877 Binary files /dev/null and b/.vscode/ipch/17673dc72b18d19f/DBG.ipch differ diff --git a/.vscode/ipch/17673dc72b18d19f/mmap_address.bin b/.vscode/ipch/17673dc72b18d19f/mmap_address.bin new file mode 100644 index 0000000..862b842 Binary files /dev/null and b/.vscode/ipch/17673dc72b18d19f/mmap_address.bin differ diff --git a/.vscode/ipch/1c9eca09f9303331/ARRAYS_TESTS.ipch b/.vscode/ipch/1c9eca09f9303331/ARRAYS_TESTS.ipch new file mode 100644 index 0000000..440befc Binary files /dev/null and b/.vscode/ipch/1c9eca09f9303331/ARRAYS_TESTS.ipch differ diff --git a/.vscode/ipch/1c9eca09f9303331/mmap_address.bin b/.vscode/ipch/1c9eca09f9303331/mmap_address.bin new file mode 100644 index 0000000..862b842 Binary files /dev/null and b/.vscode/ipch/1c9eca09f9303331/mmap_address.bin differ diff --git a/.vscode/ipch/6a285fc2618f22fc/ARRAYS.ipch b/.vscode/ipch/6a285fc2618f22fc/ARRAYS.ipch new file mode 100644 index 0000000..0f69ac7 Binary files /dev/null and b/.vscode/ipch/6a285fc2618f22fc/ARRAYS.ipch differ diff --git a/.vscode/ipch/6a285fc2618f22fc/mmap_address.bin b/.vscode/ipch/6a285fc2618f22fc/mmap_address.bin new file mode 100644 index 0000000..862b842 Binary files /dev/null and b/.vscode/ipch/6a285fc2618f22fc/mmap_address.bin differ diff --git a/arrays/arrays.c b/arrays/arrays.c index 48c23e7..84f1f23 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -3,13 +3,13 @@ #include #include -typedef struct Array { - int capacity; // How many elements can this array hold? - int count; // How many states does the array currently hold? - char **elements; // The string elements contained in the array +typedef struct Array +{ + int capacity; // How many elements can this array hold? + int count; // How many states does the array currently hold? + char **elements; // The string elements contained in the (an array of pointers or char *'s) } Array; - /************************************ * * CREATE, DESTROY, RESIZE FUNCTIONS @@ -19,45 +19,60 @@ typedef struct Array { /***** * Allocate memory for a new array *****/ -Array *create_array (int capacity) { +Array *create_array(int capacity) +{ // Allocate memory for the Array struct + Array *arr = malloc(sizeof(Array)); // Set initial values for capacity and count + arr->capacity = capacity; + arr->count = 0; // Allocate memory for elements + arr->elements = malloc(capacity * sizeof(char *)); + return arr; } - /***** * Free memory for an array and all of its stored elements *****/ -void destroy_array(Array *arr) { - +void destroy_array(Array *arr) +{ // Free all elements + for (int i = 0; i < arr->count; i++) + { + free(arr->elements[i]); + } + free(arr->elements); // Free array - + free(arr); } /***** * Create a new elements array with double capacity and copy elements * from old to new *****/ -void resize_array(Array *arr) { - +void resize_array(Array *arr) +{ // Create a new element storage with double capacity - + int new_capacity = 2 * arr->capacity; + char **new_elements = malloc(new_capacity * sizeof(char *)); // Copy elements into the new storage + for (int i = 0; i < arr->count; i++) + { + new_elements[i] = arr->elements[i]; + } // Free the old elements array (but NOT the strings they point to) + free(arr->elements); // Update the elements and capacity to new values - + arr->capacity = new_capacity; + arr->elements = new_elements; } - - /************************************ * * ARRAY FUNCTIONS @@ -69,43 +84,66 @@ void resize_array(Array *arr) { * * Throw an error if the index is out of range. *****/ -char *arr_read(Array *arr, int index) { - +char *arr_read(Array *arr, int index) +{ // Throw an error if the index is greater than the current count - - // Otherwise, return the element at the given index + if (index >= arr->count) + { + printf("That index is beyond the current arrays size, you may try again or let the code perish"); + return NULL; + } + else + { + // Otherwise, return the element at the given index + return arr->elements[index]; + } } - /***** * Insert an element to the array at the given index *****/ -void arr_insert(Array *arr, char *element, int index) { - +void arr_insert(Array *arr, char *element, int index) +{ // Throw an error if the index is greater than the current count + if (index > arr->count) + { + printf("bruhh :dev 🐱‍👤"); + } // Resize the array if the number of elements is over capacity - + if (arr->capacity > arr->count) + { + 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 and add it to the array - + arr->elements[index] = strdup(element); // Increment count by 1 - + arr->count++; } /***** * Append an element to the end of the array *****/ -void arr_append(Array *arr, char *element) { +void arr_append(Array *arr, char *element) +{ // Resize the array if the number of elements is over capacity + if (arr->count > arr->capacity) + { + resize_array(arr); + } // or throw an error if resize isn't implemented yet. // Copy the element and add it to the end of the array - + char *new_element = strdup(element); + arr_insert(arr, new_element, arr->count); // Increment count by 1 - + arr->count++; } /***** @@ -114,33 +152,48 @@ void arr_append(Array *arr, char *element) { * * Throw an error if the value is not found. *****/ -void arr_remove(Array *arr, char *element) { - - // Search for the first occurence of the element and remove it. - // Don't forget to free its memory! +void arr_remove(Array *arr, char *element) +{ - // Shift over every element after the removed element to the left one position + // Search for the first occurrence of the element and remove it. + int count = arr->count; + int i; + int idx; + for (i = 0; i < count; i++) + { + // strcmp compares to string values until it reaches NULL or a value that is different + // if the values are identical, strcmp will return 0 + if (strcmp(element, arr->elements[i]) == 0) + { + idx = i; + // Don't forget to free its memory! + free(arr->elements[idx]); + } + // Shift over every element after the removed element to the left one position + arr->elements[idx] = arr->elements[idx + 1]; + } // Decrement count by 1 - + arr->count--; } - /***** * Utility function to print an array. *****/ -void arr_print(Array *arr) { +void arr_print(Array *arr) +{ printf("["); - for (int i = 0 ; i < arr->count ; i++) { + for (int i = 0; i < arr->count; i++) + { printf("%s", arr->elements[i]); - if (i != arr->count - 1) { + if (i != arr->count - 1) + { printf(","); } } printf("]\n"); } - #ifndef TESTING int main(void) { diff --git a/arrays/arrays.exe b/arrays/arrays.exe new file mode 100644 index 0000000..79b5a17 Binary files /dev/null and b/arrays/arrays.exe differ