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
33 changes: 33 additions & 0 deletions ReadFiles.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Bettina Benitez
#include <stdio.h>
#include <math.h>

int main () {
FILE *openFile;
float c, total = 0;
int n = 0;
float arr[11];
openFile = fopen("trial.txt", "r");
while (fscanf(openFile, "%f", &c) != EOF) {
printf("%d %.1f\n", n, c);
arr[n] = c;
n++;
}
for (int i = 0; i < 11; i++) {
total += arr[i];
}
float ave = total/11;
printf("\nAverage is: %.2f", ave);
for (int i = 0; i <11; i++) {
arr[i] -= ave;
arr[i] = sqrt(arr[i]);
}
float mean;
for (int i = 0; i < 11; i++) {
mean += arr[i];
}
mean = mean/11;
float stdev = sqrt(mean);
printf("\nStandard Deviation is: %.2f", stdev);
return 0;
}
27 changes: 27 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Bettina Benitez
Assignment 8 Part 2

1. CAN CRASH! there is a inifite while loop because of line 29. pointer p is not initialised either.

2. 4 bytes

3.
14
18-20
29-30

4.
for memory leak:
free(p);

14:
char c[2] = {'0', '0'};

18-20:
initialise the pointer *p

29-30:
maybe take out the while loop? I mean it's not really doing anyhting.



53 changes: 53 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Bettina Benitez
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main () {

printf("How many numbers do you want? ");
int input;
scanf("%d", &input); //prompt
int *arrNormal = (int*) malloc(input*sizeof(int)); //malloc for the normal array
int *arrAscend = (int*) malloc(input*sizeof(int)); //malloc for the ascending array
int *arrDescend = (int*) malloc(input*sizeof(int)); //malloc for the descending array

printf("Your array numbers are: \n");
for (int i = 0; i < input; i++) {
arrNormal[i] = rand() % 101; //gets random number
printf("%d: %d\n", i + 1, arrNormal[i]); //prints the original
arrAscend[i] = arrNormal[i]; //stores in the ascending array
arrDescend[i] = arrNormal[i]; //stores in the descending array
}

printf("Ascending Order: \n");
for (int i = 0; i < input; i++) {
int temp;
for (int j = i + 1; j < input; j++) {
if (arrAscend[i] > arrAscend[j]) { //swap happens
temp = arrAscend[i]; //holds the first number
arrAscend[i] = arrAscend[j]; //puts the second number into the first spot
arrAscend[j] = temp; //the second number takes the first number
}

}
printf("%d: %d\n", i + 1, arrAscend[i]);
}

printf("Descending Order: \n");
for (int i = 0; i < input; i++) {
int temp;
for (int j = i + 1; j < input; j++) {
if (arrDescend[i] < arrDescend[j]) {
temp = arrDescend[i];
arrDescend[i] = arrDescend[j];
arrDescend[j] = temp;
}
}
printf("%d: %d\n",i + 1, arrDescend[i]);
}
free(arrAscend);
free(arrDescend);
free(arrNormal);
return 0;
}
11 changes: 11 additions & 0 deletions trial.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10
5
4.5
82.3
5.8
16
0.5
3
10.3
3.3
8