| ags792 (9) | |||
|
I'm having some trouble figuring out a C++ problem in my book. This is the part I really don't understand: "Note that what you encode cannot be directly decoded, as there is not a one-to-one correspondence". The way I was going to try to do this was by assigning each character a value and checking that to output the code. I'm completely confused as to how to solve this. Thanks for the help. Here it is: Background Info:
Assignment:
and here is the image: [IMG]http://i.imgur.com/fHu6d.png[/IMG] | |||
|
Last edited on
|
|||
| pogrady (410) | |||
There isn't really anything to solve, necessarily. You have a collection of data that gets operated on by an algorithm. The data is organized by an index, in this case the then number on the telephone. Each number has with it an associated string, or list of characters. here is the algorithm:
| |||
|
Last edited on
|
|||
| ags792 (9) | |
|
That's what I thought, but this threw me off: "Note that what you encode cannot be directly decoded, as there is not a one-to-one correspondence" Are you sure that's right? Thanks | |
|
|
|
| pogrady (410) | |
|
usually an encoded message relies on some sort of letter to letter mutation. For example, the letter 'b' might be replaced with the letter 'a'. By knowing what the offset is, you can write a program that will decode the message. In this case the translation would be a character by character shift of the alphabet. This is a simple cypher, and is called a Caesar Shift. xtllx xtllx xtllx h fns knud hm lx stllx Since the type of encoding that you want to reproduce does not rely on a shifting of the alphabet, a mathematical interpretation is somewhat complex. The solution set for each element is three. the number 2 can either be 'a' or 'b' or 'c'. Since 1 can be any of the three letters, its difficult for the program to come to a reasonable outcome. This is where the human needs to interact with the result to know what the meaningful output is. The sentence "I like Brownies" means nothing to the computer. Its a string of numeric representations in memory: 73 108 105 107 101 66 114 111 110 105 101 115 These numbers have no meaning to the computer either. These numbers are representations of this: 1001001 1101100 1101001 1101011 1100101 1000010 1110010 1101111 1101110 1101001 1100101 1110011 Since it means nothing, the program cannot look at the sentence and expound "that's the answer" because it doesn't know what a brownie is. Only you do. | |
|
|
|
| ags792 (9) | |||
This is what I have so far:
I know it's not anywhere near done, but I'm not sure where to go from here. I understand what I have to do now, but not how to do it in C++. | |||
|
Last edited on
|
|||
| pogrady (410) | |||
|
the easiest way might be to check the value of each character against the ASCII code. If it lies within a certain range, assign it the number that corresponds to the letter. For example:
look here for values: http://www.asciitable.com/ | |||
|
|
|||
| ags792 (9) | |
|
I have to use the values on the phone in the image I posted, not ASCII. It's a rotary phone and it's not the same as a regular cell phone keyboard, but it's close. So for an example L would be 555 and J would be 5. Thanks for the help. | |
|
|
|
| pogrady (410) | |
|
Yes, but when the user enters the string as the programmer you need to be able to determine what they entered. One of the ways you can do this is by using the ASCII values. So if the user inputted 'a' you need to know that, and you can check if they did by using the ASCII code for 'a', which is 97. Does this make sense? (nice. This is post 404. Can you see this post? Otherwise you'll need to contact the system administrator. lol) | |
|
Last edited on
|
|
| ags792 (9) | |
|
Thanks, I can see the post. So I get an input, convert it to ASCII, and then display the ASCII in a message? Didn't make sense at first, but I guess it does now, otherwise it would get confusing with a J and an L next to each other for example. So can you show me how to combine what you posted above with what I already wrote? or will that not work? Thanks again for all the help. Been stuck on this problem for a while now. | |
|
|
|
| pogrady (410) | |||||
|
Well, what you are doing is using the equivalent ASCII value to determine what the encoded character is. So for J and L the ASCII range would be 74 through 76. If the character is within this range, you add to the the encoded string a '5'. The computer doesn't know what 'J' is, but it knows 74. Or you could do this:
Its essentially the same thing. anyway... I'm not sure if this is homework, but its better and easier to use the string class. to do so you'll need to include <string>.
| |||||
|
Last edited on
|
|||||
| cire (1845) | ||||
Based on the text of the assignment, L, J and K would each be 5.
| ||||
|
|
||||
| pogrady (410) | |
| I see. Then all he has to do is loop through each element to determine it corresponding representation. | |
|
|
|
| ags792 (9) | |
|
Thanks for posting, but I haven't learned to use structures yet. I'm going to search for them online and if I can figure them out and code the for loops. Thanks again | |
|
|
|
| ags792 (9) | |
|
Can't figure it out and get it working. I'm going to try pogrady's way instead. Thanks for posting though. | |
|
|
|
| ags792 (9) | |||
Do I have to change char to a new variable I made? Won't it think 'char' is a data type instead of a variable?
| |||
|
Last edited on
|
|||