Define a range type:
struct range
{
int low;
int high;
}
Then define an array of such:
struct range ranges[<some size>] = {0};
Define and implement functions finding the highest high
and the lowest lowest low
values:
int lowest(struct range * ranges, int * plowest);
int highest(struct range * ranges, int * phighest);
Enter all ranges and stores them in ranges
.
Call highest()
and lowest()
on ranges
and you have the outer interval:
struct range range_outer = {0};
/* code reading ranges. */
if (0 != lowest(ranges, &range_outer.lowest))
fprintf(stderr, "lowest() failed\n");
if (0 != highest(ranges, &range_outer.highest))
fprintf(stderr, "highest() failed\n");
To help lowest()
and highest()
it might make sense to sort the ranges
, by member low
for the former by member high
for the latter.
int sort_by_low(struct ranges * ranges);
int sort_by_high(struct ranges * ranges);
Sorting arrays can easily be done by using the qsort()
function.