From 1901fc4d2a85bb8e7ae242176c8219ac20d1a6f5 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Tue, 12 Jul 2016 01:43:12 -0400 Subject: [PATCH 1/3] in progress --- sort.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sort.c diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..2265670 --- /dev/null +++ b/sort.c @@ -0,0 +1,42 @@ +#include +#include +#include + +int main() +{ + srand(time(NULL)); + + size_t size; + printf("NUMBER: "); + scanf("%d", &size); + int* num_list = (int*) malloc(size*sizeof(int)); + printf("%d", (sizeof(num_list)/sizeof(num_list[0]))); + for (int i = 0; i < size; i++) + { + num_list[i] = rand(); + printf("%d, num_list[i]\n", num_list[i]); + } + int* as_sort = (int*) malloc((size+1)*sizeof(int)); + for (int i = 0; i < size; i++) + { + as_sort[i] = num_list[i]; + } + for (int i = size-1; i <= 0; i--) + { + int key = i; + int j = as_sort[i-1]; + while (i < j) + { + as_sort[i] = as_sort[j]; + i--; + j--; + for (int printi = 0; printi < size+1; i++) + { + printf("|%d", as_sort[printi]); + } + printf("\n"); + } + } + + return 0; +} From 21eecf76564379f98875c9785005022159cf00ec Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Tue, 12 Jul 2016 03:02:23 -0400 Subject: [PATCH 2/3] assignment 8 --- assignment8.txt | 9 +++++++++ averagestd.c | 34 ++++++++++++++++++++++++++++++++ averagestd.txt | 16 +++++++++++++++ sort.c | 52 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 assignment8.txt create mode 100644 averagestd.c create mode 100644 averagestd.txt diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..3d8b7ca --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,9 @@ +Christopher Liu +1. The output is ABCabc123. I get how the first part will make the arg capital, but the part after q = p + strlen(a) I don’t get. +2. I don’t know what valgrind is, but probably 7? +3. line 20-23 is redundant and reassigns the array I think. Not sure. +line 28 + +4. +line 14 char *c +line 28 [free(p)] \ No newline at end of file diff --git a/averagestd.c b/averagestd.c new file mode 100644 index 0000000..6297fed --- /dev/null +++ b/averagestd.c @@ -0,0 +1,34 @@ +/* Christopher Liu */ +#include +#include +#include + +int main() +{ + FILE * file_var = fopen("averagestd.txt", "r"); + float distance_sum = 0, total = 0; + int conv = 1, count = 0; + while (!feof(file_var)) + { + conv = fscanf(file_var, "%*f"); + count++; + } + fseek (file_var, 0, SEEK_SET); + float num_list[count]; + + for (int i = 0; i < count; i++) + { + fscanf(file_var, "%f", &num_list[i]); + total += num_list[i]; + } + float avg = total/count; + for (int i = 0; i < count; i++) + { + distance_sum += (float) fabs((avg - num_list[i])*(avg - num_list[i])); + } + float std = sqrt(distance_sum/count); + printf("AVG: %f, TOTAL: %f, COUNT: %d, STD: %f", avg, total, count, std); + + fclose(file_var); +} + diff --git a/averagestd.txt b/averagestd.txt new file mode 100644 index 0000000..ffb4ac2 --- /dev/null +++ b/averagestd.txt @@ -0,0 +1,16 @@ +90 +80 +99.4 +102.7 +39210.3 +30.0 +60.4 +70.8 +99.54 +102.3 +98.0 +97.6 +43.5 +60.79 +70.92 +84.3 \ No newline at end of file diff --git a/sort.c b/sort.c index 2265670..aa93d6a 100644 --- a/sort.c +++ b/sort.c @@ -1,42 +1,68 @@ +/* Christopher Liu */ #include #include #include +void printlist(int * list, int size) +{ + printf("LIST: "); + for (int i = 0; i < size; i++) + { + printf("%d ", list[i]); + } + printf("\n"); +} + int main() { srand(time(NULL)); - size_t size; + int size; printf("NUMBER: "); scanf("%d", &size); int* num_list = (int*) malloc(size*sizeof(int)); - printf("%d", (sizeof(num_list)/sizeof(num_list[0]))); for (int i = 0; i < size; i++) { - num_list[i] = rand(); - printf("%d, num_list[i]\n", num_list[i]); + num_list[i] = rand()%40; } int* as_sort = (int*) malloc((size+1)*sizeof(int)); + int* de_sort = (int*) malloc((size+1)*sizeof(int)); for (int i = 0; i < size; i++) { as_sort[i] = num_list[i]; + de_sort[i] = num_list[i]; } - for (int i = size-1; i <= 0; i--) + + for (int i = 1; i < size; i++) { - int key = i; - int j = as_sort[i-1]; - while (i < j) + int key = as_sort[i]; + int j = i-1; + while (key < as_sort[j]) { as_sort[i] = as_sort[j]; i--; j--; - for (int printi = 0; printi < size+1; i++) - { - printf("|%d", as_sort[printi]); - } - printf("\n"); } + as_sort[i] = key; + } + for (int i = 1; i < size; i++) + { + int key = de_sort[i]; + int j = i-1; + while (key > de_sort[j] && i > 0) + { + de_sort[i] = de_sort[j]; + i--; + j--; + } + de_sort[i] = key; } + printf("NUMLIST: "); + printlist(num_list, size); + printf("AS_SORT: "); + printlist(as_sort, size); + printf("DE_SORT: "); + printlist(de_sort, size); return 0; } From 4b410f3ecba33d68cd6b916c1a717553bd690708 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Wed, 13 Jul 2016 09:28:17 -0400 Subject: [PATCH 3/3] forgot free() --- sort.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sort.c b/sort.c index aa93d6a..8dad721 100644 --- a/sort.c +++ b/sort.c @@ -63,6 +63,9 @@ int main() printlist(as_sort, size); printf("DE_SORT: "); printlist(de_sort, size); + free(num_list); + free(de_sort); + free(as_sort); return 0; }