program C++ 6th edition

need help understanding chapter 10 exercises 10.9

Page 1 of 1

5 Replies - 2298 Views - Last Post: 23 March 2009 - 07:42 PM Rate Topic: -----

#1 Telaco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 09

program C++ 6th edition

Post icon  Posted 23 March 2009 - 11:26 AM

Hey guys i was wondering, is there a way to make an array, that can hold 100 random numbers with out you having to put them in 1 by 1?

also i have another problem, i'm in this programing class that use this book, How to program C++ 6th edition.

and on chapter 10 exercises 10.9 i'm having a hard time understanding what to do. I just need a good push in the right direction.

Below is the Exercise:

"Create class IntegerSet for which each object can hold integers in the range 0-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."

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.

Provide an intersectionOfSets member function which creates a third set which is the set-theoretic intersection of two existing sets.

Provide an insertElement member function that inserts a new integer k into a set (by setting a[k] to 1).

Provide an 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.
print --- for 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."

Thanks in advance guys.

Is This A Good Question/Topic? 0
  • +

Replies To: program C++ 6th edition

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: program C++ 6th edition

Posted 23 March 2009 - 12:03 PM

We don't tend to give out code, but the easiest way to demonstrate something so simple is with an example.

Basically, all you need to do is loop 100 times and put a random number in the array.

#include <iostream>
#include <ctime>

using namespace std;

int main() {
    srand(time(0)); // seed the random number (better results)
    int array[100] = {0}; // declare an array of 100 elements
    for(int i = 0; i < 100; i++) { // loop 100 times
        array[i] = rand(); // put a random number in the array
    }

    // the next loop will output each value in the array on a line
    for(int i = 0; i < 100; i++) {
        cout << array[i] << endl;
    }

    cin.get(); // hold the window open
    return EXIT_SUCCESS; // successful execution
}

Hope this helps :)

This post has been edited by gabehabe: 23 March 2009 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 Telaco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 09

Re: program C++ 6th edition

Posted 23 March 2009 - 12:14 PM

View Postgabehabe, on 23 Mar, 2009 - 11:03 AM, said:

We don't tend to give out code, but the easiest way to demonstrate something so simple is with an example.

Basically, all you need to do is loop 100 times and put a random number in the array.

#include <iostream>
#include <ctime>

using namespace std;

int main() {
    srand(time(0)); // seed the random number (better results)
    int array[100] = {0}; // declare an array of 100 elements
    for(int i = 0; i < 100; i++) { // loop 100 times
        array[i] = rand(); // put a random number in the array
    }

    // the next loop will output each value in the array on a line
    for(int i = 0; i < 100; i++) {
        cout << array[i] << endl;
    }

    cin.get(); // hold the window open
    return EXIT_SUCCESS; // successful execution
}

Hope this helps :)

sweet thanks! that helps out alot with my first problem. now i just need to understand the exercise 10.9. can you help me out with that? :) I don't need the answer i just need a push in the right direction.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: program C++ 6th edition

Posted 23 March 2009 - 12:34 PM

Well, you'll be wanting an array of bools.

A trimmed-down version of what you're looking for could look something like this:
class IntegerSet {
    public:
        IntegerSet(); // default constructor
        void insert_element(int); // add a number
        void remove_element(int); // remove a number
        void print_set(void);
    private:
        bool array[100];
};

IntegerSet::IntegerSet() { // the constructor
    this->array = {false}; // set everything to false
}

void IntegerSet::insert_element(int x) {
    this->array[x] = true;
}

void IntegerSet::remove_element(int x) {
    this->array[x] = false;
}

void IntegerSet::print_set(void) {
    for(int i = 0; i < 100; i++) {
        if(this->array[x] == true) // if the number is held
            cout << i;
        }
    }
}

That oughtta get you on your way. :)
(Note that I typed it in the quick edit box here, it could be full of errors)
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: program C++ 6th edition

Posted 23 March 2009 - 03:09 PM

Sarcasm post: You'd think after 6 editions, he'd just write a new book! :D
Was This Post Helpful? 0
  • +
  • -

#6 Telaco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 09

Re: program C++ 6th edition

Posted 23 March 2009 - 07:42 PM

View PostHyper, on 23 Mar, 2009 - 02:09 PM, said:

Sarcasm post: You'd think after 6 editions, he'd just write a new book! :D


lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1