Solved

implementing a function

Posted on 2000-03-01
3
161 Views
Last Modified: 2010-04-15
Implement the function find_median. The function find_median accepts three integer values as formal parameters and returns the median of the three numbers. The median of three numbers is the number that is greater than one of the numbers and less than the other number. The median of 10, 30 and 20 is 20. The median of 5, 5 and 5 is 5. The median of 12, 20 and 12 is 12. A call to find_median will take the form:
     
     median = find_median(num1,num2,num3);

i need this written out to implement this function...thanks in advance!
0
Comment
Question by:beachbumm
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2573062

0
 
LVL 16

Accepted Solution

by:
imladris earned 25 total points
ID: 2573063
Do it by sorting the values:

int find_median(int num1,int num2,int num3)
{    int tmp;

     if(num1>num2)
     {    tmp=num1;   /* swap num1 and 2*/
          num1=num2;
          num2=tmp;
     }
     /* Now if num3 is <= num1
        the median is num1 */
     if(num3<=num1)return(num1);

     /* if not returned, num3 > num1
        if it is also <= num2
        then num3 is the median  */
     if(num3<=num2)return(num3);

     /* if not returned, num3 > num2
        as well so ....  */
     return(num2);
}
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2573073
hmmm , beat to me,imladris :)
However , here it is:
int find_median(num1,num2,num3)
{
   int seri[3],temp;
   seri[0]=num1;
   seri[1]=num2;
   seri[2]=num3;
   for(int i=0;i<2;i++){
   if(seri[i]>=seri[i+1]){
   temp=seri[i+1];
   seri[i+1]=seri[i];
   seri[i]=temp;
   }
   }
   
   return seri[0]>seri[1]?seri[0]:seri[1];

}
   
Regards
W.Yinan  
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now