Last active
September 25, 2016 16:32
-
-
Save ohmree/f0db39fd6f97631a926311493131d0fc to your computer and use it in GitHub Desktop.
Min and max functions in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#include "stdlib.h" | |
#include "stdarg.h" | |
/** | |
* The maxOf function takes in any number of signed integers preceded by the amount of arguments (not including the first one) that | |
* it receives and returns the biggest number amongst all of it's arguments | |
* example usage: | |
* maxOf(5, 56, 34, 345, 1235, 90686); should return 90686*/ | |
int maxOf(int num, ...) | |
{ | |
va_list args; | |
int max; | |
/*register keyword may not be needed*/ | |
register int i; | |
va_start(args, num); | |
for (i = 0; i < num; i++) | |
{ | |
int a = va_arg(args, int); | |
if (a > max) max = a; | |
} | |
va_end(args); | |
return max; | |
} | |
/** | |
* The minOf function takes in any number of signed integers preceded by the amount of arguments (not including the first one) that | |
* it receives and returns the smallest number amongst all of it's arguments | |
* example usage: | |
* minOf(5, 56, 34, 345, 1235, 90686); should return 5*/ | |
int minOf(int num, ...) | |
{ | |
va_list args; | |
int min; | |
/*register keyword may not be needed*/ | |
register int i; | |
va_start(args, num); | |
for (i = 0; i < num; i++) | |
{ | |
int a = va_arg(args, int); | |
if (a < min) min = a; | |
} | |
va_end(args); | |
return min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment