diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..41aa815 --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,14 @@ +Julia Tan + +1. CAN CRASH +- while loop does not check for whether the condition is true or false, instead sets a value +- *p in for loop is not a condition + +2. 4 bytes +Because that is the size of one char pointer. + +3. lines 18-24, 29-30 + +4. Between 32 and 33: free(*p); +14: single quotes around 0 +I'm not sure how to fix the while loop and for loop sorry diff --git a/fileIO.txt b/fileIO.txt new file mode 100644 index 0000000..4934959 --- /dev/null +++ b/fileIO.txt @@ -0,0 +1,5 @@ +4 +1 +2 +3 +4 diff --git a/file_IO.c b/file_IO.c new file mode 100644 index 0000000..e136a24 --- /dev/null +++ b/file_IO.c @@ -0,0 +1,33 @@ +#include +#include + +int main(){ + FILE *file; + float num = 0; + float list[4]; + int listNum = 0; + float total = 0; + float mean = 0; + file = fopen("fileIO.txt", "r"); + while (fscanf(file, "%f", &num) != EOF){ + printf("%.2f\n", num); + list[listNum] = num; + listNum ++; + } + printf("\n"); + for (int i = 1; i < 5; i ++){ // calculating total + total += list[i]; + } + mean = total / list[0]; // calculating mean + printf("The mean is: %.2f.\n", mean); + // calculating standard deviation + total = 0; + for (int i = 1; i < 5; i ++){ + float x = list[i]-mean; + total += (x*x); + } + mean = total / list[0]; + printf("Standard deviation is: %.2f.\n",(mean)); + fclose(file); + return 0; +} diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..99743dc --- /dev/null +++ b/sort.c @@ -0,0 +1,75 @@ +// Julia Tan +// sort.c + +#include +#include +#include + +int main(){ + int array_size; + int *array_1; + int *array_2; + int *array_3; + int i = 0; + srand(time(0)); + printf("How many numbers do you want?\n"); + scanf("%d", &array_size); + array_1 = (int *)malloc(sizeof(int)*array_size); + array_2 = (int *)malloc(sizeof(int)*array_size); + array_3 = (int *)malloc(sizeof(int)*array_size); + + // check if there is enough memory + if ((array_1 = (int *)malloc(sizeof(int)*array_size)) == NULL) + printf("array_1 cannot be allocated memory."); + if ((array_1 = (int *)malloc(sizeof(int)*array_size)) == NULL) + printf("array_2 cannot be allocated memory."); + if ((array_1 = (int *)malloc(sizeof(int)*array_size)) == NULL) + printf("array_3 cannot be allocated memory."); + + // add random numbers into arrays + for (i = 0; i < array_size; i++){ + array_1[i] = rand(); + array_2[i] = array_1[i]; + array_3[i] = array_1[i]; + printf("array[%d] = %d\n", i, array_1[i]); + } + + // sort numbers by ascending or descending + for (i = 0; i < array_size; i++){ + for (int j = i + 1; j < array_size; j++){ //ascending + if (array_2[j] < array_2[i]){ + int temp = array_2[i]; + array_2[i] = array_2[j]; + array_2[j] = temp; + } + } + } + for (i = 0; i < array_size; i++){ + for (int j = i + 1; j < array_size; j++){ //descending + if (array_3[j] > array_3[i]){ + int temp = array_3[i]; + array_3[i] = array_3[j]; + array_3[j] = temp; + } + } + } + // print sorted array + printf("First array, no sorting:\n"); + for (i = 0; i < array_size; i++){ + printf("%d\n",array_1[i]); + } + printf("Second array, ascending:\n"); + for (i = 0; i < array_size; i++){ + printf("%d\n", array_2[i]); + } + printf("Third array, descending:\n"); + for (i = 0; i < array_size; i++){ + printf("%d\n", array_3[i]); + } + + free(array_1); + free(array_2); + free(array_3); + printf("%d",'a'); + return 0; +}