Skip to content

Commit

Permalink
Changed median function to cast the formula to find median as a doubl…
Browse files Browse the repository at this point in the history
…e, and moved fork() towards end of main.
  • Loading branch information
rikkustrife committed Sep 29, 2014
1 parent 8e60eee commit 850c27b
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ double median (int *p, int length)
if (length%2 == 0)
{
position = length/2;
medianValue = (p[position-1] + p[position])/2;
medianValue = (double) (p[position-1] + p[position])/2;
}
else
{
Expand All @@ -46,12 +46,6 @@ int main(int argc, char *argv[]) {

int i, length, *pt;
double medianValue, meanValue;
//calling fork, and checking if failed
int rc = fork();
if (rc < 0)
{
fprintf(stderr, "fork failed\n");
}

// Check for proper usage
if (argc < 2) {
Expand All @@ -71,14 +65,6 @@ int main(int argc, char *argv[]) {
exit(1);
}

if (rc == 0) //if child
{
medianValue = median(pt, length);
}
if (rc > 0) //if parent
{
meanValue = mean(pt, length);
}
// Read numbers into array
for (i = 0; i < length; i++) {
pt[i] = (int) strtol(argv[i+1], NULL, 10);
Expand All @@ -92,6 +78,25 @@ int main(int argc, char *argv[]) {
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}

//caling fork() and checking if call failed
int rc = fork();
if (rc < 0)
{
fprintf(stderr, "fork failed\n");
}

if (rc == 0) //if child
{
medianValue = median(pt, length);
// fprintf(stdout, "The median is: %.2f\n", medianValue);
}

if (rc > 0) //if parent
{
meanValue = mean(pt, length);
}

fprintf(stdout, "\n%s: FIN. \n", argv[0]);
return 0;
}

0 comments on commit 850c27b

Please sign in to comment.