Solved

implementing a function

Posted on 2000-03-01
3
164 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

856 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