Skip to content
Open
Changes from all 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
71 changes: 53 additions & 18 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;

/* researching unions in C right now for stretch goal #2, looking at how the array could handle multiple data types */

/************************************
*
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 All @@ -47,13 +52,16 @@ void destroy_array(Array *arr) {
void resize_array(Array *arr) {

// Create a new element storage with double capacity

char **new_storage = calloc((arr->capacity * 2), sizeof(char *));
// Copy elements into the new storage

for(int i = 0; i < arr->count; i++) {
new_storage[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->elements = new_storage;
arr->capacity *= 2;
}


Expand All @@ -72,8 +80,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 All @@ -83,15 +97,23 @@ char *arr_read(Array *arr, 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) {
fprintf(stderr, "Index out of range!\n");
exit(0);
/* looking at how to do the above while using errno as well */
}
// 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+1] = arr->elements[i]; /* shifts each elements one position right */
}
// Copy the element and add it to the array

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

arr->count++;
}

/*****
Expand All @@ -101,11 +123,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 All @@ -117,12 +143,21 @@ 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.
for(int i = 0; i < arr->count; i++) {
if(arr->elements[i] == element) {
arr->elements[i] = NULL;
free(arr->elements[i]); /* when the desired element is found, null the value and free the memory block */
for(int x = i; x < arr->count; x++) {
arr->elements[x] = arr->elements[x + 1]; /* shifts the values after target occurrence */
}
}
}
// Don't forget to free its memory!

// Shift over every element after the removed element to the left one position

// Decrement count by 1

arr->count--;
}


Expand Down