Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

it is C language, not C++. #include <stdio.h> #include <stdlib.h> /* for realloc

ID: 673494 • Letter: I

Question

it is C language, not C++.

#include <stdio.h>
#include <stdlib.h> /* 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;
}

It needs to start at "Your code here."

Explanation / Answer

/*In place of Your code here*/

call function  printMedian(int const * numbers, int cnt) as follows:

printMedian()