diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..e4b3706 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,25 @@ +Sean Kee + +1. CAN CRASH, the array is a char and contains ints. This wouldnt allow the program to compile in the first place. If it did compile, it would print out the alphabet. + +2. Valgrind will report that there are 52 bytes that ae "definitely lost". Since this program is intended to print out the alphabet, the malloc will allocate that much space for the alphabet string. Since there are 52 letters, capital and lowercase, it will allocate 52 bytes, one for each character. Since the memory is not freed before point "p" is pointed to NULL, that memory is lost, causing a memory leak. + +3. +LINE # ERROR +14 Trying to set an array with type char to integers. + +18 Did not provide a suitable condition for the for loop, which means it will go on forever, which would eventually eat up all the system memory and cause a crash + +29 + + +4. +14 char c[2] = { '0', '0' }; + +18 for (p = a; *p <= a; p++) { +} + +29, add do on line 17, to compliment the while function. + + + diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..a538a9f --- /dev/null +++ b/sort.c @@ -0,0 +1,87 @@ +/*Sean Kee*/ +/*Dynamic Allocation Sorting System v1.0.1*/ +#include +#include +#include + +void sortAsc(int *output, int size) { + int i; /*Number of Passes*/ + int j; + int temp; + int *ptr = output; + for (i = 0; i < size - 1; i++) { + for (j = 0; j < size - 1; j++) { + if (ptr[j] > ptr[j + 1]) { /*if the current number being checked is greater than the next one, swap places)*/ + temp = ptr[j]; + ptr[j] = ptr[j + 1]; + ptr[j + 1] = temp; + } + } + } +} + +int *sortDes(int *output, int size) { + int i; /*Number of Passes*/ + int j; + int temp; + int *ptr = output; + for (i = 0; i < size - 1; i++) { + for (j = 0; j < size - 1; j++) { + if (ptr[j] < ptr[j + 1]) { /*Same thing as ascending, just backwards*/ + temp = ptr[j]; + ptr[j] = ptr[j + 1]; + ptr[j + 1] = temp; + } + } + } +} + + +int main() { + int *original; /*Creates original pointer*/ + original = (int *) malloc(10 * sizeof(int)); /*Assigns Dynamic memory,10 ints of 4 bytes each*/ + + int size; /*User input for array size*/ + printf("Input the array size\n#: "); + scanf("%d", &size); + + original = (int *) realloc(original, size * sizeof(int)); /*Reallocates enough space based on the size inputted by the user*/ + + int *ascending = (int *) malloc(size * sizeof(int)); + int *descending = (int *) malloc(size * sizeof(int)); + + int i; /*counter*/ + + srand(time(NULL)); + + for(i = 0; i < size; i++) { /*Fills the original with random numbers between 1 and 100 */ + original[i] = (rand() % 100); + ascending[i] = original[i]; + descending[i] = original[i]; + } + + sortAsc(ascending, size); + sortDes(descending, size); + + printf("Original Numbers\n"); + for (i = 0; i < size; i++) { + printf("%d\n", original[i]); + } + printf("****\n"); + printf("Numbers in Ascending order\n"); + for (i = 0; i < size; i++) { + printf("%d\n", ascending[i]); + } + + printf("****\n"); + printf("Numbers in Descending order\n"); + for (i = 0; i < size; i++) { + printf("%d\n", descending[i]); + } + + free(original); /*Frees dynamic memory*/ + free(ascending); + free(descending); + + return 0; +}