Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / lcov / example / methods / iterate.c
blob023d1801c9364f71c994e8c0dbe04b93f3992f34
1 /*
2 * methods/iterate.c
3 *
4 * Calculate the sum of a given range of integer numbers.
6 * This particular method of implementation works by way of brute force,
7 * i.e. it iterates over the entire range while adding the numbers to finally
8 * get the total sum. As a positive side effect, we're able to easily detect
9 * overflows, i.e. situations in which the sum would exceed the capacity
10 * of an integer variable.
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "iterate.h"
19 int iterate_get_sum (int min, int max)
21 int i, total;
23 total = 0;
25 /* This is where we loop over each number in the range, including
26 both the minimum and the maximum number. */
28 for (i = min; i <= max; i++)
30 /* We can detect an overflow by checking whether the new
31 sum would become negative. */
33 if (total + i < total)
35 printf ("Error: sum too large!\n");
36 exit (1);
39 /* Everything seems to fit into an int, so continue adding. */
41 total += i;
44 return total;