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..8281dbd --- /dev/null +++ b/floats.txt @@ -0,0 +1,5 @@ +9.9 +2.6 +6.7 +8.1 +2.4 diff --git a/floats_mean b/floats_mean new file mode 100755 index 0000000..47f4ad4 Binary files /dev/null and b/floats_mean differ diff --git a/floats_mean.c b/floats_mean.c new file mode 100644 index 0000000..2ad755b --- /dev/null +++ b/floats_mean.c @@ -0,0 +1,51 @@ +//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 //source: http://www.sanfoundry.com/c-program-mean-variance-standard-deviation/ + +int main(){ + FILE *arrFile = fopen("floats.txt", "r"); + char arr[100]; + float average, variance, std_deviation, sqr_num, sum = 0, sum1 = 0; + float a; + fgets(arr, sizeof(arr), arrFile); + int i; + float x[MAXSIZE]; //from same source as #define MAXSIZE 10 + 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); //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); + + fclose(arrFile); + return 0; +} diff --git a/sort b/sort new file mode 100755 index 0000000..490a656 Binary files /dev/null and b/sort differ diff --git a/sort.c b/sort.c new file mode 100644 index 0000000..e773255 --- /dev/null +++ b/sort.c @@ -0,0 +1,69 @@ +//Ava N. +//Write a program sort.c that dynamically allocates an array of integers. The size of the array (the number of integers, not the byte size) should be read from the user using scanf(). You may assume that the user will input a positive integer. The elements of the array should be filled using the rand() function. After filling the array with random numbers, your program should then make a copy of the array, and sort the new array in ascending order. Then make a second copy of the original array and sort it in descending order. Finally your program should print out all three arrays. All three arrays should be allocated using malloc() library function. + +#include +#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; +}