Anonymous
Anonymous
But if I write like this
Anonymous
Krishna loves Radha and wants to propose her in a different way. He wants to tell her the message, but he wanted the message to be a surprise. So, he plans to encrypt the message and send the encrypted message to Radha and he also gives the key to decrypt. He thinks that this might be a surprise to Radha. Help Radha to decrypt the message.
Rules of encryption :
0<=key<=51
The original message only consists of a-z, A-Z, and space.
Spaces are encrypted as '.'
If key lies between 0 and 25, then the character is added with the key and modulo is taken and the case is maintained. For eg, if key is 3, then a is decrypted to d and e is decrypted to h.
If key lies between 26 and 51, then the character is added with the key and modulo is taken and the case is inverted. For eg, if key is 29, then a is decrypted to D and E is decrypted to h.
Input
First line consists of t, the number of test cases. (1<=t<=20)
For each test case, first line consists of key. 0<=key<=51
Second line consists of the encrypted message S. |S| <= 100000
Output
For each test case, display the decrypted message in a separate line.
Anonymous
But if I write like this
#include <iostream>
int main()
{
int sum = 0, val = 1;
while (val <= 10) {
Difference val++;
Difference sum += val;
}
std::cout << "Sum of 1 to 10 inclusively is " << sum << std::endl;
return 0;
}
Anonymous
klimi
You switched 2 lines
klimi
In first code you sum 1 + 2 + 3 .... And in second you sum 2 + 3 + 4 ....
Anonymous
Anonymous
for(int value = 0; value <= 10; value++) {
sum += value;
}
for(int value = 0; value <= 10; value++)
sum += value;
Which one is a good habit? Including curly brackets or not?
Anonymous
Language:
c
Source:
#include <stdio.h>
int main() {
char arr[3] = "hell";
puts(arr);
}
Warnings:
1015575703/source.c: In function ‘main’:
1015575703/source.c:4:19: warning: initializer-string for array of chars is too long
char arr[3] = "hell";
^~~~~~
Result:
hel
Note:
gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Anonymous
Anonymous
Anonymous
It says that the string ends here
if , suppose
char arr[] = "hell";
outputs
hell
I haven't given '\0' in it,
how did it know it ends there ?
Nameful
Anonymous
Nameful
Anonymous
Send source
#include <stdio.h>
int main() {
char arr[] = "hell";
puts(arr);
}
Anonymous
I was using an online compiler
Nameful
Language:
c
Source:
#include <stdio.h>
int main() {
char arr[] = "hell";
puts(arr);
}
Result:
hell
Note:
gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Pavel
But it is not printed when you print the string because it is a terminator (the idea is that the sting to work on is everything from the beginning and before the terminator)
Anonymous
Pavel
Nameful
Language:
c
Source:
#include <stdio.h>
int main() {
char arr[4] = {'h', 'e', 'l', 'l'};
puts(arr);
}
Result:
hell
Note:
gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Nameful
Interesting …
Anonymous
As far as I know
where does it get the space to put that character in the string?
klimi
movl $1819043176, -12(%rbp)
leaq -12(%rbp), %rax
movq %rax, %rdi
call puts@PLT
Pavel
where does it get the space to put that character in the string?
it allocates one more char when you ask it for char[], basically "hell" can use 5 chars to be stored (not sure if it always should)
I've updated the code above to show how this '\0' affects the logic, note that sizeof(arr) is five
klimi
i don't see anywhere allocating 5 chars hm
Pavel
klimi
i think p means pointer... so... hm
Anonymous
klimi
i think that compillers is smart enough than when it sees it is only 4 bytes it ends the stream and doesnt run into memory
Pavel
Pavel
Anonymous
First variant
but sizeoff(arr) is 4,
that fills the "hell" in "hello"
so you did they put a "/0" in an already filled arr ?
klimi
I think its undefined behaviour
klimi
And we are lucky cuz there was 0
Nameful
Maybe the bot zeroes the memory
Nameful
To prevent leaking information
klimi
write(1, "hell\n", 5) = 5
Pavel
klimi
klimi
in memory there is nullbyte! aaaaaaaaaaaaaa
klimi
yes... it is undefined behaviour it reads next bytes
klimi
char arr[4] = {'h', 'e', 'l', 'l'};
char next[4] = {'A','A','A','A'};
puts(arr);
klimi@14are05~/testing$ ./a.out
hellAAAA
Pavel
klimi
klimi
they are saved onto stack, so you create 4bytes and 4bytes
Ammar
klimi
they should be next to each other
Ammar
Or even elided if the optimization is on.
klimi
idk
klimi
but thats not the point
Ammar
ammarfaizi2@integral:/tmp$ cat test.c
#include <stdio.h>
int main()
{
char arr[4] = {'h', 'e', 'l', 'l'};
char next[4] = {'A','A','A','A'};
puts(arr);
}
ammarfaizi2@integral:/tmp$ gcc test.c -O0 -otest
ammarfaizi2@integral:/tmp$ ./test
hellAAAA
ammarfaizi2@integral:/tmp$ gcc test.c -O3 -otest
ammarfaizi2@integral:/tmp$ ./test
hell
ammarfaizi2@integral:/tmp$
Pavel
C++ question
Do we have some way of rounding/flooring float with some amount of digits after zero (not necessary decimal digits)? Something better than two multiplications and round/floor (static_cast<float>(static_cast<int>(floatVal * 100))*0.01f), I'm thinking maybe we can directly zero some bits in mantissa depending on value of exponent, or maybe there's already some function for that?
Pavel
My task, I want to store floating points as a key in a map, but they can be slightly off and I want nearly equal values to be stored in the same node of the map
like I want these two to give me the same value from the map:
myMap[shorten(3.1415926)] and myMap[shorten(3.141592)]
and I want to implement this shorten function for that
Igor🇺🇦
Pavel
There is std::hash methods for floats and doubles. Why do you want to shorten them?
Maybe shorten isn't the best name of the function.
I can have some values that are nearly equal, close enough that for my algorithms they are basically the same. But if I store them as key in the map, then they are checked for exact equality.
I don't know how std::hash implemented, but I expected it to return the same hash (if no collisions) only for exactly equal floats, if it returns the same hash for nearly equal floats, then it is probably what I need.
Igor🇺🇦
Pavel
Ehsan
Ehsan
When you assign a float it’s 32 bit whether you round it or not
Pavel
Ehsan
32 bit is 32 bit
Ehsan
And float operations are expensive
Anonymous
Here's how it looks, note that first time it prints two strings, it's because the strings are going one after another in memory (because they are in the same struct) and the first one lacking '\0' at the end
https://ideone.com/InKWga
wait, so idk if my understanding is correct or not, but from observing that example code,
so, suppose,
char myCharArr[4] = "hello world";
when it is printed it shows "hell".
now myCharArr[4] doesn't have a \0
and by default assuming that the char someGarbage[] = "\0";
then,
memcpy(data.shortArr, myCharArr, sizeof(myCharArr));
memcpy(data.someGarbage, someGarbage, sizeof(someGarbage));
Now, this will make data.shortArr "hell\0" (memory is next to each other since they are in same struct)
and finally when printing it was able to print
"hell"
Is my understanding correct? Anything wrong here?
All of this based on your https://ideone.com/InKWga
Pavel
wait, so idk if my understanding is correct or not, but from observing that example code,
so, suppose,
char myCharArr[4] = "hello world";
when it is printed it shows "hell".
now myCharArr[4] doesn't have a \0
and by default assuming that the char someGarbage[] = "\0";
then,
memcpy(data.shortArr, myCharArr, sizeof(myCharArr));
memcpy(data.someGarbage, someGarbage, sizeof(someGarbage));
Now, this will make data.shortArr "hell\0" (memory is next to each other since they are in same struct)
and finally when printing it was able to print
"hell"
Is my understanding correct? Anything wrong here?
All of this based on your https://ideone.com/InKWga
I specified the sizes of the arrays so
char myCharArr[4] = "hello world";
assigns only the first 4 chars without '\0'.
Then memcpy copies this data "as is" to data.shortArr. So this array has only this 4 chars.
someGarbage is an arrays of 16 chars, when I assign "some garbage" there, it sets first 12 chars to represent the symbols of the string, then sets 13th char to zero ('\0'), the last three chars probably left as zero (not sure if in C they are initialized or not to zero), but we don't care at all here about these last three chars. When I memcpy the someGarbage array I copy all 16 chars "as is".
What I wanted to show, that if you print your array of 4 chars withour '\0' at the end, then it's matter of luck whether it will be printed correctly or not (as in klimi's example there can be non-zero memory after your array on the stack and then it will interpret memory as part of your string until it find a zero byte).
You don't want to rely on something that works by luck.