Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions floats.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2.3
5.2
7.1
0.8
9.35
8.6
42 changes: 42 additions & 0 deletions readFloats.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <math.h>


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);
}
73 changes: 73 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>

/*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");
}