class help

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.

James

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  //
//  main.cpp
//  exercise 10.8 integerClass
//
//  Created by James Farrow on 10/10/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#include <iostream>
#include <vector>
#include <ctime>
#include "integerset.hpp"

int main() {
    
    integerSet firstSet;
    integerSet secondSet;
    
    firstSet.print();
    secondSet.print();
    
    return 0;
}

//
//  integerset.hpp
//  exercise 10.8 integerClass
//
//  Created by James Farrow on 12/10/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#ifndef integerset_hpp
#define integerset_hpp

#include <vector>

class integerSet{
public:
    integerSet(); //constructor
    void unionOfSets(); // function to make a union of 2 sets into another set
    void intersectionOfSets(); //function to make common elements into another set
    void insertElement(); // function to insert an element into set
    void isEqualTo(); // function to check if one set is equal to another
    void print();//print contents of a set
private:
    std::vector<bool> intStorageSet;
};

#endif /* integerset_hpp */

//  exercise 10.8 integerClass
//
//  Created by James Farrow on 12/10/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#include "integerset.hpp"
#include <iostream>
#include <ctime>


//constructor creates a vector with 100 false elements and stores random numbers
integerSet::integerSet(){
    srand(static_cast<unsigned int>(time(NULL))); // randomise seed
    //create vector with 100 false elemnts
    for (int i = 0; i < 100; i++) {
        intStorageSet.push_back(i);
        intStorageSet[i] = false;
    }
    //poulate vector with random numbers
    for (int i = 0; i < 10; i++) {
        int num = ( rand() % 99);
        if(!intStorageSet[num]){
            intStorageSet[num] = true;
        }
    }
}

void integerSet::print(){
    std::cout << "Contents of set" << std::endl;
    for (int i = 0; i < 100; i++) {
        if(intStorageSet[i]){
            std::cout << i << " ";
        }
    }
}
You are resetting the seed value back to the same value by having the srand function inside the constructor.
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,

Thanks again.

James
I think I will put my seed statement in main and not the constructor, hopefullt that will work as expected.

Thanks!

James
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 🤔

Thanks

James
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
//  integerset.cpp
//  exercise 10.8 integerClass
//
//  Created by James Farrow on 12/10/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#include "integerset.hpp"
#include <iostream>
#include <ctime>


//constructor creates a vector with 100 false elements and stores random numbers
integerSet::integerSet(){
    //create vector with 100 false elemnts
    for (int i = 0; i < 100; i++) {
        intStorageSet.push_back(i);
        intStorageSet[i] = false;
    }
}

void integerSet::populateSet(){
    //poulate vector with random numbers
    for (int i = 0; i < 10; i++) {
        int num = ( rand() % 99);
        if(!intStorageSet[num]){
            intStorageSet[num] = true;
        }
    }
}

void integerSet::print(){
    std::cout << "Contents of set" << std::endl;
    for (int i = 0; i < 100; i++) {
        if(intStorageSet[i]){
            std::cout << i << " ";
        }
    }
    std::cout << std::endl;
}

void integerSet::unionOfSets(integerSet &set1, integerSet &set2){
    for (int i = 0; i < 100; i++) {
        if (set1[i] || set2[i]) {
            intStorageSet[i] = true;
        }
    }
}


//
//  integerset.hpp
//  exercise 10.8 integerClass
//
//  Created by James Farrow on 12/10/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#ifndef integerset_hpp
#define integerset_hpp

#include <vector>

class integerSet{
public:
    integerSet(); //constructor
    void populateSet();
    void unionOfSets(integerSet &set1, integerSet &set2); // function to make a union of 2 sets into another set
    void intersectionOfSets(); //function to make common elements into another set
    void insertElement(); // function to insert an element into set
    void isEqualTo(); // function to check if one set is equal to another
    void print();//print contents of a set
private:
    std::vector<bool> intStorageSet;
};

#endif /* integerset_hpp */
I get an erorr with meber function unionOfSets!!

James
It would be helpful if you posted the exact text of the error message you're getting.

Line 18 causes a performance warning. You're trying to push an int where a bool is expected.
You could do simply:
18
19
  intStorageSet.push_back (false);
// line 19 not needed 


Line 45: You've not defined an operator || that operates in IntegerSets nor have you defined an [] operator for IntegerSet. I think what you mean is:
 
  if (set1.intStorageSet[i] || set2.intStorageSet[i])

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.

Thanks in advance

James
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]);
    }
}


In main:
1
2
3
4
5
  integerSet s1, s2, s3;

  s1.populateSet ();
  s2.populateSet ();
  s3.unionOfSets (s1, s2);

Thank you! I will give it a go tomorrow. Does it need to declared const so there is no danger of modifying the arguments!
Just a thought 🤔
Thank you! Works brilliantly - cheers!!

James
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){
    return this->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;



Thanks again for your help.

James
Topic archived. No new replies allowed.