diff options
author | Haidong Ji | 2022-04-15 15:51:30 -0500 |
---|---|---|
committer | Haidong Ji | 2022-04-15 15:51:30 -0500 |
commit | 442a49ad5a48d417345959b903ae6a6d32d55759 (patch) | |
tree | c7127bb497e5e439018b1915e0136eec2c9cb124 /14_array_max/arrayMax.c |
Excellent fundamentals and displine training, many tools and techniques
exercises: gdb, emacs, valgrind, git
Diffstat (limited to '14_array_max/arrayMax.c')
-rw-r--r-- | 14_array_max/arrayMax.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/14_array_max/arrayMax.c b/14_array_max/arrayMax.c new file mode 100644 index 0000000..5fc5d62 --- /dev/null +++ b/14_array_max/arrayMax.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> + +int * arrayMax(int * array, int n) { + if (n == 0){ + return NULL; + } + int maxInt = array[0]; + int * answer = array; + for (int i = 1; i < n; i++) { + if (array[i] > maxInt) { + maxInt = array[i]; + answer = &array[i]; + } + } + return answer; +} + +void doTest(int * array, int n) { + printf("arrayMax("); + if (array == NULL) { + printf("NULL"); + } + else { + printf("{"); + for (int i =0; i < n; i++) { + printf("%d", array[i]); + if (i < n -1) { + printf(", "); + } + } + printf("}"); + } + printf(", %d) is \n", n); + int * p = arrayMax (array, n); + if (p == NULL) { + printf("NULL\n"); + } + else { + printf("%d\n", *p); + } +} + +int main(void) { + int array1[] = { 77, 33, 19, 99, 42, 6, 27, 4}; + int array2[] = { -3, -42, -99, -1000, -999, -88, -77}; + int array3[] = { 425, 59, -3, 77, 0, 36}; + + doTest (array1, 8); + doTest (array2, 7); + doTest (array3, 6); + doTest (NULL, 0); + doTest (array1, 0); + + return EXIT_SUCCESS; +} |