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
9 changes: 9 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1. It can crash, because in line 18, the condition is a pointer instead. In line 29, there is a possible infinite while loop.
2. Valgrind is a program which checks memory leaks and the possible number of bytes that the user is wasting. If a pointer is not freed where allocated in the heap, the pointer will be definitely lost. Only one char pointer isnt freed, so 4 bytes is lost.
3. Line 27.
4.
Line 26: p=(char*)malloc(strlen(a),strlen(b),+1) = {" "," ", " "};
Last line: free(p);
}

--Oscar So--
27 changes: 27 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
FILE *file = fopen("file.txt", "r");
char c[20];
int i,count=0;
float arr[5],sd=0,avg=0,sum=0;
//Initial Numbers:
printf("Your Numbers are: \n");
for (i=0;i<5;i++){
fgets(c,100,file);
arr[i]=atof(c);
printf("%f \n",arr[i]);
//sum
sum=sum+arr[i];
count++; //n
}
avg=sum/count;
printf("The average is: %.3f \n", avg);
for (i=0;i<5;i++){
sd=sd+(arr[i]-avg)*(arr[i]-avg); //standard deviation
}
sd=sqrt(sd/(count-1));
printf("The standard deviation is: %.3f \n" , sd); //%.3 decimal places
fclose(file);
}
5 changes: 5 additions & 0 deletions file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
5
29
73
10
28
35 changes: 35 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
srand(time(NULL));
int i,n,x; //input, comparison number
printf("Please input a positive integer for the number of variables: ");
scanf("%d",&i); //scanning for user input
int *arr=(int*)malloc(i*sizeof(int)); //creating array size of user input
printf("Your numbers: \n");
for(n=0;n<i;n++){ //randomly generated i amount of numbers
arr[n]=rand()%9999+1;
printf("%d \n", arr[n]);
}
// i is the size of array
int store=0; //temporarily stores an array number
for (n=0;n<i;n++){
for (x=n+1;x<i;x++){
if (arr[n]>arr[x]){
store=arr[n];
arr[n]=arr[x];
arr[x]=store;
} // ascending order ^^
}
}
printf("This is the ascending order: \n");
for (n=0;n<i;n++){
printf("%d \n", arr[n]);
}
printf("This is the descending order: \n");
for(n=i-1;n>=0;n--){
printf("%d \n", arr[n]);
}//print ascending order backwards, for descending
free(arr);
}