1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
int reduce(const int* array, int size, int (*accumulator)(int, int)) {
int value = 0;
for (int i = 0; i < size; i++) {
value = accumulator(value, array[i]);
}
return value;
}
int max(int left, int right) {
return left > right ? left : right;
}
int sum(int left, int right) {
return left + right;
}
int main(int argc, char* argv[]) {
int numbers[] = { 5, 9, 81, 32, 14, 6, 1 };
int numbers_len = sizeof(numbers) / sizeof(numbers[0]);
int sum_of_numbers = reduce(numbers, numbers_len, sum);
int max_number = reduce(numbers, numbers_len, max);
printf("sum: %d\nmax: %d\n", sum_of_numbers, max_number);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn & David Burnett This content is protected and may not be shared, uploaded, or distributed.