# program C++ 6th edition

Page 1 of 1

## 5 Replies - 2812 Views - Last Post: 23 March 2009 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=94843&amp;s=64b3665f94bd99d96d7a9841a1ecf3aa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Telaco

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

# program C++ 6th edition

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

Is This A Good Question/Topic? 0

## Replies To: program C++ 6th edition

### #2 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• 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

### #3 Telaco

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

## Re: program C++ 6th edition

Posted 23 March 2009 - 12:14 PM

gabehabe, 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.

### #4 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• 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)

### #5 Hyper

• Banned

Reputation: 108
• 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!

### #6 Telaco

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

## Re: program C++ 6th edition

Posted 23 March 2009 - 07:42 PM

Hyper, on 23 Mar, 2009 - 02:09 PM, said:

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

lol