C/ C++ Stats:

How to support the site


Site Wide Message: (current site time 9/3/2010 7:04:58 PM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

A simple demostraion of Sets and sets operations by using a Class

Print
Email
VB icon
Submitted on: 1/22/2005 3:52:26 PM
By: Aba  
Level: Beginner
User Rating: Unrated
Compatibility:C++ (general), Microsoft Visual C++, Borland C++, UNIX C++

Users have accessed this code  4353 times.
 
author picture
 
     I have demonstrated a nice use of set data structure represented by a Class. Here is the assignment. You have to Create a class IntegerSet for which each object can hold integers in the range 0 through 100. A set is represented internally as an array of ones and zeros. Array element a[i] is 1 if integer i is in the set. Array element a[j] is 0 if integer j is not in the set. The default constructor initializes a set to the so-called “empty set,” i.e., a set whose array representation contains all zeros. Provide member functions for the common set operations. For example, provide a unionOfSets member function that creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set’s array is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set’s array is set to 0 if that element is 0 in each of the existing sets). Provide an intersectionOfSets member function which creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third set’s array is set to 1 if that element is 1 in each of the existing sets). Provide an insertElement member function that inserts a new integer k into a set (by setting a[k] to 1). Provide a deleteElement member function that deletes integer m (by setting a[m] to 0). Provide a printSet member function that prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set (i.e., their position in the array has a value of 1). Print --- for an empty set. Provide an isEqualTo member function that determines whether two sets are equal. Provide an additional constructor that receives an array of integers and the size of that array and uses the array to initialize a set object. Now write a driver program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your member functions work properly.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A simple demostraion of Sets and sets operations by using a Class
// Description:I have demonstrated a nice use of set data structure represented by a Class.
Here is the assignment.
You have to Create a class IntegerSet for which each object can hold integers in the range 0 through 100. A set is represented internally as an array of ones and zeros. Array element a[i] is 1 if integer i is in the set. Array element a[j] is 0 if integer j is not in the set. The default constructor initializes a set to the so-called “empty set,” i.e., a set whose array representation contains all zeros. 
Provide member functions for the common set operations. For example, provide a unionOfSets member function that creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set’s array is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set’s array is set to 0 if that element is 0 in each of the existing sets).
Provide an intersectionOfSets member function which creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third set’s array is set to 1 if that element is 1 in each of the existing sets).
Provide an insertElement member function that inserts a new integer k into a set (by setting a[k] to 1). Provide a deleteElement member function that deletes integer m (by setting a[m] to 0). 
Provide a printSet member function that prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set (i.e., their position in the array has a value of 1). Print --- for an empty set.
Provide an isEqualTo member function that determines whether two sets are equal.
Provide an additional constructor that receives an array of integers and the size of that array and uses the array to initialize a set object.
Now write a driver program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your member functions work properly. 

// By: Aba
//
//This code is copyrighted and has// limited warranties.Please see http://www.PlanetSourceCode.com/vb/scripts/ShowCode.asp?txtCodeId=8835&lngWId=3//for details.//**************************************

#include<iostream.h>
const SIZE=101;//0 to 100 set containing 101 elements
class IntegerSet
{
		int a[SIZE];
	public:
		IntegerSet();
		IntegerSet(int *, int);
		IntegerSet unionOfSets(IntegerSet s2);
		IntegerSet intersectionOfSets(IntegerSet s2);
		int isEqualTo(IntegerSet s2);
		void insertElement(int n);
		void printSet();
};
IntegerSet::IntegerSet()//default constructor creates empty set
{
	int i;
	for(i=0;i<SIZE;i++)
		a[i]=0;
}
IntegerSet::IntegerSet(int *ptr, int n)//initialize object using another array of integers
{
	int i;
	for(i=0;i<SIZE;i++)//Initialize the set first
		a[i]=0;
	for(i=0;i<n;i++)
		a[(ptr[i])]=1;
}
IntegerSet IntegerSet::unionOfSets(IntegerSet s2)
{
	 IntegerSet s3;
	 int i;
	 for(i=0;i<SIZE;i++)
		if(a[i]==1 || s2.a[i]==1)
			s3.a[i]=1;
	 return s3;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet s2)
{
	 IntegerSet s3;
	 int i;
	 for(i=0;i<SIZE;i++)
		if(a[i]==1 && s2.a[i]==1)
			s3.a[i]=1;
	 return s3;
}
int IntegerSet::isEqualTo(IntegerSet s2)
{
	int i;
	int check=1;//equal
	for(i=0;i<SIZE;i++)
	 if(a[i]!=s2.a[i])
	 {
		check=0;//not equal
		break;
	 }
	return check;
}
void IntegerSet::insertElement(int n)
{
	if (n<SIZE && n>=0) //check validity
		a[n]=1;
}
void IntegerSet::printSet()
{
	int i, count=0;
	for(i=0;i<SIZE;i++)
		if(a[i]==1)
		{
			count++;
			cout<<i<<" ";
		}
	if(count==0)
		cout<<"---";
}
void main()
{
	int arr[4]={3,76,34,56};
	IntegerSet s1, s2(arr, 4), s3, s4;
	s4.insertElement(34);
	s4.insertElement(56);
	cout<<"\nDefault set=";
	s1.printSet();
	cout<<"\nUsing array as parameter, set s2= ";
	s2.printSet();
	cout<<endl;
	cout<<"Inserting 50, 100,1,0,34, 56 to s1, set s1=";
	s1.insertElement(50);
	s1.insertElement(100);
	s1.insertElement(1);
	s1.insertElement(0);
	s1.insertElement(34);
	s1.insertElement(56);
	s1.printSet();
	cout<<endl;
	cout<<"Union of s1 and s2, set s3=";
	s3=s1.unionOfSets(s2);
	s3.printSet();
	cout<<endl;
	cout<<"Intersection of s1 and s2, set s3=";
	s3=s1.intersectionOfSets(s2);
	s3.printSet();
	cout<<endl;
	cout<<"set s4=";
	s4.printSet();
	cout<<endl;
	cout<<"s2==s3=";
	if(s2.isEqualTo(s3)==1)
		cout<<"True";
	else
		cout<<"False";
	cout<<"\ns3==s4=";
	if(s3.isEqualTo(s4)==1)
		cout<<"True";
	else
		cout<<"False";
}


Other 1 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
4/22/2008 7:09:24 PMdawit

good job man
(If this comment was disrespectful, please report it.)

 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.