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
12 changes: 12 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions floats.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
9.9
2.6
6.7
8.1
2.4
Binary file added floats_mean
Binary file not shown.
51 changes: 51 additions & 0 deletions floats_mean.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <math.h>

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

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;
}