i need help using the following .h file to
Write a definition of a member function
void complementSet(); which for a set A, the complement of A is the
difference of U-A, where U is the universe set.
Would the universe set be the monthsSet istself??
Thank you all very much for all your help


This is the .h file..

#ifndef H_monthsSet
#define H_monthsSet

#include <iostream>
using namespace std;

class monthsSet
{
private:
	bool months[12];

public:
	monthsSet();
	int cardinality();
	void print();			
	bool membership(int month);
	void differenceSet(monthsSet &B);
	void unionSet(monthsSet &B);
	void intersectionSet(monthsSet &B);
	bool equals(monthsSet &B);
	void copySet(monthsSet &B);
	bool emptySet();
	void clearSet();
	void insertMonth(int month);
	void deleteMonth(int month);
};

monthsSet::monthsSet()
{
	for (int i=0; i<12; i++)
		months[i] = false;
};

int monthsSet::cardinality()
{
	int count=0;
	for (int i=0; i<12; i++)
	{
		if (months[i])
			count++;
	}
	return count;
};

void monthsSet::print()
{
	for (int i=0; i<12; i++)
        {       
                if (months[i])
			switch (i)
			{
				case 0: 
					cout << "january "; break;
				case 1: cout << "february "; break;
				case 2: cout << "march "; break;
				case 3: cout << "april "; break;
				case 4: cout << "may "; break;
				case 5: cout << "june "; break;
				case 6: cout << "july "; break;
				case 7: cout << "august "; break;
				case 8: cout << "september "; break;
				case 9: cout << "october "; break;
				case 10: cout << "november "; break;
				default: cout << "december"; break;
			}
        }
	cout << endl;
};

bool monthsSet::membership(int month)
{
	return (months[month]); 
};

void monthsSet::differenceSet(monthsSet &B)
{
	for (int i=0; i<12; i++)
		if (B.months[i])
			months[i] = false;
};

void monthsSet::unionSet (monthsSet &B)
{
	for (int i=0; i<12; i++)
		if (B.months[i])
			months[i] = true;
};

void monthsSet::intersectionSet (monthsSet &B)
{
	for (int i=0; i<12; i++)
		if (months[i] && !B.months[i])
			months[i] = false;
};

bool monthsSet::equals (monthsSet &B)
{
	for (int i=0; i<12; i++)
		if (months[i] != B.months[i])
			return false;
	return true;
};

void monthsSet::copySet (monthsSet &B)
{
	for (int i=0; i<12; i++)
		if (B.months[i])
			months[i] = true;
		else
			months[i] = false;
};

bool monthsSet::emptySet ()
{
	for (int i=0; i<12; i++)
		if (months[i])
			return false;
	return true;
};

void monthsSet::clearSet ()
{
	for (int i=0; i<12; i++)
		months[i] = false;
};

void monthsSet::insertMonth(int month)
{
	months[month-1] = true;
};

void monthsSet::deleteMonth(int month)
{
	months[month-1] = false;
};

#endif

I thank you for any help or advice you may have.

Dani AI

Generated

Good question on the universe set. In this class, the universe U is the fixed domain of all 12 months (Jan..Dec). It is not any particular monthsSet object. Because U is fixed and fully known, the complement of A (U - A) can be computed by flipping each boolean in place. That is exactly what hinted at.

Here is a simple in-place member you can add; no parameters needed since the universe is implicit:

void monthsSet::complementSet()
{
    for (int i = 0; i < 12; ++i)
        months[i] = !months[i];
}

If you also want a non-mutating version that returns a new set, add:

monthsSet monthsSet::complement() const
{
    monthsSet out;
    for (int i = 0; i < 12; ++i)
        out.months[i] = !months[i];
    return out;
}

Two correctness tips that will save you time:

  • Indexing is inconsistent right now. insertMonth/deleteMonth use 1-based months, but membership(int month) uses the index directly. That risks out-of-bounds for membership(12) (accesses months[12]). Make it consistent and safe:
bool monthsSet::membership(int month) const
{
    if (month < 1 || month > 12) return false;
    return months[month - 1];
}
  • Mark read-only functions const and pass other sets by const&: bool equals(const monthsSet& B) const, void unionSet(const monthsSet& B), etc. This documents intent and prevents accidental modification. As noted, operators can be added later (operator~ for complement, operator- for difference), but you do not need them to implement complement now.

Quick test idea: build A, print it, call A.complementSet();, then print again. You should see exactly the months that were not in A before.

Recommended Answers

All 4 Replies

1. Move all function definitions to a separate .cpp file (and never, ever include them in .h files, except class templates - but it's the other story).
2. Think about month[i] = !month[i]

commented: thanks alot +1

the cpp file we have created is as follow...

#include <iostream>
#include "monthsSet.h"

using namespace std;

int main()
{
	cout << "**** Running testMonths.cpp ****\n\n";
	monthsSet A, B;
	A.insertMonth(1);
	A.insertMonth(5);
	A.insertMonth(10);
	A.insertMonth(11);
	A.insertMonth(12);
	cout << "Set A= ";
	A.print();
	B.insertMonth(1);
	B.insertMonth(2);
	B.insertMonth(5);
	B.insertMonth(6);
	B.insertMonth(9);
	cout << "Set B= ";
	B.print();

	cout << "Set A cardinality is = " << A.cardinality() << endl;
	cout << "Set B cardinality is = " << B.cardinality() << endl;

	A.unionSet(B);
	//A += B;
	cout << "After unioned B in A, set A is = ";
        A.print();

	A.differenceSet(B);
	//A -= B;
	cout << "After substracted B from A, set A is = ";
        A.print();
	
	A.intersectionSet(B);
	cout << "After intersected A with B, set A is = ";
        A.print();

	A.copySet(B);
	//A = B;
	cout << "After copying B in A, the set A is = ";
        A.print();
	
	if ( A.equals(B) )
	//if ( A == B )
		cout << "After comparing sets A and B, they are the same" << endl;
	else 
		cout << "After comparing sets A and B, they are different" << endl;

	return 1;
}

for the function defenition i was thinking around the lines of..

void complementSet(&b);
{
monthsSet a = months[i] - b
};

am i working along similar lines? or am i completly off.?

You have two threads on the same topic.

http://www.daniweb.com/forums/thread176040.html

Mark one solved so people know where to comment. Yes, you are way off. Read my post on the other thread for some of the reasons why.

I don't think this is what ArkM meant by the separate cpp file (what you have is a driver/tester cpp file with a main function. I assume the file he is referring to is a class member function implementation cpp file WITHOUT a main function). It's good advice, but it involves separate compilation. You might want to tackle that later and just keep it all in the .h file like you have it now, at least for now.

Beyond the problems I mentioned in the other thread, even after you fix them, your function won't work since you haven't written the - operator function yet, so do that first.

commented: you are so helpful. thanks a bunch +1

thanks a whole lot!

You have two threads on the same topic.

http://www.daniweb.com/forums/thread176040.html

Mark one solved so people know where to comment. Yes, you are way off. Read my post on the other thread for some of the reasons why.

I don't think this is what ArkM meant by the separate cpp file (what you have is a driver/tester cpp file with a main function. I assume the file he is referring to is a class member function implementation cpp file WITHOUT a main function). It's good advice, but it involves separate compilation. You might want to tackle that later and just keep it all in the .h file like you have it now, at least for now.

Beyond the problems I mentioned in the other thread, even after you fix them, your function won't work since you haven't written the - operator function yet, so do that first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.