Max / Min

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1715
Joined: Sat Feb 23, 2008 11:15 pm

Max / Min

Post by Kjell » Mon Dec 19, 2011 6:58 pm

:arrow:

Since they are kind of a pain to write ( the large ones at least ), here are unrolled minified ( so you won't have to scroll so much ) functions for finding the greatest / smallest number out of a "collection".

Code: Select all

int max2(int A, int B)
{
  return A>B?A:B;
}

int min2(int A, int B)
{
  return A<B?A:B;
}

int max3(int A, int B, int C)
{
  return A>B?A>C?A:C:B>C?B:C;
}

int min3(int A, int B, int C)
{
  return A<B?A<C?A:C:B<C?B:C;
}

int max4(int A, int B, int C, int D)
{
  return A>B?A>C?A>D?A:D:C>D?C:D:B>C?B>D?B:D:C>D?C:D;
}

int min4(int A, int B, int C, int D)
{
  return A<B?A<C?A<D?A:D:C<D?C:D:B<C?B<D?B:D:C<D?C:D;
}

int max5(int A, int B, int C, int D, int E)
{
  return A>B?A>C?A>D?A>E?A:E:D>E?D:E:C>D?C>E?C:E:D>E?D:E:B>C?B>D?B>E?B:E:D>E?D:E:C>D?C>E?C:E:D>E?D:E;
}

int min5(int A, int B, int C, int D, int E)
{
  return A<B?A<C?A<D?A<E?A:E:D<E?D:E:C<D?C<E?C:E:D<E?D:E:B<C?B<D?B<E?B:E:D<E?D:E:C<D?C<E?C:E:D<E?D:E;
}

int max6(int A, int B, int C, int D, int E, int F)
{
  return A>B?A>C?A>D?A>E?A>F?A:F:E>F?E:F:D>E?D>F?D:F:E>F?E:F:C>D?C>E?C>F?C:F:E>F?E:F:D>E?D>F?D:F:E>F?E:F:B>C?B>D?B>E?B>F?B:F:E>F?E:F:D>E?D>F?D:F:E>F?E:F:C>D?C>E?C>F?C:F:E>F?E:F:D>E?D>F?D:F:E>F?E:F;
}

int min6(int A, int B, int C, int D, int E, int F)
{
  return A<B?A<C?A<D?A<E?A<F?A:F:E<F?E:F:D<E?D<F?D:F:E<F?E:F:C<D?C<E?C<F?C:F:E<F?E:F:D<E?D<F?D:F:E<F?E:F:B<C?B<D?B<E?B<F?B:F:E<F?E:F:D<E?D<F?D:F:E<F?E:F:C<D?C<E?C<F?C:F:E<F?E:F:D<E?D<F?D:F:E<F?E:F;
}
If someone somehow needs more then 6 .. let me know :roll:

K

Post Reply