I have an assignment asking me to create a vector that will hold intervals. I've done everything up until the point where I have to subtract the beginning interval value from the ending interval value to find the length that they represent. After that I need to add all of the interval lengths together.
I would imagine that the vector looks something like this (correct me if I'm wrong I'm unfamiliar with this) [20,50][10,20][40,30]. The problem I'm having is how would I subtract the difference of each interval (40-30 would give me a length of 10 for example). I can't subtract them like I normally would because one place holds two values if I understand it correctly.
This is due in an hour so I doubt I'll get it running in time. I've just spent so much time on this, so I'm really frustrated. I want to find out what simple thing I'm missing here.
This is what I have so far for the function that I'm having trouble with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
double IntervalSet::sum()
{
double C = 0.0;
double D = 0.0;
for(int i = 0; i <= seq.size(); i++)
{
while(!seq[i].empty())
{
double A = seq[i].getUpper(); // i was hoping this would get the highest of the 2 interval numbers
double B = seq[i].getLower(); // and that this would get the lowest
double C = A - B; // so that i could subtract to get the length of one interval
double D = D + C; // and then add them all together here
}
}
return D;
}
#ifndef INTERVALSET_H
#define INTERVALSET_H
#include <iostream>
#include <vector>
#include "interval.h"
class IntervalSet
{
private:
std::vector<Interval> seq;
public:
//Create the IntervalSet with a single starting vector
IntervalSet (double l, double h) : seq(1,Interval(l,h)) {} // created the Interval set, and created the starting interval in the first slot
//Subtracting overlapping interval sets
void subtract(Interval I);
//Find the length of the intervals, then add up the lengths
double sum();
};
#endif
#include "intervalSet.h"
#include <iostream>
#include <iomanip>
usingnamespace std;
IntervalSet seq(double l,double h)
{}
//remove overlapping intervals and intervals that are empty
void IntervalSet::subtract(Interval I)
{
double beginning = 0.0;
double ending = 0.0;
beginning = I.getLower();
ending = I.getUpper();
for(int i = 0; i <= seq.size(); i++)
{
while(!seq[i].empty() && seq[i].overlaps(I)) // while the seq is not empty and there is an overlapping interval
{ // if the interval does not overlap it will be left alone
seq.push_back(I.below(beginning)); //
seq.push_back(I.above(ending));
}
}
}
double IntervalSet::sum()
{
double C = 0.0;
double D = 0.0;
for(int i = 0; i <= seq.size(); i++)
{
while(!seq[i].empty())
{
double A = seq[i].getUpper(); // i was hoping this would get the highest of the 2 interval numbers
double B = seq[i].getLower(); // and that this would get the lowest
double C = A - B; // so that i could subtract to get the length of one interval
double D = D + C; // and then add them all together here
}
}
return D;
}
#include "interval.h"
#include <algorithm> // for min, max
#include <iostream>
usingnamespace std;
// Construct a new interval with the indicated bounds
Interval::Interval (double l, double h)
: low(l), high(h)
{}
// Test to see if this interval overlaps with another
bool Interval::overlaps (Interval x) const
{
return !(low >= x.high || high <= x.low);
}
// Get the portion of this interval that lies below x. For
// example:
// Interval w (-10.0, 10.0);
// Interval z = w.below(5.0);
// z should be (-10.0..5.0)
Interval Interval::below (double x) const
{
return Interval (low, min(high, max(low, x)));
}
// Get the portion of this interval that lies above x. For
// example:
// Interval w (-10.0, 10.0);
// Interval z = w.above(5.0);
// z should be (5.0..10.0)
Interval Interval::above (double x) const
{
return Interval (max(low,min(high, x)), high);
}
// Print an interval
void Interval::print (std::ostream& out) const
{
out << "(" << low << " .. " << high << ")";
}
#ifndef INTERVAL_H
#define INTERVAL_H
#include <iostream>
class Interval {
private:
double low;
double high;
public:
// Construct a new interval with the indicated bounds
Interval (double l, double h);
// Get the lower bound of this interval
double getLower() const {return low;}
// Get the upper bound of this interval
double getUpper() const {return high;}
// Test to see if this interval overlaps with another
bool overlaps (Interval x) const;
// Get the portion of this interval that lies below x. For
// example:
// Interval w (-10.0, 10.0);
// Interval z = w.below(5.0);
// z should be (-10.0..5.0)
Interval below (double x) const;
// Get the portion of this interval that lies above x. For
// example:
// Interval w (-10.0, 10.0);
// Interval z = w.above(5.0);
// z should be (5.0..10.0)
Interval above (double x) const;
// Test to see if this interval is empty
bool empty () const
{
return high <= low;
}
// Print an interval
void print (std::ostream& out) const;
};
#endif