From 3bb4e9da70a04161ca410bede2dacacea0caea13 Mon Sep 17 00:00:00 2001 From: k Date: Tue, 12 Jul 2016 10:43:21 -0400 Subject: [PATCH 1/4] File --- file.c | 27 +++++++++++++++++++++++++++ file.txt | 5 +++++ 2 files changed, 32 insertions(+) create mode 100644 file.c create mode 100644 file.txt diff --git a/file.c b/file.c new file mode 100644 index 0000000..7589d79 --- /dev/null +++ b/file.c @@ -0,0 +1,27 @@ +#include +#include +#include +int main() { + FILE *file = fopen("file.txt", "r"); + char c[20]; + int i,count=0; + float arr[5],sd=0,avg=0,sum=0; + //Initial Numbers: + printf("Your Numbers are: \n"); + for (i=0;i<5;i++){ + fgets(c,100,file); + arr[i]=atof(c); + printf("%f \n",arr[i]); + //sum + sum=sum+arr[i]; + count++; //n + } + avg=sum/count; + printf("The average is: %.3f \n", avg); + for (i=0;i<5;i++){ + sd=sd+(arr[i]-avg)*(arr[i]-avg); //standard deviation + } + sd=sqrt(sd/(count-1)); + printf("The standard deviation is: %.3f \n" , sd); //%.3 decimal places + fclose(file); +} diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..66679c7 --- /dev/null +++ b/file.txt @@ -0,0 +1,5 @@ +5 +29 +73 +10 +28 From 4a0f3e3a1b6176de3098f168262a918e3d58a359 Mon Sep 17 00:00:00 2001 From: k Date: Tue, 12 Jul 2016 11:37:40 -0400 Subject: [PATCH 2/4] Sort Array --- sort.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sort.c diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..66ea7bd --- /dev/null +++ b/sort.c @@ -0,0 +1,34 @@ +#include +#include +#include +int main() { + srand(time(NULL)); + int i,n,x; //input, comparison number + printf("Please input a positive integer for the number of variables: "); + scanf("%d",&i); //scanning for user input + int *arr=(int*)malloc(i*sizeof(int)); //creating array size of user input + printf("Your numbers: \n"); + for(n=0;narr[x]){ + store=arr[n]; + arr[n]=arr[x]; + arr[x]=store; + } // ascending order ^^ + } + } + printf("This is the ascending order: \n"); + for (n=0;n=0;n--){ + printf("%d \n", arr[n]); + }//print ascending order backwards, for descending +} From 9864785e62363b6e2e25cb8eab6afb7c456fbc40 Mon Sep 17 00:00:00 2001 From: k Date: Tue, 12 Jul 2016 11:42:58 -0400 Subject: [PATCH 3/4] Sort Array --- sort.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sort.c b/sort.c index 66ea7bd..bf1072e 100644 --- a/sort.c +++ b/sort.c @@ -29,6 +29,7 @@ int main() { } printf("This is the descending order: \n"); for(n=i-1;n>=0;n--){ - printf("%d \n", arr[n]); + printf("%d \n", arr[n]); }//print ascending order backwards, for descending + free(arr); } From 63e708ac388d1fdd1f2eddf0d2f8b4f53b09010a Mon Sep 17 00:00:00 2001 From: k Date: Wed, 13 Jul 2016 01:26:45 -0400 Subject: [PATCH 4/4] Assignment 8 --- assignment8.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 assignment8.txt diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..3039ff4 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,9 @@ +1. It can crash, because in line 18, the condition is a pointer instead. In line 29, there is a possible infinite while loop. +2. Valgrind is a program which checks memory leaks and the possible number of bytes that the user is wasting. If a pointer is not freed where allocated in the heap, the pointer will be definitely lost. Only one char pointer isnt freed, so 4 bytes is lost. +3. Line 27. +4. +Line 26: p=(char*)malloc(strlen(a),strlen(b),+1) = {" "," ", " "}; +Last line: free(p); +} + +--Oscar So--