Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

implementing a function

Posted on 2000-03-01
3
Medium Priority
?
170 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 100 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

715 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