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 new file mode 100644 index 0000000..8dad721 --- /dev/null +++ b/sort.c @@ -0,0 +1,71 @@ +/* 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)); + + int size; + printf("NUMBER: "); + scanf("%d", &size); + int* num_list = (int*) malloc(size*sizeof(int)); + for (int i = 0; i < size; 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 = 1; i < size; i++) + { + int key = as_sort[i]; + int j = i-1; + while (key < as_sort[j]) + { + as_sort[i] = as_sort[j]; + i--; + j--; + } + 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); + free(num_list); + free(de_sort); + free(as_sort); + + return 0; +}