Created
February 13, 2023 15:52
-
-
Save pavly-gerges/9103dd0eed5dbd937bb22541a4df98a5 to your computer and use it in GitHub Desktop.
Finds the maximum number among some numbers
This file contains hidden or 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> | |
/** | |
* Finds the maximum number between 2 numbers. | |
* | |
* @param n0 the first operand | |
* @param n1 the second operand | |
* @return the maximum number, any of the 2 numbers if the numbers are equal | |
*/ | |
static inline int max0(int n0, int n1) { | |
if (n0 == n1) { | |
return n0; | |
} | |
if (n0 > n1) { | |
return n0; | |
} else { | |
return n1; | |
} | |
} | |
/** | |
* Finds the max number in an array of numbers. | |
* | |
* @param numbers a buffer of integers | |
* @param count the number of items in the buffer | |
* @return the maximum number among the [numbers] buffer, any number if all the numbers are equal | |
*/ | |
static inline int max1(int* numbers, int count) { | |
int out = 0; | |
for (int i = 0; i < count; i++) { | |
out = max0(out, numbers[i]); | |
} | |
return out; | |
} | |
int main() { | |
printf("Max number = %i\n", max0(max0(100, 5080), 1000)); | |
int numbers[5]; | |
numbers[0] = 2000; | |
numbers[1] = 100000; | |
numbers[2] = 333; | |
numbers[3] = 4455; | |
numbers[4] = (long) 5000L; | |
printf("Max number = %i\n", max1(numbers, sizeof(numbers)/sizeof(int))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment