-
Notifications
You must be signed in to change notification settings - Fork 184
Luis Martinez #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luiscmartinez
wants to merge
12
commits into
bloominstituteoftechnology:master
Choose a base branch
from
luiscmartinez:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Luis Martinez #71
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
75e7cf9
initial commit
luiscmartinez fbd9633
created array
luiscmartinez b2bba2b
freed memory for an array and all its stored elements
luiscmartinez ef762c1
filled out arr_append
luiscmartinez 08b5d03
filled in arr_read
luiscmartinez 0a60b28
filled in arr_insert
luiscmartinez 88d58a6
completed mvp & passed test
luiscmartinez 6f2e15d
resized array if number of elements is over capacity
luiscmartinez 522f75e
removed unwanted comments
c0fe3eb
removed more unwanted comments
c65d1b4
removed more unwanted comments
f037ba5
removed more unwanted comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,13 @@ | |
| #include <string.h> | ||
| #include <errno.h> | ||
|
|
||
| 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,45 @@ 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 *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; | ||
| } | ||
|
|
||
|
|
||
| /***** | ||
| * Free memory for an array and all of its stored elements | ||
| *****/ | ||
| void destroy_array(Array *arr) { | ||
| void destroy_array(Array *arr) | ||
| { | ||
|
|
||
| // Free all elements | ||
| if (arr->elements != NULL) | ||
| { | ||
| free(arr->elements); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you'll want to loop over each element and free them one by one. |
||
|
|
||
| // Free array | ||
|
|
||
| if (arr != NULL) | ||
| { | ||
| 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 | ||
|
|
||
|
|
@@ -53,11 +66,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 +79,24 @@ 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 | ||
|
|
||
| if (index > arr->count) | ||
| { | ||
| fprintf(stderr, "Index, out of range"); | ||
| exit(1); | ||
| } | ||
| // 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 | ||
|
|
||
|
|
@@ -91,21 +107,28 @@ 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. | ||
| if (arr->count >= arr->capacity) | ||
| { | ||
| // resize_array(arr); | ||
| fprintf(stderr, "Index, out of range"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice error message. 👍 |
||
| exit(0) | ||
| } | ||
|
|
||
| // Copy the element and add it to the end of the array | ||
| arr->elements[arr->count] = element; | ||
|
|
||
| // Increment count by 1 | ||
|
|
||
| arr->count++; | ||
| } | ||
|
|
||
| /***** | ||
|
|
@@ -114,33 +137,34 @@ 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! | ||
|
|
||
| // 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) | ||
| { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of calloc instead of malloc here.