Okay, I have this program I have to write. I think I get the basis of the idea, but I'm a little confused about parts of the instructions. Here's the instructions.

Write a C++ program that uses bit strings to represent sets, and finds A^B, AUB, A-B, where A and B are sets of strings. Read A and B from a file of two lines. Do not hardcode the file name, instead, prompt it from the user. The first line contains elements of A, the second line contains elements of B. Elements are separated by exactly one blank.

I get everything except the first line. I have absolutely no clue how they want me to use bit strings to represent the sets.

I figured I'd read in the lines into two arrays and then compare them that way, but is that the right way to approach this or am I doing this wrong?

Recommended Answers

All 4 Replies

Your assignment is the kind of exactingly vague description I've come to expect from CS instructors. Do you have an exemplar of the input file? I will assume some things from here on:

A bit string is more commonly called a binary number (integer types).

Hence, the number "10" is the bitstring 1010 (msb to lsb).
For a number, the bits and values are:

{bit 0} = {2^{0}} = 1\\
{bit 1} = {2^{1}} = 2\\
{bit 2} = {2^{2}} = 4\\
{bit 3} = {2^{3}} = 8

etc, so 1010 is 8 + 0 + 2 + 0 = 10.
(Sorry, the LATEX capabilities on the forum are very basic.)

You don't have to associate powers of two with each bit. You can associate other things instead. For example:

"I eat green eggs and ham":
bit 0 = "I"
bit 1 = "eat"
bit 2 = "green"
bit 3 = "eggs"
bit 4 = "and"
bit 5 = "ham"

So the sentence could be represented as 00 0011 1111 (read that from right to left).
Let's add another:

"I don't like green eggs and ham, Sam I am"
bit 6 = "don't"
bit 7 = "like"
bit 8 = "Sam"
bit 9 = "am"

Notice how we didn't need to assign new bits for values that are already associated with a particular bit. The second sentence would then be: 11 1111 1101 . Notice how that "I", as a member of a set, only gets counted once (in bit 0).

Now you can find the difference between the two sets:

00 0011 1111    ham and eggs green eat I
XOR 11 1111 1101    am Sam like don't ham and eggs green I
----------------
    11 1100 0010    eat don't Sam am

Etc.

Make sure you have an idea of:
1) what kinds of items you can expect in your input files
2) the maximum number (if any) of distinct items per file (line 1 OR line 2)

Hope this helps.

No, he hasn't given us any example of what the file would look like, hence why I'm having issues with this.

Okay, I sorta get what you mean for the binary thing, but I'm a little lost on how to make it work when reading from a file. Should I put the two lines from the file into two arrays and then figure out the binary code for each set. Like, in your code.

I eat green eggs and ham
I don't like green eggs and ham, Sam I am

How each reads to this.
00 0011 1111
11 1111 1101

If I have those two lines of code, would I have to make something that would read the two arrays and make sure not to use the same word for two different places (like in the case of "I, green, eggs, and, ham")?

And, for setting the strings equal to a bit, can you simply do it by using
bit # = <something>
or is there another way to do this, cause I have no clue how long this list is going to get so I need to use a variable for the # place.

All I know about the file is we are reading in strings and I believe it is less the 100, that's all he gave us really.

I think you'll have to go ask your professor for clarification. Sorry.

Oh, the bit position is indexed like an element of an array. So if your bits do represent strings (as in vector <string> values; ), then bit zero represents values[ 0 ], bit 1 represents values[ 1 ], etc. A set of those values is then just a number.

(also, I hope you realize that I added spaces in the number for readability: 00 0011 1111 instead of 0000111111).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.