Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
52fc4ce
initial commit
ecarlstrom Feb 25, 2019
f57c9f3
first pass solution for create_array with memeory allocation and stru…
ecarlstrom Feb 25, 2019
6b6d15e
first pass for destroy_array
ecarlstrom Feb 25, 2019
7f4bc9f
fixed memory error; was accidentally freeing array's capacity, now fr…
ecarlstrom Feb 25, 2019
6fe0528
first pass at arr_read solution
ecarlstrom Feb 25, 2019
17a7c75
first pass solution for arr_append, testing to make sure today's MVP …
ecarlstrom Feb 25, 2019
70d3ecc
corrected syntax error (forgot to close a comment) that was causing t…
ecarlstrom Feb 25, 2019
461d139
day 1 array tests are passing, MVP complete. Will take a look at othe…
ecarlstrom Feb 25, 2019
ebd906f
changed memory allocation from malloc to calloc as Jonathan suggested…
ecarlstrom Feb 25, 2019
3cb4f3c
first ideas for resize_array, figuring out the best way to copy eleme…
ecarlstrom Feb 26, 2019
f6e8c87
fixed new storage creation and now copying elements into new storage …
ecarlstrom Feb 26, 2019
776af3e
first pass at arr_insert solution and adjusted test file to properly …
ecarlstrom Feb 26, 2019
7f9122b
fixed typo on line 110 (arr-elements -> arr->elements)
ecarlstrom Feb 26, 2019
0700028
replaced some other missing arrows in arr references, not sure why th…
ecarlstrom Feb 26, 2019
efbff23
first pass solution for arr_remove, doing some testing
ecarlstrom Feb 26, 2019
81b08e7
adjusted resize_array code to properly assign new storage space, test…
ecarlstrom Feb 26, 2019
de69c7f
working on stretch #2 and learning about unions
ecarlstrom Feb 26, 2019
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
31 changes: 23 additions & 8 deletions arrays/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef struct Array {
char **elements; // The string elements contained in the array
} Array;

/* initial commit, re-watching first part of video due to this morning's power outage */

/************************************
*
Expand All @@ -21,11 +22,15 @@ typedef struct Array {
*****/
Array *create_array (int capacity) {
// Allocate memory for the Array struct

Array *arr = malloc(sizeof(Array));
// Set initial values for capacity and count

/* using arrow to retrieve the structure member as per last week's questions */
arr->capacity = capacity;
arr->count = 0; /* initialize counter to 0 as normal */
// Allocate memory for elements
arr->elements = calloc(capacity, sizeof(char *));

return arr;
}


Expand All @@ -35,9 +40,9 @@ Array *create_array (int capacity) {
void destroy_array(Array *arr) {

// Free all elements

free(arr->elements);
// Free array

free(arr);
}

/*****
Expand Down Expand Up @@ -72,8 +77,14 @@ void resize_array(Array *arr) {
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.\n");
exit(0);
}
// Otherwise, return the element at the given index
else {
return arr->elements[index]; /* not sure this else is necessary, can probably just do return outside of any inner block */
}
}


Expand Down Expand Up @@ -101,11 +112,15 @@ 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.

/* will redo this with resizing when resize_array has been completed */
if(arr->capacity == arr->count) {
fprintf(stderr, "Array is full! Please free some space.\n");
exit(1);
}
// Copy the element and add it to the end of the array

arr->elements[arr->count] = element;
// Increment count by 1

arr->count++;
}

/*****
Expand Down
2 changes: 1 addition & 1 deletion arrays/tests/arrays_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ char *all_tests()
return NULL;
}

RUN_TESTS(all_tests);
RUN_TESTS(day_1_array_tests);