answersLogoWhite

0


Best Answer

There are several ways to implement this, however the basic principal is to sort the doubles in ascending order and return the middle element (in this case the 2nd element). The simplest implementation uses a 3-element array which is then sorted using an insertion sort, as shown in the following example.

#include<iostream>

#include<list>

double median(double& x, double& y, double& z)

{

// initialise the array

double a[3] = { x, y, z };

// sort the array

for(int i=1; i<3; ++i)

{

int hole=i;

int prev=hole-1;

double cur=a[hole];

while(hole && cur<a[prev])

{

a[hole]=a[prev];

--hole, --prev;

}

a[hole]=cur;

}

// return the middle element.

return( a[1] );

}

int main()

{

using namespace std;

double a, b, c;

a = 0.9;

b = 0.1;

c = 0.5;

cout<<"a="<<a<<endl;

cout<<"b="<<b<<endl;

cout<<"c="<<c<<endl;

cout<<"median="<<median(a,b,c)<<endl;

return(0);

}

Output:

a=0.9

b=0.1

c=0.5

median=0.5

It should be noted that when dealing with an even number of elements (such as 4 or 6), then there is no middle element, so you therefore need take the mean of the two middle elements instead. So if the sorted numbers are 0.1, 0.2, 0.5 and 0.9, then the median is the mean of 0.2 and 0.5, which is 0.35 (e.g., ( 0.2 + 0.5 ) / 2).

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program using function median that take three parameters of type double and returns the median of the three?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is C program to find power of number?

Use #include &lt;math.h&gt;, and then add to your code the function pow(x, y), which returns double and equals to x to power y (x and are both double).


Write a method header for a method named find that takes a String and a double as parameters and returns an integer?

int find(String str, double d);


When function returns a value the entire function call can be assigned a variable?

AnswerYes, it can. For instance, if your function returns double you can assign the function call to a variable of type double.AnswerNo, only the returned value, of course.


How would you write a function prototype for a function named calcAreaReact that accepts two arguments width and length and returns the area.?

double calcAreaRect (double a, double b);


How do you find the area of rectangle using function?

The area is the length times the width. That's the function. If you want to write a function in a computer language, you need two parameters. Just return the product of the two parameters. Example in Java: double rectangle_area(double length, double width) { return length * width; } I didn't test this, but that's the basic idea.


How do you write a c program to find area of rectangle using function?

There will be a function in it like this: double RectangleArea (double a, double b) { return a*b; }


How do you write a program in c to compute the power of a number without using the mathh library?

There is a function which can do it for you. You have to include math.h in headers. And then use the function pow(x, y) which returns a value of type double (x and y are double too).pow(x, y) = x to the power of y.


Explain use of pow in c language?

The general form of the pow function is: double pow(double base, double exp); Which returns the value of baseexp This function may be overloaded to allow for different parameter and return types, but the basic function is the same.


What is the Simple definition of power in c basic program?

A mathematical function declared in math.h: double pow (double b, double q);


Program in Java to illustrate the function overloading?

Below is an example of method overloading which returns the sum of two number types. Each method performs the calculation and returns a value in a different type. int sum(final int a, final int b) { return a + b; } long sum(final long a, final long b) { return a + b; } float sum(final float a, final float b) { return a + b; } double sum(final double a, final double b) { return a + b; }


How do you find the square root of a number in C?

You write a function that evaluates the square root of its argument and returns the result to the caller.You can also use the run-time library functions in math.h ...double sqrt (double x);double pow (double x, (double) 0.5);


Write the prototype of a function' divide' that takes two integer values and returns the quotient of double type.?

Your question isn't a question, but here is the answer: double divide (int p, int q);