Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void fold (const void * array, unsigned size, unsigned count,
- void (* callback) (void *, const void *), void * initial) {
- const char * p = array;
- while (count --) {
- callback(initial, p);
- p += size;
- }
- }
- static void add_integers (void * accumulator, const void * value) {
- *(unsigned *) accumulator += *(const unsigned *) value;
- }
- static unsigned sum_array (const unsigned * array, unsigned count) {
- unsigned result = 0;
- fold(array, sizeof *array, count, &add_integers, &result);
- return result;
- }
- static void initialize_array (unsigned * array, unsigned count) {
- unsigned p;
- for (p = 0; p < count; p ++) array[p] = p;
- }
- unsigned hundred (void) {
- unsigned values[10];
- initialize_array(values, sizeof values / sizeof *values);
- unsigned p, sum = sum_array(values, sizeof values / sizeof *values);
- for (p = 0; p < (sizeof values / sizeof *values); p ++) values[p] = 10 - p;
- return sum + sum_array(values, sizeof values / sizeof *values);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement