diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..40110c1 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,48 @@ +```C +1 #include +2 #include +3 #include +4 +5 int main(int argc, char **argv) +6 { +7 if (argc != 3) { +8 printf("usage: %s \n", argv[0]); +9 return 1; +10 } +11 +12 char *a = *++argv; /* gets first argument */ +13 char *b = *++argv; /* gets second argument */ +14 char c[2] = { 0, 0 }; +15 char *p; +16 char *q; +17 +18 for (p = a; *p; p++) { /* loop until the end of the first arg */ +19 if (’a’ <= *p && *p <= ’z’) /* If the first arg is a lowercase letter */ +20 *c = *p + ’A’ - ’a’; /* store the capitalized version in c[0] */ +21 else +22 *c = *p; /* otherwise store it in c[0] */ +23 printf("%s", c); /* print out c[0] with no newline*/ +24 } +/* lines 18 to 24 print out the first arg capitalized */ +25 +26 p = (char *)malloc(strlen(a) + strlen(b) + 1); /* allocate memory for an array big enough to store first and second arg and \0 */ + /* also, reassigns p */ +27 strcpy(p, a); /* copies a to P */ +28 q = p + strlen(a); /* moves q to the \0 of the first arg */ +29 while ((*q++ = *b++) != 0) +30 ; /* copies the second arg to after the first arg in p */ +31 printf("%s\n", p); /* prints the first arg followed by the second arg (no space) and then a newline */ +32 p = NULL; /* reassigns P AGAIN */ +33 return 0; +34 } +``` +line 23 outputs +Line 31 outputs + +line 26 leaks memory +line 32 leaks memory + +1) ABCabc123 +2) 7 (because the pointer will be deallocated, but the memory it points to will not be) +3) NONE +4) 32 free(p); diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..f64b6b9 --- /dev/null +++ b/sort.c @@ -0,0 +1,47 @@ +/* Harry Brickner +Generates an array and then sorts it */ +#include +#include +#include + +void bubbleSort(int array[], unsigned int length, int ascending){ + for(int loops = 1; loops < length; loops++){ + for(int i = 0; i < length - loops; i++){ + if((array[i] > array[i + 1]) == ascending){ + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } + } + } +} + +int main(){ + printf("How long should the array be?\n"); + unsigned int length; + scanf("%u", &length); + int* unsorted = malloc(length * sizeof(int)); + int* ascending = malloc(length * sizeof(int)); + int* descending = malloc(length * sizeof(int)); + srand(time(0)); + for(int i = 0; i < length; i++){ + unsorted[i] = rand() % 100; + ascending[i] = unsorted[i]; + descending[i] = unsorted[i]; + } + bubbleSort(ascending, length, 1); + bubbleSort(descending, length, 0); + for(int i = 0; i < length; i++) + printf("%d ", unsorted[i]); + printf("\n"); + for(int i = 0; i < length; i++) + printf("%d ", ascending[i]); + printf("\n"); + for(int i = 0; i < length; i++) + printf("%d ", descending[i]); + printf("\n"); + free(unsorted); + free(ascending); + free(descending); + return 0; +}