Click here to Skip to main content
15,913,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I removed the empty tags, and guess what, there was no other content, meaning there was no question.
Posted
Updated 19-Feb-11 2:16am
v2

It may seem overkill, but the first thing to do is to sort them.

C
#define swap(x, y) do {int tmp; tmp = x; x = y; y = tmp; } while (0)
int getMiddleNumber(int a, int b, int c)
   {
   if (b < a) swap(b, a);
   if (c < b) swap(c, b);
   if (b < a) swap(b, a);
   return b;
   }

Then all you have to do, is use b - it is in the middle.
(this code pinched from here[^])


"Using macros like that (while convenient) is generally frowned on. You can't debug a macro, so you have to hope your code is right."


Agreed, but it's been quite a while since I used straight C and I couldn't remember where I put my QuickC disks... :laugh:

C#
void swap(int *x, int* y)
   {
   int tmp; 
   tmp = *x; 
   *x = *y; 
   *y = tmp;
   }
int getMiddleNumber(int a, int b, int c)
   {
   if (b &lt; a) swap(&b, &a);
   if (c &lt; b) swap(&c, &b);
   if (b &lt; a) swap(&b, &a);
   return b;
   }
 
Share this answer
 
v2
Comments
#realJSOP 19-Feb-11 8:40am    
Using macros like that (while convenient) is generally frowned on. You can't debug a macro, so you have to hope your code is right.
OriginalGriff 19-Feb-11 8:47am    
Good point - updated.
Sergey Alexandrovich Kryukov 19-Feb-11 22:16pm    
Good, after update it's 5.
--SA
Espen Harlinn 20-Feb-11 8:19am    
Good effort - and while some may frown, I sometimes miss the functionality of the c preprocessor in c# :)
I would not suggest sorting. In this case it might work well. But as the list increases, the complexity increases. The best way to use is to use two variables, one to store the largest, and the other to store the second largest. Then traverse through the array from first to last and compare its value with those two variables and at last you'll get the second largest variable in the second variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900