| 1 |
/* |
| 2 |
* methods/iterate_mod.c |
| 3 |
* |
| 4 |
* identical to 'iterate.c', but with some trivial code changs to create |
| 5 |
* differences - for differential coverage report. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <limits.h> |
| 12 |
#include "iterate.h" |
| 13 |
|
| 14 |
|
| 15 |
int iterate_get_sum (int min, int max) |
| 16 |
{ |
| 17 |
int total = 0; |
| 18 |
|
| 19 |
/* This is where we loop over each number in the range, including |
| 20 |
both the minimum and the maximum number. */ |
| 21 |
|
| 22 |
/* just reverse iteration order */ |
| 23 |
for (int i = max; i >= min; total += i , --i) |
| 24 |
{ |
| 25 |
/* We can detect an overflow by checking whether the new |
| 26 |
sum would exceed the maximum integer value. */ |
| 27 |
|
| 28 |
if (total > INT_MAX - i) |
| 29 |
{ |
| 30 |
printf ("Error: sum too large!\n"); |
| 31 |
exit (1); |
| 32 |
} |
| 33 |
|
| 34 |
/* Everything seems to fit into an int, so continue adding. */ |
| 35 |
} |
| 36 |
|
| 37 |
return total; |
| 38 |
} |