forked from smuos/simpleStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.c
112 lines (95 loc) · 2.96 KB
/
mm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#define debug 0
#define FAILURE 1
// Declare Function
int numcmp (const void *a, const void *b);
float mean(int *num, int length);
float median(int *num, int length);
// Comparison function for qsort()
int numcmp (const void *a, const void *b) {
int x = *((int*) a);
int y = *((int*) b);
if (x > y) return 1;
if (x < y) return -1;
return 0;
}
//Mean functiont that outputs mean of an array
float mean(int *num, int length) {
// Initialization of vars
// sum -> sum of ints of array
// i -> num used for doing iteation
float sum = 0;
int i;
// For loop to add every number to sum
for(i=0; i<length;i++){
sum += num[i];
}
// Return mean
return sum/length;
}
// Median function that outputs median
float median(int *num, int length){
// If the length of array is even
if (length%2==0){
//return the average number between two median numbers
return (num[length/2-1]+num[length/2])/(float)2;
// If the length of array is odd
}else{
// Return the median
return num[length/2];
}
}
int main(int argc, char *argv[]) {
int i, length, *pt;
// Check for proper usage
if (argc < 2) {
fprintf(stderr, "%s: Aborting, not enough arguments.\n", argv[0]);
return (-1);
}
// Determine amount of numbers from argc
length = argc - 1;
#if debug
fprintf(stderr, "%s: DEBUG: %d numbers were passed.\n", argv[0], length);
#endif
// Allocate memory for array of number (and error check)
if ((pt = malloc(length * sizeof(int))) == NULL) {
fprintf(stderr, "%s: Could not allocate memory.\n", argv[0]);
}
// Read numbers into array
for (i = 0; i < length; i++) {
pt[i] = (int) strtol(argv[i+1], NULL, 10);
}
// Sort numbers
qsort(pt, length, sizeof(int), numcmp);
// Print out message
fprintf(stdout, "%s: Sorted output is: \n%s: ", argv[0], argv[0]);
// Initilaztion fork() for process
int rc = fork();
// If there is no arugment
if (rc < 0){
// Print filed output and exit with FAILED
fprintf(stderr, "Fork Failed %s\n", argv[0]);
exit(FAILURE);
// If the process is child
} else if (rc == 0){
//print out sorted number
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}
// Print child's pid and median
fprintf(stdout, "\n%s: This is child process(pid:%d)\n", argv[0], (int)getpid());
fprintf(stdout, "%s: The median is: %f\n", argv[0], median(pt, length));
// If the process is parent
} else if (rc > 0){
// Wait for child process
int wc = wait(NULL);
// Print parent's pid, mean and FIN
fprintf(stdout, "\n%s: This is parent prcesss(pid:%d) Waited for child process %d\n", argv[0], (int)getpid(), wc);
fprintf(stdout, "%s: The mean is: %f\n", argv[0], mean(pt, length));
fprintf(stdout, "%s: FIN. \n",argv[0]);
}
return 0;
}