8.1k views
2 votes
Write a function called median, that takes as parameter a full, sorted array of doubles and returns the median of the list. For a sorted list of odd length, the median is the middle value. For a sorted list of even length, the median is the average of the two middle values. Make an example function call in your main.

User Rita Azar
by
4.6k points

1 Answer

4 votes

Answer: Provided in the explanation section

Step-by-step explanation:

#include <stdio.h>

double median(double array[] , int n)

{

double med;

//if n is even then return average of middle of two element.

if(n%2==0)

med = ( array[(n-1)/2] + array[n/2] ) /2.0 ;

else

med = array[n/2];//if n is odd return middle element.

return (med);

}

int main()

{

double array[6] = {1.3,3.5,5,7.7,9.0,11};

//size store size of array.

int size=sizeof(array)/sizeof(double);

//call the median function.

double medvalue =median(array, size);

printf("median is : %lf\\",medvalue);

return 0;

}

//OUTPUT : median is : 6.350000

⇒ Provided is the Image of the code and output.

cheers i hope this helped !!

Write a function called median, that takes as parameter a full, sorted array of doubles-example-1
User Moby Disk
by
4.3k points