From 29ad9560a2c7263a283f4d38e5d9bc6eab9709a2 Mon Sep 17 00:00:00 2001 From: MrSkee Date: Mon, 11 Jul 2016 22:14:53 -0400 Subject: [PATCH 1/5] not yet operational --- sort.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 sort.c diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..27afa04 --- /dev/null +++ b/sort.c @@ -0,0 +1,64 @@ +/*Sean Kee*/ +/*Dynamic Allocation Sorting System v1.0.0*/ +#include +#include +#include + +void *sortAsc(const int *output) { + int i; + int j; + int *ptr = output; + for (i = 0; i < sizeof(output)/4; i++) { + for (j = 0; j < sizeof(output)/4; j++) { + if (*(output + j) <= *(output + i)) + *ptr = *(output + j); + } + ptr++; + } +} + +int *sortDes(int *output) { +} + +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 = 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); +// descending = sortDes(descending); + + printf("Original Numbers\n"); + for(i = 0; i < size; i++) { + printf("%d\n", *(original + i)); + } + printf("****\n"); + printf("Numbers in Ascending order"); + for(i = 0; i < size; i++) { + printf("%d\n", *(ascending + i)); + } + + free(original); /*Frees dynamic array 'original*/ + free(ascending); + free(descending); + + return 0; +} From 271521bb00f950ec54028e0d1459d8635de7d20e Mon Sep 17 00:00:00 2001 From: Skee Date: Tue, 12 Jul 2016 09:48:51 -0400 Subject: [PATCH 2/5] Sorting System finised, created my own sorting algorithm --- sort.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/sort.c b/sort.c index 27afa04..192ba2b 100644 --- a/sort.c +++ b/sort.c @@ -4,22 +4,39 @@ #include #include -void *sortAsc(const int *output) { - int i; +void sortAsc(int *output, int size) { + int i; /*Number of Passes*/ int j; + int temp; int *ptr = output; - for (i = 0; i < sizeof(output)/4; i++) { - for (j = 0; j < sizeof(output)/4; j++) { - if (*(output + j) <= *(output + i)) - *ptr = *(output + j); + 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; + } } - ptr++; } } -int *sortDes(int *output) { +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*/ @@ -27,36 +44,42 @@ int main() { 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*/ - original = 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); -// descending = sortDes(descending); + sortAsc(ascending, size); + sortDes(descending, size); printf("Original Numbers\n"); - for(i = 0; i < size; i++) { + for (i = 0; i < size; i++) { printf("%d\n", *(original + i)); } printf("****\n"); - printf("Numbers in Ascending order"); - for(i = 0; i < size; i++) { + printf("Numbers in Ascending order\n"); + for (i = 0; i < size; i++) { printf("%d\n", *(ascending + i)); } - free(original); /*Frees dynamic array 'original*/ + 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); From 8e6ff827166105f83223a64a9c0f440a4cd4c3b3 Mon Sep 17 00:00:00 2001 From: Skee Date: Tue, 12 Jul 2016 10:18:02 -0400 Subject: [PATCH 3/5] v1.0.1 changed the way the arrays are referred to --- sort.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/sort.c b/sort.c index 192ba2b..a538a9f 100644 --- a/sort.c +++ b/sort.c @@ -1,5 +1,5 @@ /*Sean Kee*/ -/*Dynamic Allocation Sorting System v1.0.0*/ +/*Dynamic Allocation Sorting System v1.0.1*/ #include #include #include @@ -11,10 +11,10 @@ void sortAsc(int *output, int size) { 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; + 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; } } } @@ -27,10 +27,10 @@ int *sortDes(int *output, int size) { 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; + if (ptr[j] < ptr[j + 1]) { /*Same thing as ascending, just backwards*/ + temp = ptr[j]; + ptr[j] = ptr[j + 1]; + ptr[j + 1] = temp; } } } @@ -55,9 +55,9 @@ int main() { 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); + original[i] = (rand() % 100); + ascending[i] = original[i]; + descending[i] = original[i]; } sortAsc(ascending, size); @@ -65,18 +65,18 @@ int main() { printf("Original Numbers\n"); for (i = 0; i < size; i++) { - printf("%d\n", *(original + 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("%d\n", ascending[i]); } printf("****\n"); printf("Numbers in Descending order\n"); for (i = 0; i < size; i++) { - printf("%d\n", *(descending + i)); + printf("%d\n", descending[i]); } free(original); /*Frees dynamic memory*/ From f92830d877dafc9df24946729729eae4ebc03a2d Mon Sep 17 00:00:00 2001 From: Skee Date: Tue, 12 Jul 2016 19:02:46 -0400 Subject: [PATCH 4/5] assignment8.txt --- assignment8.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 assignment8.txt diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..2efdfd7 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,3 @@ +Sean Kee + + From 9a677c24a0b15e99818b142c76a63d71a1b7224a Mon Sep 17 00:00:00 2001 From: MrSkee Date: Tue, 12 Jul 2016 20:06:03 -0400 Subject: [PATCH 5/5] Assignment8 --- assignment8.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/assignment8.txt b/assignment8.txt index 2efdfd7..e4b3706 100644 --- a/assignment8.txt +++ b/assignment8.txt @@ -1,3 +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. + +