Question

In: Electrical Engineering

Complete the function main in file median.c to implement the computation of the median of 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.

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

Here is the function

#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 ? '\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. Median undefined.\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 remember that.

Solutions

Expert Solution


Related Solutions

Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Nearby is a main() function demonstrating the use of the function earliest_word. Implement this function according...
Nearby is a main() function demonstrating the use of the function earliest_word. Implement this function according to the documentation given. My solution is about 25 lines plus some closing curly braces. #include <stdio.h> #include <stdlib.h> #include <string.h> char *earliest_word(char *fname, int *nwords); // Opens the file fname and reads words from it // until the end of file is reached then closes // the file. If the file to be opened doesn't // exist, returns NULL and sets nwords to...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function...
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function param so it can sort vector in increasing or decreasing order based on the comparator passed. the test code uses std::less comparator. #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT