Complete the function main in file median.c to implement the computation of the median of a...

80.2K

Verified Solution

Question

Electrical Engineering

Complete the function main in file median.c to implement thecomputation of the median of a sequence of integers read fromstdin. The program should use realloc to allow an arbitrary numberof integers to be read into a dynamically allocated array. Everytime realloc is called, let the new size of the array be twice theold size plus 1. Look at function readLongLine in the slides forChapter 6 for an example of how to use realloc. The program shouldreturn -1 if anything other than an integer is read from stdin. Atest script is provided in test_median.sh.

Note that median.c uses qsort from the standard library to sortthe array of integers. We have not seen how to call qsort yet, sodon't worry if it still looks mysterious. It involves passing afunction pointer, which we shall cover shortly.

Here is the function

#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 ? '\n' : '');
}
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\n\", 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. Medianundefined.\n\");
    return -1;
}
/* Sort numbers in ascending order. */
qsort(numbers, cnt, sizeof(int), icompare);
printMedian(numbers, cnt);
free(numbers);
return 0;
}

How do I go about finishing this? This is in C language rememberthat.

Answer & Explanation Solved by verified expert
3.6 Ratings (549 Votes)
    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students