Can anyone explain to me where & how can I use this equation. I was told to make a program that reads 3 integer numbers and prints the greatest one using the following formula. But how?
$$\operatorname{Major} AB = \frac{a+b+abs(a-b)}{2}$$
Can anyone explain to me where & how can I use this equation. I was told to make a program that reads 3 integer numbers and prints the greatest one using the following formula. But how?
$$\operatorname{Major} AB = \frac{a+b+abs(a-b)}{2}$$
The equation you've given returns the maximum of two numbers; that is,
$$\max\{a, b\} = \frac{a + b + |a - b|}{2}$$
To see this, simply note that if $a \ge b$, we have
$$a + b + |a - b| = a + b + (a - b) = 2a$$
and if $a < b$,
$$a + b + |a - b| = a + b - (a - b) = 2b$$ Then to find the largest of three numbers, simply compare the first two, and then compare the result with the third number.
This is the max function
$$ max(a,b) = \frac{(a+b) + |a-b|}{2}$$
So $$ \begin{align} max(a,b,c) &= max(max(a,b),c) \\ &= \frac{max(a,b)+c+|max(a,b)-c|}{2}\\ &=\frac{\frac{(a+b) + |a-b|}{2}+c+|\frac{(a+b) + |a-b|}{2}-c|}{2}\ \end{align} $$
But this is probably not what your instructor wants.
He probably wants you to use write the function and then use is twice. Since you do not specify the programming language, I will use the C language. Works with C++ also. You may have to change it a bit for Java.
\begin{verbatim} #include <stdio.h> #include <math.h> int mx(int a, int b) { return (a+b+abs(a-b))/2; } void main() { int x,y,z,answer; scanf("%d %d %d", &x, &y, &z); // or any other way to read 3 numbers answer=mx(a,b); answer=mx(answer, c); printf("Max of %d, %d, %d is %d\n", a, b, c, answer); } \end{verbatim}