| | |
Bit String help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
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?
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?
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:

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:
So the sentence could be represented as
Let's add another:
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:
Now you can find the difference between the two sets:
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.
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:
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:
C++ Syntax (Toggle Plain Text)
"I eat green eggs and ham": bit 0 = "I" bit 1 = "eat" bit 2 = "green" bit 3 = "eggs" bit 4 = "and" bit 5 = "ham"
00 0011 1111 (read that from right to left).Let's add another:
C++ Syntax (Toggle Plain Text)
"I don't like green eggs and ham, Sam I am" bit 6 = "don't" bit 7 = "like" bit 8 = "Sam" bit 9 = "am"
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:
C++ Syntax (Toggle Plain Text)
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
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.
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
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.
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
(also, I hope you realize that I added spaces in the number for readability: 00 0011 1111 instead of 0000111111).
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.
![]() |
Similar Threads
- multiple variables in a mod_rewrite URL string (Linux Servers and Apache)
- HELP Concert Floating Point to ASCII String... (Assembly)
- Finding the Most Common Character in a String (Java)
- String operatios (C++)
- Conver int Array into a String (Java)
- I would kill for this, just a little bit (C)
- Running a program in a computer with no OS (C)
Other Threads in the C++ Forum
- Previous Thread: counts letters in a word
- Next Thread: Getline help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






