diff --git a/fizzbuzz/fizzbuzz.c b/fizzbuzz/fizzbuzz.c index 6cbb52518..b7428f238 100644 --- a/fizzbuzz/fizzbuzz.c +++ b/fizzbuzz/fizzbuzz.c @@ -7,10 +7,36 @@ by both 3 and 5, print "FizzBuzz". Increment the counter variable every time that nothing gets printed and return the counter. Don't forget to include newlines '\n' in your printf statements! + + javascript: + for (var i=1; i <= n; i++){ + if (i % 15 == 0) console.log("FizzBuzz"); + else if (i % 3 == 0) console.log("Fizz"); + else if (i % 5 == 0) console.log("Buzz"); + else console.log(i); +} + */ int fizzbuzz(int n) -{ - +{ int counter = 0; + for( int i = 1; i <=n; i++) { + if (i % 15 == 0) + { + printf("FizzBuzz\n"); + } + else if (i % 3 == 0) + { + printf("Fizz\n"); + } + else if (i % 5 == 0) + { + printf("Buzz\n"); + } + else { + counter ++; + } + } + return counter; } #ifndef TESTING diff --git a/malloc/malloc.c b/malloc/malloc.c index d46a8360c..f565e5aea 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -13,7 +13,15 @@ */ char *string_dup(char *src) { - + char *dup = malloc(string_length(src)); + char *point = dup; + while (*src != '\0') { + *dup = *src; + src++; + dup++; + } + *dup = '\0'; + return point; } /* @@ -43,7 +51,12 @@ void *mem_copy(void *dest, const void *src, int n) */ void *resize_memory(void *ptr, int old_size, int new_size) { - + char *pointer = (char *)ptr; + char *old = (char *)old_size; + for (int i = 0; i < new_size; i++) { + *(pointer + i) = *(old + i); + } + return ptr; } #ifndef TESTING diff --git a/pointers/pointers.c b/pointers/pointers.c index a4251532d..8debe2ff3 100644 --- a/pointers/pointers.c +++ b/pointers/pointers.c @@ -8,7 +8,9 @@ */ void swap(int* a, int* b) { - +int temp = *a; +*a = *b; +b = temp; } /* @@ -34,7 +36,12 @@ char *find_char(char *str, int c) */ void string_copy(char *x, char *y) { - + while (*y != '\0') { + *x = y; + x++; + y++; + } + *x ='\0'; } /* @@ -53,7 +60,7 @@ void string_copy(char *x, char *y) */ int string_compare(char *m, char *n) { - + } /* diff --git a/quicksort/quicksort.c b/quicksort/quicksort.c index bbfbff7f7..1cc6a5999 100644 --- a/quicksort/quicksort.c +++ b/quicksort/quicksort.c @@ -17,10 +17,35 @@ */ void quicksort(int *arr, int low, int high) { + int i, j, pivot, temp; + + if(low < high){ + pivot=low; + i = low; + j = high; + + while(i arr[pivot]) + j--; + if(i #include + /* Given a character array s, return the number of characters held inside it. @@ -9,9 +10,15 @@ */ int string_length(char s[]) { - + int len = 0; + for(int i = 0; s[i] != '\0'; i++ ) + { + len++; + } +return len; +// return printf("%d", len); +// return printf("%c", s[0]); } - /* Write a function that reverses the order of string s and outputs the reversed string to the input array rv. The rv array will have @@ -20,7 +27,15 @@ int string_length(char s[]) */ char *reverse_string(char rv[], char s[]) { - + int rvlen = string_length(s); + int arr = 0; + int i; + for (i= (rvlen -1); i>=0; i--) { + rv[arr] = s[i]; + arr++; + } + rv[rvlen] ='\0'; + return rv; } #ifndef TESTING diff --git a/structs/structs.c b/structs/structs.c index afad3cf93..7346e3b5f 100644 --- a/structs/structs.c +++ b/structs/structs.c @@ -8,9 +8,13 @@ Person should have the fields `name`, `age`, `height`, and `weight`. */ typedef struct Person { - + char *name; + int age; + int height; + int weight } Person; + /* Creates an instance of the Person struct that receives all the relevant pieces of data associated with a Person instance. @@ -21,7 +25,13 @@ typedef struct Person { */ Person *createPerson(char *name, int age, int height, int weight) { +Person *p = malloc(sizeof(Person)); +p->name = string_dup(name); +p->age =age; +p->height = height; +p->weight = weight; +return p; } /* @@ -30,7 +40,12 @@ Person *createPerson(char *name, int age, int height, int weight) */ void destroyPerson(Person *who) { - + if (who != NULL) { + if (who -> name !=NULL) { + free(who -> name); + } + free(who); + } } #ifndef TESTING diff --git a/structs/structs.h b/structs/structs.h index 5d00746f6..3b4849be7 100644 --- a/structs/structs.h +++ b/structs/structs.h @@ -12,4 +12,4 @@ Person *createPerson(char *name, int age, int height, int weight); void destroyPerson(Person *who); -#endif \ No newline at end of file +#endif