From 75e7cf9daaa06dcebe905450030c909c8b2cf3a5 Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Mon, 25 Feb 2019 12:41:06 -0800 Subject: [PATCH 01/12] initial commit --- arrays/arrays.c | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 48c23e7..5d95067 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 array } Array; - /************************************ * * CREATE, DESTROY, RESIZE FUNCTIONS @@ -19,32 +19,32 @@ 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 // Set initial values for capacity and count // Allocate memory for elements - } - /***** * Free memory for an array and all of its stored elements *****/ -void destroy_array(Array *arr) { +void destroy_array(Array *arr) +{ // Free all elements // Free array - } /***** * 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 @@ -53,11 +53,8 @@ void resize_array(Array *arr) { // Free the old elements array (but NOT the strings they point to) // Update the elements and capacity to new values - } - - /************************************ * * ARRAY FUNCTIONS @@ -69,18 +66,19 @@ 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 } - /***** * 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 @@ -91,13 +89,13 @@ void arr_insert(Array *arr, char *element, int index) { // Copy the element and add it to the array // Increment count by 1 - } /***** * 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 // or throw an error if resize isn't implemented yet. @@ -105,7 +103,6 @@ void arr_append(Array *arr, char *element) { // Copy the element and add it to the end of the array // Increment count by 1 - } /***** @@ -114,7 +111,8 @@ void arr_append(Array *arr, char *element) { * * Throw an error if the value is not found. *****/ -void arr_remove(Array *arr, char *element) { +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! @@ -122,25 +120,25 @@ void arr_remove(Array *arr, char *element) { // Shift over every element after the removed element to the left one position // Decrement count by 1 - } - /***** * 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) { From fbd9633d92e0ba6d6c1c2e30496abb00d039b28f Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Mon, 25 Feb 2019 12:44:29 -0800 Subject: [PATCH 02/12] created array --- arrays/arrays.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 5d95067..fded918 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -22,10 +22,15 @@ typedef struct Array Array *create_array(int capacity) { // Allocate memory for the Array struct + Array *newArray = malloc(sizeof(Array)); // Set initial values for capacity and count - + newArray->capacity = capacity; + newArray->count = 0; // Allocate memory for elements + newArray->elements = calloc(capacity, sizeof(char *)); + + return newArray; } /***** From b2bba2b357e57dc4b497cdc03618081cef3702de Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Mon, 25 Feb 2019 12:48:04 -0800 Subject: [PATCH 03/12] freed memory for an array and all its stored elements --- arrays/arrays.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arrays/arrays.c b/arrays/arrays.c index fded918..589d205 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -40,8 +40,16 @@ void destroy_array(Array *arr) { // Free all elements + if (arr->elements != NULL) + { + free(arr->elements); + } // Free array + if (arr != NULL) + { + free(arr); + } } /***** From ef762c1d7f81418528ca4539a283ddb8240d14f1 Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Mon, 25 Feb 2019 14:53:57 -0800 Subject: [PATCH 04/12] filled out arr_append --- arrays/arrays.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arrays/arrays.c b/arrays/arrays.c index 589d205..3b69b96 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -112,10 +112,16 @@ 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] = element; // Increment count by 1 + arr->count++; } /***** From 08b5d03b04a5b9ff3034b1e7d3420828ee10c936 Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Mon, 25 Feb 2019 15:03:07 -0800 Subject: [PATCH 05/12] filled in arr_read --- arrays/arrays.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 3b69b96..22e9458 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -83,8 +83,13 @@ char *arr_read(Array *arr, int index) { // Throw an error if the index is greater than the current count - + if (index > arr->count) + { + fprintf(stderr, "Index, out of range"); + exit(1); + } // Otherwise, return the element at the given index + return arr->elements[index] } /***** @@ -114,7 +119,9 @@ void arr_append(Array *arr, char *element) // or throw an error if resize isn't implemented yet. if (arr->count >= arr->capacity) { - resize_array(arr); + // resize_array(arr); + fprintf(stderr, "Index, out of range"); + exit(0) } // Copy the element and add it to the end of the array From 0a60b2870ed7e666d55b43d8e20d03225f8b1471 Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Tue, 26 Feb 2019 12:56:35 -0800 Subject: [PATCH 06/12] filled in arr_insert --- arrays/arrays.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arrays/arrays.c b/arrays/arrays.c index 22e9458..70432da 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -99,14 +99,30 @@ 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) + { + fprintf(stderr, "Index out of range"); + exit(1); + } // 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]; // repointing the new address to previous element's address + } // Copy the element and add it to the array + char *new_element = element; + arr->elements[index] = new_element; // Increment count by 1 + arr->count += 1; } /***** From 88d58a601523ea861880e5d236dbbfb5eb255d2a Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Tue, 26 Feb 2019 15:39:58 -0800 Subject: [PATCH 07/12] completed mvp & passed test --- arrays/arrays.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 70432da..88289c9 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -60,12 +60,23 @@ void resize_array(Array *arr) { // Create a new element storage with double capacity + char **doubleCapacity = calloc((arr->capacity * 2), sizeof(char *)); // Copy elements into the new storage + for (int i = 0; i < arr->count; i++) + { + doubleCapacity[i] = arr->elements[i]; + } // Free the old elements array (but NOT the strings they point to) + if (arr->elements != NULL) + { + free(arr->elements); + } // Update the elements and capacity to new values + arr->elements = doubleCapacity; // set elements array with newly, expanded array + arr->capacity *= 2; } /************************************ @@ -89,7 +100,7 @@ char *arr_read(Array *arr, int index) exit(1); } // Otherwise, return the element at the given index - return arr->elements[index] + return arr->elements[index]; } /***** @@ -137,7 +148,7 @@ void arr_append(Array *arr, char *element) { // resize_array(arr); fprintf(stderr, "Index, out of range"); - exit(0) + exit(0); } // Copy the element and add it to the end of the array @@ -156,7 +167,33 @@ void arr_append(Array *arr, char *element) void arr_remove(Array *arr, char *element) { - // Search for the first occurence of the element and remove it. + // Search for the first occurence of the element and remove it + int found; + for (int i = 0; i < arr->count; i++) + { // loop over to find matching element + if (arr->elements[i] == element) + { // if match occurs at index + found = i; // set temp variable to hold the index + arr->elements[i] = NULL; // WITHOUT -> ERROR: 0x1063a9fa8: pointer being freed was not allocated + // Don't forget to free its memory! + free(arr->elements[i]); + + // Shift over every element after the removed element to the left one position + for (int j = found; j < arr->count; j++) + { + arr->elements[j] = arr->elements[j + 1]; + } + + // Decrement count by 1 + arr->count--; + break; + } + else if (i == arr->count) + { // if the index has reached the end of the array + fprintf(stderr, "Value is not found."); + exit(1); + } + } // Don't forget to free its memory! // Shift over every element after the removed element to the left one position From 6f2e15d448e653f963845d095a0efc962830cbcb Mon Sep 17 00:00:00 2001 From: luiscmartinez Date: Tue, 26 Feb 2019 15:43:01 -0800 Subject: [PATCH 08/12] resized array if number of elements is over capacity --- arrays/arrays.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 88289c9..f7353bc 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -146,9 +146,9 @@ void arr_append(Array *arr, char *element) // or throw an error if resize isn't implemented yet. if (arr->count >= arr->capacity) { - // resize_array(arr); - fprintf(stderr, "Index, out of range"); - exit(0); + resize_array(arr); + // fprintf(stderr, "Index, out of range"); + // exit(0); } // Copy the element and add it to the end of the array From 522f75e1ed191dde70c6e4cfadbf8321947659bd Mon Sep 17 00:00:00 2001 From: luis Martinez Date: Wed, 27 Feb 2019 04:09:09 -0800 Subject: [PATCH 09/12] removed unwanted comments --- arrays/arrays.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index f7353bc..bcd0b69 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -147,8 +147,6 @@ void arr_append(Array *arr, char *element) if (arr->count >= arr->capacity) { resize_array(arr); - // fprintf(stderr, "Index, out of range"); - // exit(0); } // Copy the element and add it to the end of the array From c0fe3eb6701de99692c71a27c64098f123d9f429 Mon Sep 17 00:00:00 2001 From: luis Martinez Date: Wed, 27 Feb 2019 04:12:59 -0800 Subject: [PATCH 10/12] removed more unwanted comments --- arrays/arrays.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index bcd0b69..6c9f181 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -170,8 +170,8 @@ void arr_remove(Array *arr, char *element) for (int i = 0; i < arr->count; i++) { // loop over to find matching element if (arr->elements[i] == element) - { // if match occurs at index - found = i; // set temp variable to hold the index + { + found = i; arr->elements[i] = NULL; // WITHOUT -> ERROR: 0x1063a9fa8: pointer being freed was not allocated // Don't forget to free its memory! free(arr->elements[i]); From c65d1b4e666c8190fbb0cc86bedabcf65af39cd3 Mon Sep 17 00:00:00 2001 From: luis Martinez Date: Wed, 27 Feb 2019 04:14:31 -0800 Subject: [PATCH 11/12] removed more unwanted comments --- arrays/arrays.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 6c9f181..863dbc9 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -164,8 +164,6 @@ void arr_append(Array *arr, char *element) *****/ void arr_remove(Array *arr, char *element) { - - // Search for the first occurence of the element and remove it int found; for (int i = 0; i < arr->count; i++) { // loop over to find matching element @@ -181,8 +179,6 @@ void arr_remove(Array *arr, char *element) { arr->elements[j] = arr->elements[j + 1]; } - - // Decrement count by 1 arr->count--; break; } From f037ba5d6ca199ebe2a3af3e6db4085298757202 Mon Sep 17 00:00:00 2001 From: luis Martinez Date: Wed, 27 Feb 2019 04:15:31 -0800 Subject: [PATCH 12/12] removed more unwanted comments --- arrays/arrays.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arrays/arrays.c b/arrays/arrays.c index 863dbc9..021e974 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -183,7 +183,7 @@ void arr_remove(Array *arr, char *element) break; } else if (i == arr->count) - { // if the index has reached the end of the array + { fprintf(stderr, "Value is not found."); exit(1); }