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...
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.
Implement a function that reads numbers in from a file with one number per line and...
Implement a function that reads numbers in from a file with one number per line and outputs all the possible sums that can be formed by subsets of the numbers. For instance, if the numbers in the file are 1 2 4, then the output would be 0, 1, 2, 4, 3, 5, 6, 7. Note that 0 is in the output because it uses none of the numbers, while 7 is the sum of all of the numbers. //...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
C++ If you tried to compile a source file that doesn't contain a main() function, would...
C++ If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer. Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT