CSc 265 Lab 11

Team Name:

Team Members:


1) What is the exact output of the following program?
IntSet.h
const int MAXSIZE = 100;

// exception classes
class DuplicateExc {};
class FullExc {};
class NotFoundExc {};

class IntSet {
public:
    IntSet();
    void add(int);
    void remove(int);
    int isMember(int) const;
    int size() const;
protected:
    int findPos(int) const;
    int s[MAXSIZE];
    int curSize;
};
IntSet.cc
IntSet::IntSet()
{
    curSize = 0;
}

void IntSet::add(int x)
{
    if (findPos(x) >= 0) {
        throw DuplicateExc();
    } else if (curSize == MAXSIZE) {
        throw FullExc();
    }
    s[curSize++] = x;
}

void IntSet::remove(int x)
{
    int i,pos;

    if ((pos = findPos(x)) == -1) {
	throw NotFoundExc();
    } else {
        for (i = pos; i < curSize-1; i++)
            s[i] = s[i+1];
        curSize--;
    }
}

int IntSet::isMember(int x) const
{
    return(findPos(x) >= 0);
}

int IntSet::size() const
{
    return(curSize);
}

int IntSet::findPos(int x) const
{
    int pos,i; 

    pos = -1;
    for (i = 0; i < curSize; i++)
        if (s[i] == x) {
            pos = i;
            break;
        }

    return(pos);
}
driver.cc
#include 
#include "IntSet.h"

int main()
{
    int i,j;
    IntSet s;

    for (i = 1; i <= 100; i++)
        s.add(i);

    for (i = 1; i <= 100; i++)
        for (j = 2; j < i; j++)
            if (i%j == 0) {
                s.remove(i);
                break;
            }


    for (i = 1; i <= 100; i++)
        if (s.isMember(i))
            cout << i << " ";

    cout << endl;
}


Answer: 1 2 3 5 7 11 13 17 19 23 etc. (prime #s up to 100)


2) Write a method to re-define the * operator for IntSet:

             IntSet& operator*(const IntSet& src1,const IntSet& src2) { }
which returns a new IntSet object containing the intersection of the sets src1 and src2.
	IntSet& IntSet::operator*(const IntSet& src1,const IntSet& src2) {
		IntSet* temp;

		for (int i = 0; i < src1.size(); i++)
			if (src2.isMemebr(src1.s[i]))
				temp->add(src1.s[i]);

		return(*temp);
	}