
Patrick B. answered 06/25/19
Math and computer tutor/teacher
I would just bubble sort the three....
#include <stdio.h>
Swap( int * X, int * Y)
{
int temp = *Y;
*Y = *X;
*X=temp;
}
BubbleSort3( int* A)
{
if (A[0]>A[1]) { Swap(&A[0],&A[1]); }
if (A[1]>A[2]) { Swap(&A[1],&A[2]); }
if (A[0]>A[1]) { Swap(&A[0],&A[1]); }
}
int main()
{
int A[3];
//change these to whatever you like; Or you can have the use INPUT the values, read them from file, input from command line, etc.
A[0]=13;
A[1]=7;
A[2]=10;
BubbleSort3(A);
printf(" the median is %d \n",A[1]);
}
===================================================
Sorry you asked for Java.... but still the same reasoning and logic...
bubble sort the three integers and return A[1] as the median...
Here's some quick and dirty java
class MedianOf3
{
public int getMedian(int [] A)
{
int temp;
if (A[0]>A[1])
{
temp = A[1];
A[1] = A[0];
A[0] = temp;
}
if (A[1]>A[2])
{
temp = A[2];
A[2] = A[1];
A[1] = temp;
}
if (A[0]>A[1])
{
temp = A[1];
A[1] = A[0];
A[0] = temp;
}
return(A[1]);
}
public static void main(String args[])
{
MedianOf3 myMedian = new MedianOf3();
int A[] = {7,13,10};
System.out.println(" Median is " + myMedian.getMedian(A));
}
}