Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

implementing a function

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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

886 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