Skip to content
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

PR :: Andrew Jarrett #1

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ If you see this error:
```
git config --global core.autocrlf false
```

21 changes: 20 additions & 1 deletion fizzbuzz/fizzbuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,32 @@
*/
int fizzbuzz(int n)
{
int count = 0;
int i = 0;

while(i <= n) {
if(i % 5 == 0 && i % 3 == 0) {
printf("%s", "FizzBizz");
}
else if(i % 3 == 0) {
printf("%s", "Fizz");
}
else if(i % 5 == 0) {
printf("%s", "Buzz");
}
else {
count++;
}
i++;
}
return count;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of while loop. Good job.

}

#ifndef TESTING
int main(void)
{
fizzbuzz(20);
int result = fizzbuzz(20);
printf("%d", result);

return 0;
}
Expand Down
19 changes: 14 additions & 5 deletions malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

Do not use the `strdup` function from the standard library.
*/
char *string_dup(char *src)
{

char *string_dup(char *src) {
int len = string_length(src);
char *dupe = (char *) malloc(len * sizeof(char));
return strcpy(dupe, src);
// return memcpy(malloc(len * sizeof(char)), src, len);
}

/*
Expand All @@ -24,9 +26,16 @@ char *string_dup(char *src)

Do not use the `memcpy` function from the standard library.
*/
void *mem_copy(void *dest, const void *src, int n)
{
void *mem_copy(void *dest, const void *src, int n) {
char *original = src;
char *new = dest;

for (int i = 0; i < n; i++) {
new[i] = dest[i];
*(new + i) = *(original + i);
}

return new;
}

/*
Expand Down
56 changes: 56 additions & 0 deletions pointers/notes.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#+TITLE: Pointer Notes
#+AUTHOR: Andrew Jarrett

Date: <2018-10-28 Sun>

* Memory Allocation

** =malloc=

- Takes a parameter that indicates how much memory the user wants
- Finds a /contiguous/ spot in memory that satisfies the size criteria

#+BEGIN_SRC c
// create a pointer to a block of memory that
// is big enough to hold 100 integers
int *100_ints = malloc(100 * sizeof(int));

// then we can fill the empty block of memory:
for(int i = 0; i < 100; i++) {
,*(100_ints + 1) = rand();
}
#+END_SRC

** Passing Points as Parameters

We're used to passing parameters by value, where:

- parameter is a copy of value of a specific variable
- must return new value to retain changes outside scope of the function

This is good in a lot of scenarios, but what if for example the value is very large (takes up a lot of memory)?

In C we can pass pointers to functions.

This is called *passing by reference*

- parameter is a pointer
- cheaper if we are working with really large arrays/strings/objects (objects == structs?)
- *mutated thing retains those updates outside the scope of the function*

#+BEGIN_SRC c
// example - print out a long string
void print_long_str(int *str, int len) {
for(int i = 0; i < len; i++) {
printf("%c", *(str + 1));
}
}
#+END_SRC

*Note:* In the example above, shouldn't the function's first parameter be =char *str=?

In summation:

1. We can use /pointer arithmetic/ to iterate through all the values within a block of memory, starting with the first address
2. /Passing pointers to functions/ allows us to manipulate large pieces of data more efficiently while maintaining updates after a function exits

70 changes: 57 additions & 13 deletions pointers/pointers.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
address it's referring to) or the value at the address it's
pointing at.
*/
void swap(int* a, int* b)
{

void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

/*
Expand All @@ -19,22 +20,42 @@ void swap(int* a, int* b)

Do not use the `strchr` function from the standard library.
*/
char *find_char(char *str, int c)
{

// Old solution to find_char:
/* char *result = NULL; */
/* while(*str != '\0') { */
/* if(*str == c) { */
/* result = &(*str); */
/* break; */
/* } */
/* str++; */
/* } */
/* return result; */
char *find_char(char *str, int c) {
for (int i = 0; i < strlen(str); i++) {
if(str[i] == c) {
// we return a *pointer*, not str[i]
return str + i;
}
}
return NULL;
}

/*
Given an empty (NULL) character pointer x and a character pointer y,
copies the character contents of y over to x. Again, pointer arithmetic
is necessary here. Also, make sure x points to a null character at its
end to terminate it properly.

Do not just use the `strcpy` function from the standard library.
*/
void string_copy(char *x, char *y)
{

void string_copy(char *x, char *y) {
while(*y != '\0') {
*x = *y;
x++;
y++;
}
*x = '\0';
}

/*
Expand All @@ -51,9 +72,16 @@ void string_copy(char *x, char *y)

Do not just use the `strcmp` function from the standard library.
*/
int string_compare(char *m, char *n)
{

int string_compare(char *m, char *n) {
while(*m == *n) {
if(*m == '\0') {
return 0;
}
m++;
n++;
}
return *m - *n;
}

/*
Expand All @@ -64,9 +92,25 @@ int string_compare(char *m, char *n)

Do not use the `strstr` function from the standard library.
*/
char *find_string(char *haystack, char *needle)
{

char *find_string(char *haystack, char *needle) {
for(int i = 0; i < strlen(haystack); i++) {
if(haystack[i] == needle[0]) {
char *start = haystack + i;
int n = 1;
i++;

while(haystack[i] == needle[n]) {
i++;
n++;
if(needle[n] == '\0') {
return start;
}
}

}

}
return NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm able to break this by testing a string that starts at the beginning of world. hmmmmm.... :)

}

#ifndef TESTING
Expand Down
37 changes: 25 additions & 12 deletions queue/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,50 @@ typedef struct Queue {
instance of the Queue struct, and initializes all of the fields of the
struct. Also allocates memory for the queue's storage structure.
*/
Queue *createQueue(unsigned capacity)
{

Queue *createQueue(unsigned capacity) {
Queue *queue = malloc(sizeof(Queue));
queue->length = 0;
queue->capacity = capacity;
return queue;
}

/*
Adds the given item to the end of the queue. If the queue does
not have room, expand the queue's available storage so that it
does have room for the additional item.
*/
void enqueue(Queue *q, int item)
{

void enqueue(Queue *q, int item) {
if(q->length == q->capacity) {
printf("Queue at capacity!");
}
else {
q->storage[q->length] = item;
q->length++;
}
}

/*
Removes the item at the front of the queue. If the queue is empty,
this function should return -1.
*/
int dequeue(Queue *q)
{

int dequeue(Queue *q) {
if(q->length == 0) {
printf("Empty queue!");
return -1;
}
else {
q->length--;
return q->storage[0];
}
}

/*
Frees the memory used to hold the queue instance and its
associated storage.
*/
void destroyQueue(Queue *q)
{

void destroyQueue(Queue *q) {
free(q->storage);
free(q);
}


Expand Down
30 changes: 28 additions & 2 deletions quicksort/quicksort.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,35 @@

Do not just use the `qsort` function from the standard library.
*/
void quicksort(int *arr, int low, int high)
{


int partition(int *arr, int lo, int hi) {
int pivot = arr[hi];
int pivotLoc = lo;

for(int i = lo; i < hi; i++) {
if(arr[i] <= pivot) {
// swap takes 2, not 3 arguments :facepalm:
// also need to deref the values
swap(&arr[i], &arr[pivotLoc]);
pivotLoc++;

// swap(arr, pivotLoc, i);
// pivotLoc++;
}
}

// swap pivot and hi value
swap(&arr[pivotLoc], &arr[hi]);
return pivotLoc;
}

void quicksort(int *arr, int lo, int hi) {
if(lo < hi) {
int idx = partition(arr, lo, hi);
quicksort(arr, lo, idx-1);
quicksort(arr, idx+1, hi);
}
}

#ifndef TESTING
Expand Down
35 changes: 23 additions & 12 deletions strings/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

Do not just use the `strlen` function from the standard libary.
*/
int string_length(char s[])
{

int string_length(char s[]) {
int count = 0;
while(*s !='\0') {
count++;
s++;
}

return count;
}

/*
Expand All @@ -18,23 +23,29 @@ int string_length(char s[])
enough space for the reversed string. Don't forget to terminate
the reversed string with a null character. Return the rv array.
*/
char *reverse_string(char rv[], char s[])
{

char *reverse_string(char rv[], char s[]) {
int len = string_length(s);
for(int i = 0; i < len; i++) {
rv[i] = s[len-i-1];
}

rv[len] = '\0';
return rv;
}

#ifndef TESTING
int main(void)
{
char quote1[] = "Don't forget to be awesome";
char quote2[] = "a man a plan a canal panama";

char rv[512];
char quote1[] = "Don't forget to be awesome";
char quote2[] = "a man a plan a canal panama";

printf("The string 'Don't forget to be awesome' has %d characters.\n", string_length(quote1));
printf("The string 'a man a plan a canal panama' reversed is: '%s'\n", reverse_string(rv, quote2));
char rv[512];

return 0;
printf("The string 'Don't forget to be awesome' has %d characters.\n", string_length(quote1));
printf("The string 'a man a plan a canal panama' reversed is: '%s'\n", reverse_string(rv, quote2));

return 0;
}
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested. works 👍

Loading