Solved

implementing a function

Posted on 2000-03-01
3
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

630 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