Bit String help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Bit String help

 
0
  #1
Sep 23rd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Bit String help

 
0
  #2
Sep 24th, 2008
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:
<br />
{bit 0} = {2^{0}} = 1\\<br />
{bit 1} = {2^{1}} = 2\\<br />
{bit 2} = {2^{2}} = 4\\<br />
{bit 3} = {2^{3}} = 8<br />
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:
  1. "I eat green eggs and ham":
  2. bit 0 = "I"
  3. bit 1 = "eat"
  4. bit 2 = "green"
  5. bit 3 = "eggs"
  6. bit 4 = "and"
  7. bit 5 = "ham"
So the sentence could be represented as 00 0011 1111 (read that from right to left).
Let's add another:
  1. "I don't like green eggs and ham, Sam I am"
  2. bit 6 = "don't"
  3. bit 7 = "like"
  4. bit 8 = "Sam"
  5. 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:
  1. 00 0011 1111 ham and eggs green eat I
  2. XOR 11 1111 1101 am Sam like don't ham and eggs green I
  3. ----------------
  4. 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.
Last edited by Duoas; Sep 24th, 2008 at 12:40 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: Bit String help

 
0
  #3
Sep 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Bit String help

 
0
  #4
Sep 24th, 2008
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).
Last edited by Duoas; Sep 24th, 2008 at 12:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: Bit String help

 
0
  #5
Sep 24th, 2008
Alright. Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC