How to solve this question: Create a Class called IntegerSet in c++?

This question paper :
http://up.brbz.net/live/13064216351.jpg
....
create class integerSet . each integerSet object can hold integers from 1 to 1000 .
The set is represented by an array of booleans. array element a[i] is true if integer i is in the set; false otherwise.
the no-argument constructor initializes the array to the "empty set" (i.e. a set whose array representation contains all false values).
--
write methods:
checkExistant: checks if an integer k is an element of the set or no ..
--
insertElement: insert an integer k given as input into a set
--
fill: fills the set with integers entered by the user from the keyboard. the user enters first the number of integers he wants to add to the set and then starts entering the integers one by one. if he enters an integer, already entered or out of the range, the program should print "invalid value. enter another integer".
----
deleteElemint :deletes an integer k from the set .
---
isEmpty: returns true if the set is empty and false otherwise.
---
clear: removes all the elements in the set.
---
card: returns the cardinality of a set (i.e. the number of integers in the set)
---
isEqualTo: determines wither two sets are equal.
---
copy: returns a copy of a set.
---
sum: returns the sum of the elements of the set.
---
union: returns a set which is the set-theoretic union of two setes given as input an element of the returned set is set to true if that element is true ineither or both of the input sets. otherwise, the element of the returned set is set to false.
--
intersection: returns a set which is the set-theortic intersection of two sets given as input. An element of the returned set is set to true if that element is true in both of the input sets. otherwise, the element of the returnd set is set to false.
..
This is the solution
But I have a problem
At Method
" FILL"
Can you help me please

How can i make the user adds the numbers

#include <iostream>
using namespace std;

class IntegerSet {



bool a[1000+1];
public:




void insertElement(int i){ a[i] = true;}

// is set empty
int isEmpty() {

for (int i =1; i< sizeof a/sizeof a[0]; i++)

if (a[i]== true)

return false;
else
return true;
}

// does element i exist
int checkExistant(int i) {

if (a[i]== true)

return 1;
else
return 0;
}

// number of elements in set
int card() {

int n =0;

int i = 0;

for (i =1; i< sizeof a/sizeof a[0]; i++)

if (a[i]== true)

n++;

return n;

}

// make set empty
void clear() {

for (int i =1; i< sizeof a/sizeof a[0]; i++)



a[i]= false;
}
int fill();// " HERE THE problem "

// remove element i from set
int deleteElement(int i) {
a[i] = false;
}
// constructor
IntegerSet() {

int i = 0;

for (i =1; i< sizeof a/sizeof a[0]; i++)

a[i ] = false;

}

// union
IntegerSet setUnion(IntegerSet &first, IntegerSet &second) {
int i;
IntegerSet result;
for (i = 1; i< sizeof a /sizeof a [0]; i++)
if (first. a[i] == true)
result. a[i] = true;

for (i = 1; i< sizeof a /sizeof a [0]; i++)
if (second. a[i] == true)
result. a[i] = true;

return result;
}

// intersection
IntegerSet intersection( IntegerSet &first, IntegerSet &second) {
int i;
IntegerSet result;
for (i = 1; i< sizeof a /sizeof a [0]; i++)
if (first.a[i] == true && second. a[i]== true)
result.a[i] = true;
return result;
}
int sum() {
int i, s;
for (s=0, i = 1; i< sizeof a /sizeof a [0]; i++)
if (a[i]== true)
s += i;
return s;
}

friend bool isEqualTo(IntegerSet &first, IntegerSet &second);

};

bool isEqualTo(IntegerSet &first, IntegerSet &second)
{
int i;
if (first.card() != second.card())
return false;
for (i = 1; i <sizeof first.a/sizeof first.a[0]; i++) {
if ((first. a[i] == true && second. a [i] == true)
|| (first. a[i] == false && second. a [i] == false))
continue;
return false;
}
return true;
}

int main()
{
IntegerSet a;
IntegerSet b;
IntegerSet c;
int i;

// Perform numerous tests as asked for by the assignment
// test InsertElement()
a.insertElement(2);

// test isEmpty()
if (a.isEmpty())
cout << "a is empty\n";
else
cout << "a is not emtpy\n";

if (b.isEmpty())
cout << "b is empty\n";
else
cout << "b is not empty\n";

// test clear()
a.clear();
if (a.isEmpty())
cout << "a is empty\n";
else
cout << "a is not emtpy\n";

a.insertElement(10);
a.insertElement(20);
cout << "total elements in a " << a.card() << endl;

// test checkExistant()
if(a.checkExistant(10))
cout << "10 is in set a\n";
else
cout << "10 is in not set a\n";

if(a.checkExistant(13))
cout << "13 is in set a\n";
else
cout << "13 is in not set a\n";

a.clear();
b.clear();
a.insertElement(9);
a.insertElement(15);
a.insertElement(100);
a.insertElement(200);

b.insertElement(15);
b.insertElement(200);
b.insertElement(300);
// test UNION
c = c.setUnion(a, b);
cout << "Union of A and B\n";
for(i =1; i<=1000; i++)
if (c.checkExistant(i))
cout << i<<endl;
// test INTERSECTION
c = c.intersection(a, b);
cout << "Intersection of A and B\n";
for(i =1; i <=1000; i++)
if (c.checkExistant(i))
cout << i<<endl;

a.clear();
b.clear();
a.insertElement(5);
a.insertElement(7);
b.insertElement(5);
b.insertElement(3);
if (isEqualTo(a, b))
cout << "a is equal b" << endl;
else
cout << "a is not equal b" << endl;

a.insertElement(3);
b.insertElement(7);
// test isEqualTo()
if (isEqualTo(a, b))
cout << "a is equal b" << endl;
else
cout << "a is not equal b" << endl;
return 0;
}


----

Please, Please, Please
I want help ..
Even a little help means to me =(
I need it today

something like this. Write actual code on the basis of this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
loop(till user enters a 'q')
{
    int theNumber;

    std::cout << "enter a number";
    std::cin >> theNumber;

    if(theNumber > 1000)
         return "Invalid";

    if(!checkExistant(theNumber))
         insertElement(theNumber);
    else
         return "Invalid";

    std::cout "here ask user if he want to enter more, if he enter's a 'q', break else continue";
}

Last edited on
Have you even tried making the fill function yet? Getting the values from the user would be easy. Just ask the user for the amount they want to enter, then create an array of int's the size of the number they entered. Then, in a loop, fill the array with more entered values, checking each one that is entered. Once you have the proper values stored in the int array, set all the indices, at those values, in the integerset class's bool array to true. Pretty straight forward I think.
Topic archived. No new replies allowed.