diff --git a/assignment8.txt b/assignment8.txt new file mode 100644 index 0000000..e4a3234 --- /dev/null +++ b/assignment8.txt @@ -0,0 +1,10 @@ +Yael Kelmer. + +1. The output is: ABCabc123 + +2. valgrind will report 7 bytes as "definitely lost". This is so, because after the function malloc() was used, the memory was not freed, so it will result in "definitely lost" memory. The memory allocated 7 bytes in the program. + +3. NONE + +4. line 32: free(p); +This change in code will fix the memory leak. diff --git a/floats.txt b/floats.txt new file mode 100644 index 0000000..1d1b781 --- /dev/null +++ b/floats.txt @@ -0,0 +1,6 @@ +2.3 +5.2 +7.1 +0.8 +9.35 +8.6 diff --git a/readFloats.c b/readFloats.c new file mode 100644 index 0000000..4e4e29f --- /dev/null +++ b/readFloats.c @@ -0,0 +1,42 @@ +/* Yael Kelmer. + This code calls reads in to another file that has a list of floats and calculates the mean and standard deviation of all of the floats.*/ + +#include +#include +#include + + +int main() { + + FILE *fileOfFloats; + fileOfFloats = fopen("floats.txt", "r"); + + char line[256]; + + float sum = 0; + float mean; + int numberOfFloats = 0; + while (fgets(line, sizeof(line), fileOfFloats)) { + float num = strtof(line, NULL); + sum += num; + numberOfFloats++; + } + mean = sum / numberOfFloats; + fclose(fileOfFloats); + + float deviation; + sum = 0; + float sqrOfDeviation; + fileOfFloats = fopen("floats.txt", "r"); + while (fgets(line, sizeof(line), fileOfFloats)) { + float num = strtof(line, NULL); + deviation = num - mean; + sqrOfDeviation = deviation * deviation; + sum += sqrOfDeviation; + } + float variance; + variance = sum / (numberOfFloats - 1); + float standardDev; + standardDev = sqrt (variance); + printf ("this is the standard deviation: %f\n", standardDev); +} diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..d13c5d6 --- /dev/null +++ b/sort.c @@ -0,0 +1,73 @@ +/*Yael Kelmer. + This code creates an array of random integers of the length that the user inputted. Then two functions sort this array of integers in an ascending and descending order and prints all three arrays out.*/ + +#include +#include +#include + +/*I found this code on how to use qsort(). I first thoroughly understood it and then used it for both of these following functions and calling them. http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort */ +int compAscend (const void * elem1, const void * elem2) { + int f = *((int*)elem1); + int s = *((int*)elem2); + if (f > s) return 1; + if (f < s) return -1; + return 0; +} + +int compDescend (const void * elem1, const void * elem2) { + int f = *((int*)elem1); + int s = *((int*)elem2); + if (f < s) return 1; + if (f > s) return -1; + return 0; +} + +int main() +{ + + printf("Please type the amount of integers you want in the array\n"); + int amountOfIntegers; + scanf ("%d", &amountOfIntegers); + + int *arrayOfInts = (int*) malloc (amountOfIntegers*sizeof(int)); + int i; + srand(time(NULL)); + for (i = 0; i < amountOfIntegers; i++) { + arrayOfInts[i] = rand() % 10; + } + + printf("This is the original array: "); + for (i = 0; i < amountOfIntegers; i++) { + printf ("%d ", arrayOfInts[i]); + } + printf ("\n"); + + + int *arrayAscend = (int*) malloc (amountOfIntegers*sizeof(int)); + int *arrayDescend = (int*) malloc (amountOfIntegers*sizeof(int)); + + for (i = 0; i < amountOfIntegers; i++) { + arrayAscend[i] = arrayOfInts[i]; + } + + for (i = 0; i < amountOfIntegers; i++) { + arrayDescend[i] = arrayOfInts[i]; + } + + qsort(arrayAscend, amountOfIntegers, sizeof (*arrayAscend), compAscend); + + printf("This is the array sorted in ascending order: "); + for (i = 0; i < amountOfIntegers; i++) { + printf ("%d ", arrayAscend[i]); + } + printf("\n"); + + qsort(arrayDescend, amountOfIntegers, sizeof (*arrayDescend), compDescend); + + printf ("This is the array sorted in descending order: "); + for (i = 0; i < amountOfIntegers; i++) { + printf ("%d ", arrayDescend[i]); + } + printf ("\n"); +} +