A vector containing intervals

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;
}

These are the files that I filled in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "intervalSet.h"
#include <iostream>
#include <iomanip>

using namespace 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;
}


These are the files that were given to me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iomanip>
#include <iostream>
#include "intervalSet.h"
#include "interval.h"

using namespace std;


void subtractBuildings(double borderLength)
{
  IntervalSet highway(0.0, borderLength);
  double x1, x2;
  cin >> x1 >> x2;
  while (x1 <= x2)
    {
      Interval building (x1, x2);
      highway.subtract (building);
      cin >> x1 >> x2;
      cout << x1 << " " << x2;
    }
  double sum = highway.sum();
  cout << "The total planting length is "
       << setiosflags(ios::fixed) << setprecision(1) << 5 << endl;

}
int main ()
{
  double L;
  cin >> L;
  subtractBuildings (L);
  return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "interval.h"
#include <algorithm>   // for min, max
#include <iostream>

using namespace 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 << ")";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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 
Last edited on
Topic archived. No new replies allowed.