Hi. I have been asked as an exercise to create an integerset class using a vector of bool to store the numberes - initilise to false and make that elemnt true fro a number.
I have had an attempt and so far its not what I expected. I'm nowhere near finished but testing as i go. It doesnt work as expected. Basically if i instantiate to objects they contain exactly the same randon mumbers.
Can anyone help please?
Thanks in advance.
Hi and thank you! How can I get around this? As you say my seed needs to be outside the constructor, but I'm not sure how/where to put it to get the desired effect,
You're correct that you should put the call to srand() in main.
Just a reminder that the call to srand() needs to be before lines 16-17. This is because integerSet's constructor is called at lines 16-17 and integerSet's constructor invokes rand().
Thanks everyone for your replies. I need to have a rethink. What I am trying to do is not what the exercise is asking to do!
I need to change the constructor and stop populating the vector with random numbers. Do this with a amber function.
If the constructor is used as is then I can not create an 'empty' vector full of 'false' - if you get what i mean.
These 'empty' vectors will be used to check equality, and create union and intersection sets.
Back to the drawing board 🤔
Hi - Ive had another go but ran into some more 'problems'. In a member function i need to create a union of sets. How can i pass my 2 objects to the menber fuction? Or am I simply doing it wrong!! Her is my code...
I apologise - I think I need to be a clearer on what I am trying to achieve. I instantiate 2 objects and put random numbers into them - each element marked true.
Each object has 20 random numbers 0 99.
I now need to create a member function that takes these 2 initial objects and creates a third object by doing a union of the first 2.
Thus is where I'm well n truly struggling!
Any help is greatly appreciated.
I think I'm clear on what you're trying to do. My previous response was only addressing your syntax errors, not the logic of your unionOfSets function. You haven't reposted your current main.cpp, so I'm unsure if you calling unionOfSets() correctly.
1 2 3 4 5
void integerSet::unionOfSets (integerSet &set1, integerSet &set2)
{ for (int i = 0; i < 100; i++)
{ intStorageSet[i] = (set1.intStorageSet[i] || set2.intStorageSet[i]);
}
}
Hi - need some advice again!! So far so good I've managed to get my code working as expected! Cheers!
I've been trying to get my last member function to work isEqualTo but try as I mighty i just can't!!!
I was thinking because they are vectors you can check if they are equal? Or am I missing something. Plus my fuction call doesnt work as expected either...
Thanks again
1 2 3 4 5 6 7 8 9 10 11 12
void isEqualTo(IntegerSet &, IntegerSet &); // function to check if one set is equal to another
void IntegerSet::isEqualTo(IntegerSet &set1, IntegerSet &set2){
if (set1.intStorageSet == set2.intStorageSet) {
std::cout << "The sets are equal" << std::endl;
}
else{
std::cout << "The sets are not equal" << std::endl;
}
}
Cheers I've managed to work it out.. The code below does what I want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
bool testEqual(IntegerSet&);// function to check if one set is equal to another
bool IntegerSet::testEqual(IntegerSet &rhs){
returnthis->intStorageSet == rhs.intStorageSet;
}
//call from main
case 9:
if (firstSet.testEqual(secondSet)) {
std::cout << "The sets are equal\n";
}
else{
std::cout << "The sets are not equal\n";
}
break;