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).
int find(String str, double d);
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.
double calcAreaRect (double a, double b);
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.
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; }
Use #include <math.h>, 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).
int find(String str, double d);
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.
double calcAreaRect (double a, double b);
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.
There will be a function in it like this: double RectangleArea (double a, double b) { return a*b; }
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.
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.
A mathematical function declared in math.h: double pow (double b, double q);
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; }
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);
Your question isn't a question, but here is the answer: double divide (int p, int q);