Let's consider the encryption formula given in the problem set:
cᵢ = (pᵢ + k) % 26
Here, as in the problem set,cᵢ represents the value of the encrypted or enciphered letter, pᵢ represents the value of the plain text letter, and k represents the value of the encryption key.
The English alphabet has 26 letters. Let's represent those letters with the ...
Modulo is working correctly, but you've overlooked something. The idea behind using modulo is this. When you represent letters as numbers between 0 and 25 inclusive, and add a number to shift the letter, it may go beyond 25. You can apply % 26 to wrap back to the 0 to 25 range. But it ONLY works if the letters are represented by numbers in that range.
% 26 will always give you a number between 0 and 25. The Caesar algorithm works on the assumption that your letters A-Z (or a-z) are represented by the numbers 0-25, so if you have 'z' (25) and a key of 7, 25 + 7 is 32. 32 is not within the range, but 32 % 26 is 6, which represents 'g'. Remember that the letters wrap around from 'z' back to 'a' much like a ...
The purpose of the formula is to calculate the new (cyphered) value for a given character on a string, considering a swift value.
The formula has 4 components:
ci = ciphered character
pi = current position
k = number of positions to be shifted
26 = number or alphabet characters
There's is something important here. The formula assumes that the first ...
If you tried typecasting to int, still the error would have existed because you are still storing the value in a float. So its basically like,
original value = 12.27
round()'s output = 1227.00
Typecasted result = 1227
Storing the typecasted value back in a float variable = 1227.00
And modulo operations don't work with floating point numbers. So, its ...
That's how the remainder operator just works. You may think of it that way:
given a = 3 and b = 2, the expression
a % b // results in 1
because a 3 can have a maximum of a single (i.e., one) 2 in it. Now what's the remainder? It's the difference between 3 and (2 x 1) and that's definitely 1.
Now let a = 2 and b = 3, the expression
2 % 3 // results in 2
I am among those people who suggest you to use 2 loop variables, actually its 1. Use a variable (say i) to iterate over each element of the input string and maintain another variable (say j) to hover over the key in the same loop. Consider the string to be array, then to eliminate wrapping over non-alphabetical characters, do the following in loop.
In order to use modulo to wrap around the dictionary you first need to offset both key and plaintext to convert them to values between 0 and 25, and then add them and use modulo. That part is just as it is in caesar, with the only difference that instead of converting only the plaintext, you need to convert both because the key is also an ASCII character, ...
Why are you using modulo for this? It produces an unreliable result in this usage. Further, the actual numbers don't make sense.
The modulo function returns the remainder after an integer division. So, for example, say that you're doing while (cents % 10 == 5) If cents were $1.05, it would be true, but if cents were $1.06, it would be false. In both ...
de = 1 (mod m)
where e is equal to 5 and m is equal to 924 means
(5 * d) % 924 = 1
Thomas later in the video used the extended Euclidean algorithm to find a value of d that would satisfy the equation above. However, in this specific example, you could find a value of d that would satisfy the equation easily since 925 % 924 = 1 and we're lucky that e is ...
test.c:35:49: error: invalid operands to binary expression ('double' and 'double')
printf("%lld\n", ((l / (pow(10.00, i))) % 10));
~~~~~~~~~~~~~~~~~~~~~ ^ ~~
1 error generated.
It's really telling you that there's a problem with the modulo operator. See where the pointer is under the % operator?
Modulo is an integer ...
Welcome to the world of (binary) floating point. A value like 4.2 cannot be precisely represented in binary, it would be represented by an infinite sequence of binary digits. Obviously we don't have an infinite amount of RAM, so the number is cut off at some point, resulting in a binary number that's slightly above or below the value you wanted to express, ...
You have created an infinite loop. If the loop starts, it will never exit the loop. It tests the value contained in amount to decide to continue or exit, but since the value of amount is not changed inside the loop, it will never exit. You either need to change the value of amount inside the loop or you need to use an if statement instead, so that it only ...
Please have another look at the second hint of this exercise (https://docs.cs50.net/2018/x/psets/1/cash/cash.html#hints).
After you have followed that advise, you will be able to use the modulo operator like you planned ...
And to quote Cliff B:
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;...
If you want to print something different based on a condition, you need to use a conditional like an if. As written this program will only ever output (sign) and 5 dashes. Full stop.
This does not look like an exercise from CS50x (or CS50ap). This forum focuses specifically on CS50 If you want to work on your skills in the c programming language, CS50x on ...
the definition of the pow function says that it returns a double that is the reason of your error, since the operator modulo requires integers, it is possible that it works if we do a casting although I am not sure:
odd = number % (int) round((pow (10, i))- number % (int) round((pow (10, i-1))) / (int)round(pow (10, i-1));
You could simply use coins as the variable that holds the coin value and add to it with each equation.
coins = 0;
coins += amount/25; ...
coins += amount/10 .... etc etc.
Then, at the end, coins will already be the total.
Take a close look at this line and think very carefully about what the modulo operation is being applied to:
int cipherKey = (((key[i]) - 'A') % KeyLen);
It coverts a key letter to a number between 0 and 25, and then tries to wrap around based on key length, by applying the % operation to the value of the key, not the position.
This has two bad results. ...
You're creating multiple shadow variables in the code. A shadow variable is a new var with the same name as one that has been created earlier, but the new var is created in a place where it will have smaller scope. The shadow var will mask the original var that has greater scope. This would normally generate a warning, but the IDE treats a warning as an ...
By the let me tell you that your program is not working at all
It's showing error something like this
b.c:16:10: runtime error: null pointer passed as argument 1, which is declared to never be null
/usr/include/string.h:400:33: note: nonnull attribute specified here
However you can make changes
Something like this, instead of doing %...
You were correct. You must use the modulus operator to ensure that the text output is between A and Z. However, you are wrapping the key, so that it is from A to Z. You must wrap the output. So instead of
(p[i] + (k[i]-'A') % 26) + p[i]
Use something like
((p[i] - 'A') + (k[i] - 'A')) % 26 + 'A'
To first add up the distances from A, ensure this sum of ...
another way to do it
j = j % v;
where j is the jth char of key and v is strlen of key. when placed at the end of your shift functions within your if statement, j will increment only when you've executed your shift functions. so if plaintext[i] is not alpha it doesn't increment.
as opposed to
j = i % v;
while it will wrap, it increments j ...
A few errors to point out:
your code is difficult to read because it has not been indented properly.
I believe your formula for shifting the characters is wrong. Lets say we take 'z' which is 122 on the ascii table. subtract 'a', which is 97, and add a arbitrary key of maybe 13, followed by 'a' again. This gives a result of 135.
you should avoid placing ...
Lets find pow(x,y)%n
Wikipedia lists this property here.
Now, since we know how to find the modulus of product of 2 big numbers, then we can use this identity in the following way to do it for the exponential functions.
(a*b) % n = (a%n * b%n) % n
=> (a*b*c) % n = (((a*b) % n) * (c % n) ) % n
=> (a*b*c) % n = (((( a%n * b%n ) % n) * (c%n)) % n
After watching the short on RSA, I guess you didn't mean the XOR bitwise operator by the symbol (^), but rather, you meant exponent.
The powl() function (declared in math.h) can be useful for that matter. You may run
in the terminal for more information about this function.
I will leave the information I gave before about the XOR bitwise ...