From 5f8b9d185637811489fd0279590b048160385892 Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:09:55 -0400 Subject: [PATCH 1/6] Assignment 8 --- assignment8.txt | 12 +++++++ floats.txt | 6 ++++ floats_mean | Bin 0 -> 8968 bytes floats_mean.c | 57 ++++++++++++++++++++++++++++++++ sort | Bin 0 -> 8864 bytes sort.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+) create mode 100644 assignment8.txt create mode 100644 floats.txt create mode 100755 floats_mean create mode 100644 floats_mean.c create mode 100755 sort create mode 100644 sort.c diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..c57b134 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,12 @@ +Ava N. +July 12, 2016 + +Assignment 8: + +1. The output is ABCabc123 + +2. 7 bytes will be lost. + +3. line 28. + +4. free(p) needs to be included at least in line 28. diff --git a/floats.txt b/floats.txt new file mode 100644 index 0000000..351a336 --- /dev/null +++ b/floats.txt @@ -0,0 +1,6 @@ +9.9 +2.6 +6.7 +8.1 +2.4 + diff --git a/floats_mean b/floats_mean new file mode 100755 index 0000000000000000000000000000000000000000..47f4ad4a19ecd712078c1c81af1921cafef1d894 GIT binary patch literal 8968 zcmeHMdu&_P89%o3aNE>&3yeMxwFcEnwYW)N%V2}+JldNoX+xVb=-OU8v7HzkJF~A# zTOn4Krdw88qHR>w7+N(LukDW^*i=ng)2^r$Leq)_Q>VQ$1T5h(3Sm{mMzh~{?)mKN z8+&7aOxhni(!JmLe&6Gq^WAff&v*RQwvG*MmrHPQi(3V8fm%&M)>{aDsbm$_B<2WB zEECs>8X(npG$|o!)tF8z*G#=i&jVV8r^#&rO>RwAm|kbWXu&GnL!#7Pv1qDl(Svms z%T$P<1jvs1DtwX!jaU^$2US1zD#~`mlHDR@w@B$T9akM=%HyN4(bKK`b-Oii#A`%E zt9%zJ=~C%>!;DdV=RC=RYO=y~tpy9FYHOaMy-n-u*RKubGN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{J?A$8W&g`U!f$!tcG=%!4tvgnUQrhdoX@j9#oAt;21D)h z_Zy$9N%6R{zfW?-m(%`Pi0tI;vD_b*zX<=P)r7k5uNoIBh9Csa-o7XSbN literal 0 HcmV?d00001 diff --git a/floats_mean.c b/floats_mean.c new file mode 100644 index 0000000..5139b11 --- /dev/null +++ b/floats_mean.c @@ -0,0 +1,57 @@ +//Ava N. +//First, create a .txt file with several floats, each on its own line. Then, write a program that reads values from this file, then calculates the mean and standard deviation of all the numbers it reads arr + +#include +#include +//#include +#include + +#define MAXSIZE 10 + +int main(){ + FILE *arrFile = fopen("floats.txt", "r"); + //float temp; + char arr[100]; + //float float_arr[100]; + float average, variance, std_deviation, sqr_num, sum = 0, sum1 = 0; + float a; + fgets(arr, sizeof(arr), arrFile); + int i; + //float mean; //mean of the floats + //float sum = 0; + float x[MAXSIZE]; + printf("Numbers in the array: \n"); + + for(i = 0; i<5; i++) { + a = atof(arr); + //a = float_arr[i]; + //sum += a; + printf("%lf\n", a); + x[i]=a; + fgets(arr, 100, arrFile); + } + + for (i = 0; i < 5; i++){ + sum = sum + x[i]; + } + + printf("Sum of numbers in array: %f\n", sum); + average = sum / (float)5; + +//Compute variance and standard deviation + for (i = 0; i < 5; i++){ + sqr_num = average - x[i]; + sum1 = sum1 + (sqr_num * sqr_num); + } + + variance = sum1 / 5; + std_deviation = sqrt(variance); + printf("Average/Mean: %.2f\n", average); + //printf("Variance of numbers in array: %.2f\n", variance); + printf("Standard Deviation: %.2f\n", std_deviation); + //mean = sum/5; + fclose(arrFile); + //printf("Here is the mean: %d \n", mean); + + return 0; +} diff --git a/sort b/sort new file mode 100755 index 0000000000000000000000000000000000000000..490a65683fe6f1dceac940621285321ff78c6eb3 GIT binary patch literal 8864 zcmeHMZ)_Y#6`#Ac9mjFcmo%Yn63D7F$SwG4Cr;wDK=*84vMx?y9H%W5XT84L^BuiE z=58-Ifr9BJfy1Pj7J;cis)|4<0x48g6+s(2jhm!WKrX0I1X5i@(Yd9NQ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UUZ?*%mhkge-p|T?_)|;xXTg-2HZ$R{4=>_w z>mJbhdiM@$8iRzv7>%Y)Bc+*fEf`BC47|<@CAHC5azu}5Av2juYkFp!1(Wfym;p(L zZ`+p6E!CB@a5NFscp{`32{Uz&g;RRm&_bDb{2-XvBn>ppGF41zc>i_`_;&@g!0sOC zpw(L{m)V4v*7LF5{$0J@pk=G!SBwC`C9W1wHOrgYGr+W+eS5n6ecGNKJN57P)8|th^uh!Kl_%6FAg}0MVY7s~np+j2l9?*j9Ps^kY2uNux7!Yyy>hwL5P7Bx8 zJ?w1-x?_4e&8L9vZKf<9 zp%P}*sWIkDBu&HT@9J$e^-&>>CNjQ}OcZYPqao%)DWaz%%ojSC0Dl%YQ&!CZNS&j} zM46yLUCM~*Xdp^sF_ZatczjUwjV3`fjdA$J*$YZZ{z`pDM9kMn2%L~)nOYOqvY_%O zgP;b@=<#R}`j9lyJs>zgd=PWygC!J)Wl_2Q|5F6N)35~;zYlc&r2kt{-a>v+*G}&R zVHrHIU2vjvD*azb7C155XGv~{g593Zv!-x(Jt+$AwC@4@4ahC1{b{$tHKzqY{l|L= zB+6qfiLu>Kz+8&#>3n=r0M84>I^Z~VvTqjn?Z7eDB6~VVPl*KVu+SssjNg8!z+8#! z>AbyK43xe()PK?=emB@-?nN@4%UguKA{ut~U%(8{QK(AxbbjYI@_^GgseR}8aeQds z1rHLP^Cv|6$1RxWx9H1he;;5N6YL)n2fcUvlA;zvHl{@BM?u!rNL*07?XY*Y{|wZ_X^XER zRHbt-{lC_!@06bdnV9EQ{UkSJ zNAhJTAV%Y-bLX%ybn1(6kU5h8K&-8ZSOv1b$$^tS@s&_dZQE6UDK6+5Nl{cfMBX;d keY^dX=>M#Eaqodj> +#include +#include + +int main (){ + + int array_size; //user defined array size for integers + int max_int; + int *input_arr; //input array for random numbers + int *asc_arr; //ascending array + int *des_arr; //descending array + int i; + int n; + int temp; + + printf("Input a positive integer for the size of the array:\n"); + scanf("%d", &array_size); + printf("Input maximum size of each integer in the array:\n"); + scanf("%d", &max_int); + + input_arr = malloc(array_size * sizeof(int)); + + printf("Random numbers in the array:\n"); + + for (i = 0; i < array_size; i++) { + input_arr[i] = rand() % max_int; // Random number from 0 - 99 + printf("%d\n",input_arr[i]); // Print random numbers in the array + } + + + asc_arr = input_arr; + des_arr = input_arr; + + +//ascending order + + for (i=0; i!=array_size-1; i++){ + for (n = i + 1; n != array_size; n++){ + if (asc_arr[n] > asc_arr[i]){ + temp = asc_arr[i]; + asc_arr[i] = asc_arr[n]; + asc_arr[n] = temp; + } + } + } + + printf("\nArray is sorted in ascending order:\n"); + for (i = 0; i < array_size; i++) { + + printf("%d\n",asc_arr[i]); // diagnostic for ascending + } + + +//descending order + for (i=0; i!=array_size-1; i++){ + for (n = i+1; n != array_size; n++){ + if (des_arr[n] < des_arr[i]){ + temp = des_arr[i]; + des_arr[i] = des_arr[n]; + des_arr[n] = temp; + } + } + } + + printf("\nArray is sorted in descending order:\n"); + for (i = 0; i < array_size; i++) { + + printf("%d\n",des_arr[i]); + } + + + + + + return 0; +} + + + + + From 38a301fb61853d79a8582b141da46335bbe815cc Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:11:11 -0400 Subject: [PATCH 2/6] Update floats.txt --- floats.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/floats.txt b/floats.txt index 351a336..8281dbd 100644 --- a/floats.txt +++ b/floats.txt @@ -3,4 +3,3 @@ 6.7 8.1 2.4 - From 1178668ee785d32d1a6a396e89a76dfcd2e7397a Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:12:38 -0400 Subject: [PATCH 3/6] Update floats_mean.c --- floats_mean.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/floats_mean.c b/floats_mean.c index 5139b11..3baef50 100644 --- a/floats_mean.c +++ b/floats_mean.c @@ -6,7 +6,7 @@ //#include #include -#define MAXSIZE 10 +#define MAXSIZE 10 //source: http://www.sanfoundry.com/c-program-mean-variance-standard-deviation/ int main(){ FILE *arrFile = fopen("floats.txt", "r"); @@ -19,7 +19,7 @@ int main(){ int i; //float mean; //mean of the floats //float sum = 0; - float x[MAXSIZE]; + float x[MAXSIZE]; //from same source as above printf("Numbers in the array: \n"); for(i = 0; i<5; i++) { @@ -45,11 +45,12 @@ int main(){ } variance = sum1 / 5; - std_deviation = sqrt(variance); + std_deviation = sqrt(variance); //this section is taken from the same source as the MAXSIZE printf("Average/Mean: %.2f\n", average); //printf("Variance of numbers in array: %.2f\n", variance); printf("Standard Deviation: %.2f\n", std_deviation); - //mean = sum/5; + + fclose(arrFile); //printf("Here is the mean: %d \n", mean); From 8b49d81b0820eff9051bc427ababce1f5b5fac21 Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:14:11 -0400 Subject: [PATCH 4/6] Update sort.c --- sort.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/sort.c b/sort.c index f702518..e773255 100644 --- a/sort.c +++ b/sort.c @@ -30,13 +30,10 @@ int main (){ printf("%d\n",input_arr[i]); // Print random numbers in the array } - asc_arr = input_arr; des_arr = input_arr; - //ascending order - for (i=0; i!=array_size-1; i++){ for (n = i + 1; n != array_size; n++){ if (asc_arr[n] > asc_arr[i]){ @@ -53,7 +50,6 @@ int main (){ printf("%d\n",asc_arr[i]); // diagnostic for ascending } - //descending order for (i=0; i!=array_size-1; i++){ for (n = i+1; n != array_size; n++){ @@ -64,21 +60,10 @@ int main (){ } } } - printf("\nArray is sorted in descending order:\n"); for (i = 0; i < array_size; i++) { printf("%d\n",des_arr[i]); } - - - - - return 0; } - - - - - From a8a7bcb57e5e93e8fb8763c2f80750704da9a3c6 Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:15:26 -0400 Subject: [PATCH 5/6] Update floats_mean.c --- floats_mean.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/floats_mean.c b/floats_mean.c index 3baef50..afe321f 100644 --- a/floats_mean.c +++ b/floats_mean.c @@ -10,16 +10,12 @@ int main(){ FILE *arrFile = fopen("floats.txt", "r"); - //float temp; char arr[100]; - //float float_arr[100]; float average, variance, std_deviation, sqr_num, sum = 0, sum1 = 0; float a; fgets(arr, sizeof(arr), arrFile); int i; - //float mean; //mean of the floats - //float sum = 0; - float x[MAXSIZE]; //from same source as above + float x[MAXSIZE]; //from same source as #define MAXSIZE 10 printf("Numbers in the array: \n"); for(i = 0; i<5; i++) { @@ -45,7 +41,7 @@ int main(){ } variance = sum1 / 5; - std_deviation = sqrt(variance); //this section is taken from the same source as the MAXSIZE + std_deviation = sqrt(variance); //this section of code is from the same source as #define MAXSIZE 10 printf("Average/Mean: %.2f\n", average); //printf("Variance of numbers in array: %.2f\n", variance); printf("Standard Deviation: %.2f\n", std_deviation); From eb9190fd5f1d388f31a9cff788f2ee2a7c4e2331 Mon Sep 17 00:00:00 2001 From: AvaEN Date: Wed, 13 Jul 2016 09:15:43 -0400 Subject: [PATCH 6/6] Update floats_mean.c --- floats_mean.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/floats_mean.c b/floats_mean.c index afe321f..2ad755b 100644 --- a/floats_mean.c +++ b/floats_mean.c @@ -46,9 +46,6 @@ int main(){ //printf("Variance of numbers in array: %.2f\n", variance); printf("Standard Deviation: %.2f\n", std_deviation); - fclose(arrFile); - //printf("Here is the mean: %d \n", mean); - return 0; }