Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A. Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program

A. Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program should use realloc to allow an arbitrary number of integers to be read into a dynamically allocated array. Every time realloc is called, let the new size of the array be twice the old size plus 1. Look at function readLongLine in the slides for Chapter 6 for an example of how to use realloc. The program should return -1 if anything other than an integer is read from stdin. A test script is provided in test_median.sh.

#include #include /* for realloc, free, and qsort */

/* * Read integers from stdin until EOF. Then * print the median of the numbers read. */

/* * Print the median of a sequence of sorted integers. */ void printMedian(int const * numbers, int cnt) { int i, halfWay; double median; /* debugging printout of sorted array */ for (i = 0; i < cnt; i++) { printf("%d%c", numbers[i], i == cnt-1 ? ' ' : ' '); } halfWay = cnt / 2; if (halfWay * 2 == cnt) { /* cnt is even: average two middle numbers */ median = 0.5 * (numbers[halfWay] + numbers[halfWay-1]); } else { /* cnt is odd: take middle number */ median = numbers[halfWay]; } printf("Median of %d integers: %g ", cnt, median); }

/* Integer comparison function for qsort. */ static int icompare(void const * x, void const * y) { int ix = * (int const *) x; int iy = * (int const *) y; return ix - iy; }

int main(void) { /* Size of allocated array. */ size_t size = 0; /* Number of ints stored in the array. Must be less * than or equal to size at all times. */ size_t cnt = 0; /* Pointer to dynamically allocated array. */ int * numbers = 0; /* Variables used by scanf. */ int num, ret; while ((ret = scanf("%d", &num)) != EOF) { /* Your code here. */ } if (!numbers) { printf("Read no integers. Median undefined. "); return -1; } /* Sort numbers in ascending order. */ qsort(numbers, cnt, sizeof(int), icompare); printMedian(numbers, cnt); free(numbers); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions