My new tagline --Quote:
Originally Posted by Nathand
Have you debugged your code?
You've written the program and it runs. Debugging is part of the programming process.
Regards,
Paul McKenzie
Printable View
My new tagline --Quote:
Originally Posted by Nathand
Have you debugged your code?
You've written the program and it runs. Debugging is part of the programming process.
Regards,
Paul McKenzie
What exactly do you mean by "debugged" your code?
It's due to the fact that ('Y' + the_original_encoding_key) / 26 was equal to 1+(any_other_letter_of_the_string + the_original_encoding_key)/26Quote:
Originally Posted by Nathand
In other words, 'Y' wrapped around once while other characters don't.
Actually, it's easy to see that the_original_encoding_key==4.
But, your current algorithm is incorrect... It doesn't compute things modulo 26.
Since the_decoding_key is -4 or 26-4==22 modulo 26, your program shouldn't work at all... In fact, you should never screen any value that re-creates anything looking a bit with the original text.
But an amazing thing happened! The lower case a-z characters are above the upper-case A-Z characters in the ASCII table, and, 'a'='A'+32, 'b'='B'+32
So, for decoding_key==28, your program will output something that really looks like the original text, except that you've lower case letters, and 'Y' will be translated to 'y'-26=='a'-2=='_'
But, you'll say that you don't test for decoding_key==28...
But, you do!
You iteratively modify the original string, and consequently, you're testing for decoding_key==0, decoding_key==1, decoding_key==3, decoding_key==6, decoding_key=10 and so on...
0+1+2+3+4+5+6+7 happens to be equal to 28
So, your 8th 'deciphered string' is equal to _ourareterminated
Amazing, isn't it?
Should work better (warning, it has not been tested, currently I've no compiler under the hand).Code:#include <iostream>
#include <string>
int main()
{
std::string s = "CSYEVIXIVQMREXIH";
std::cout << s << std::endl;
for(int i = 0; i < 26; i++)
{
for(int index=0; index < s.length(); index++)
{
s[ index ] = 'A'+(s[ index ]-'A' + 1)%26;
}
std::cout << s << std::endl;
}
system("PAUSE");
return 0;
}
PS: the 'beeping' is probably character #7 that you get because you end up adding large numbers to your characters that wrap around 256.
Programming isn't just writing code, running the code, and then hoping everything works. Part of programming is called "debugging", which means finding the errors in the logic in your code. Every programmer must know how to debug their own code.Quote:
Originally Posted by Nathand
This is done by using printf() or cout() statements, or using the debugging facilities provided to you by the compiler you're using, or just using pencil and paper to step through the logic in the code. These are skills that should be learned on the first, if not second day of programming.
What if the program was much more complex? How will you find the error? How do programmers such as SupeKoko and others able to find the problem?
The bottom line is that you have a running program, and it doesn't produce the output or results you wanted, then take it on yourself to figure it out by debugging the code. Sure, you got an instant answer, but IMO this takes away from you actually learning the skills needed for programming, especially if you come across another problem. However, sometimes the reasons for the error are not obvious, even during debugging -- then and only then should you call for help in figuring out a problem.
Regards,
Paul McKenzie
Take a look at the following thread - http://www.codeguru.com/forum/showthread.php?t=398759
Basically, the code in this post - http://www.codeguru.com/forum/showpo...46&postcount=6
SuperKoko - Thank you for your very informative post. I'm still trying to figure out what you said :)
I really appreciate all the help,
Nathan
SuperKoko - In your post you said:
"It's due to the fact that ('Y' + the_original_encoding_key) / 26 was equal to 1+(any_other_letter_of_the_string + the_original_encoding_key)/26."
In C++ would that beBecause I tried that and got two different values?Code:#include <iostream>
#include <string>
int main()
{
std::string s = "Y";
s[0] = (s[0] - 4) / 26;
std::cout << s;
s[0] = 1 + (86 - 4)/26;
std::cout << s;
system("PAUSE");
}
Sorry for not being good at programming :(
-Nathan
I was refering to the non-C++ values of characters in the alphabetical table.Quote:
Originally Posted by Nathand
In C++, it would be:
In other words:Code:#include <iostream>
int main()
{
std::cout << (('Y'-'A')+4)/26 << " equal " << 1+(('O'-'A')+4)/26 << '\n'; // untested, but it should display: 1 equal 1
}
'Y'-'A' == 24 (Y is the character #24 in the alphabetical table, starting from zero).
'Y'-'A'+4 == 28 and consequently, it should wrap around with a correctly written caesar encoding algorithm.
i.e. (('Y'-'A')+4)/26==1
For other characters, the base alphabetical value is smaller than 26-4, and consequently, after being added 4, they're still below 26 and ((the_character-'A')+4)/26==0 and ((the_character-'A')+4)%26==
((the_character-'A')+4).
Assuming the key is any number < 26 or >=26 having a remainder equal to 4 modulo 26, the general C++ formula is:
(('Y'-'A')+key)/26 == 1+(('O'-'A')+key)/26
And, given in non-C++ terms (a formula applying on usual alphabetic codes: 'A'=0, 'B'=1, etc.):
('Y'+key)/26 = 1+('O'+key)/26
That is just a mean to say that 'Y' is not added the same absolute value than other characters in the original text sample.
That explains why you can have a resulting "decoded" string that looks a bit like the original except for the 'Y' character.
Ok, thanks for the help.
I'm having a little trouble understanding this code:
Because the code above (which you posted), prints "1 equal 1" just like you said it would.Code:std::cout << (('Y'-'A')+4)/26 << " equal " << 1+(('O'-'A')+4)/26 << '\n';
But this code:
Prints "28 equal 19". Which doesn't make sense to me, considering the only things I removed were theCode:std::cout << (('Y'-'A')+4) << " equal " << 1+(('O'-'A')+4) << '\n';
So what I'm having trouble understanding is why 28 dived by 26 (1.07) equals 19 divided by 26 (0.73)?Code:/26
Thank you for the help.
/ is the integer division.Quote:
Originally Posted by Nathand
In other words, it returns the quotient of the euclidian division.
28 divided by 26 has a quotient equal to 1
18 divided by 26 has a quotient equal to 0, and, adding 1 gives 1.
Note that division has a higher precedence than addition.
Consequently:
1+(('O'-'A')+4)/26
Is NOT 19/26
It is interpreted as:
1+((('O'-'A')+4)/26)
And is equal to 1+(18/26), NOT (1+18)/26
Consequently, it is equal to 1+0
And, 1+0=1 :)
Right! *smacks himself*
Because when using integers you don't use decimal points right? Just whole numbers?