From 44beae26b498ad6fcd2d134c81b9c9a3cc91177b Mon Sep 17 00:00:00 2001 From: "bettinabenitez16@gmail.com" Date: Wed, 13 Jul 2016 09:45:03 +0100 Subject: [PATCH] here it is --- ReadFiles.c | 33 ++++++++++++++++++++++++++++++ assignment8.txt | 27 +++++++++++++++++++++++++ sort.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ trial.txt | 11 ++++++++++ 4 files changed, 124 insertions(+) create mode 100644 ReadFiles.c create mode 100644 assignment8.txt create mode 100644 sort.c create mode 100644 trial.txt diff --git a/ReadFiles.c b/ReadFiles.c new file mode 100644 index 0000000..dffae40 --- /dev/null +++ b/ReadFiles.c @@ -0,0 +1,33 @@ +//Bettina Benitez +#include +#include + +int main () { + FILE *openFile; + float c, total = 0; + int n = 0; + float arr[11]; + openFile = fopen("trial.txt", "r"); + while (fscanf(openFile, "%f", &c) != EOF) { + printf("%d %.1f\n", n, c); + arr[n] = c; + n++; + } + for (int i = 0; i < 11; i++) { + total += arr[i]; + } + float ave = total/11; + printf("\nAverage is: %.2f", ave); + for (int i = 0; i <11; i++) { + arr[i] -= ave; + arr[i] = sqrt(arr[i]); + } + float mean; + for (int i = 0; i < 11; i++) { + mean += arr[i]; + } + mean = mean/11; + float stdev = sqrt(mean); + printf("\nStandard Deviation is: %.2f", stdev); + return 0; +} diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..b188062 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,27 @@ +Bettina Benitez +Assignment 8 Part 2 + +1. CAN CRASH! there is a inifite while loop because of line 29. pointer p is not initialised either. + +2. 4 bytes + +3. +14 +18-20 +29-30 + +4. +for memory leak: +free(p); + +14: +char c[2] = {'0', '0'}; + +18-20: +initialise the pointer *p + +29-30: +maybe take out the while loop? I mean it's not really doing anyhting. + + + diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..4597aef --- /dev/null +++ b/sort.c @@ -0,0 +1,53 @@ +//Bettina Benitez +#include +#include +#include + +int main () { + + printf("How many numbers do you want? "); + int input; + scanf("%d", &input); //prompt + int *arrNormal = (int*) malloc(input*sizeof(int)); //malloc for the normal array + int *arrAscend = (int*) malloc(input*sizeof(int)); //malloc for the ascending array + int *arrDescend = (int*) malloc(input*sizeof(int)); //malloc for the descending array + + printf("Your array numbers are: \n"); + for (int i = 0; i < input; i++) { + arrNormal[i] = rand() % 101; //gets random number + printf("%d: %d\n", i + 1, arrNormal[i]); //prints the original + arrAscend[i] = arrNormal[i]; //stores in the ascending array + arrDescend[i] = arrNormal[i]; //stores in the descending array + } + + printf("Ascending Order: \n"); + for (int i = 0; i < input; i++) { + int temp; + for (int j = i + 1; j < input; j++) { + if (arrAscend[i] > arrAscend[j]) { //swap happens + temp = arrAscend[i]; //holds the first number + arrAscend[i] = arrAscend[j]; //puts the second number into the first spot + arrAscend[j] = temp; //the second number takes the first number + } + + } + printf("%d: %d\n", i + 1, arrAscend[i]); + } + + printf("Descending Order: \n"); + for (int i = 0; i < input; i++) { + int temp; + for (int j = i + 1; j < input; j++) { + if (arrDescend[i] < arrDescend[j]) { + temp = arrDescend[i]; + arrDescend[i] = arrDescend[j]; + arrDescend[j] = temp; + } + } + printf("%d: %d\n",i + 1, arrDescend[i]); + } + free(arrAscend); + free(arrDescend); + free(arrNormal); + return 0; +} diff --git a/trial.txt b/trial.txt new file mode 100644 index 0000000..ccacb9c --- /dev/null +++ b/trial.txt @@ -0,0 +1,11 @@ +10 +5 +4.5 +82.3 +5.8 +16 +0.5 +3 +10.3 +3.3 +8