Anonymous
sorray, O(n)
Anonymous
seem to O(n)
Anonymous
yep, if use it, can be O(n)
Anonymous
bc just for n time
Anonymous
create a linked list is a way, but it will increase the amount of code
Anonymous
Yeah, but I think linked list is optimal for space😂
Anonymous
C is too low-level. I really love and hate
Anonymous
😂STL really a nice mind, iI really admire whoever came up with this
Anonymous
how to find the the corresponding value?
İbn
#include<iostream> #include<string> using namespace std; int main() { int arr1[] = {1, 2, 3}; int hash_table[N] = {0}; if (arr1[i] == 1) hash_table[0]++ if (arr1[i] == 2) hash_table[1]++ .... if arr1[i] == 1 hash_table[0]++ ... if hash_table[i] == 2 the i number is wrong return 0;}
Anonymous
The code I wrote is pseudo code and only has a solution
Anonymous
I just borrowed the code to express my thinking
Anonymous
haha
Anonymous
if u can do cpp, u use the unordered_map is a good way just Mark said
Anonymous
u can look through the records
İbn
Ok..i will try
Anonymous
Don't worry, the sense of achievement u've made is amazing
Anonymous
#include<iostream> #include<string> using namespace std; int main() { int arr1[] = {1, 2, 3}; int hash_table[N] = {0}; if (arr1[i] == 1) hash_table[0]++ if (arr1[i] == 2) hash_table[1]++ .... if arr1[i] == 1 hash_table[0]++ ... if hash_table[i] == 2 the i number is wrong return 0;}
/*When you are to assign default value to null, you no longer need to {0}. That's enough {}*/#include<iostream> #include<string> using namespace std; int main() { ... int hash_table[N] = {}; ... return 0;}
\Device\NUL
Hello, is it possible to allocate and free heap memory using kernel syscall directly (without libc) ?
Anonymous
haha,just habit to write{0}
Anonymous
This will let me know the meaning of the code more clearly
Pol
Hi !
Pol
I just joined the group.
Danya🔥
The message from @drupol: I'm looking for some help with a C program that I'm trying to do. Basically I would like to create an implode function where I could print an array of integers with a separator in between elements. I did this, but for some reason, it won't work and I'm unable to debug in CLion: https://gist.github.com/drupol/97467ea7214e4118623c4f6116d332b0 could you give me some clue ?
Pol
Ah nice ! thanks :) I was about to rewrite the message without the link !
Pol
Do you think anyone can help? I'm stuck on this thing and I can't figure out how to fix it.
Danya🔥
Ah nice ! thanks :) I was about to rewrite the message without the link !
> strcat(str, arr[i]); Are you sure that it is going to work as you implied?
Danya🔥
And I'm going to answer it: no, arr[i] is an int strcat doesn't accept ints
Danya🔥
I don't think it should even compile
Danya🔥
Anyways, you should use itoa or sprintf
Pol
Wait, trying ! :)
Pol
Anyways, you should use itoa or sprintf
sprintf instead of strcat ?
Danya🔥
sprintf instead of strcat ?
Yes Or itoa + strcat
Danya🔥
Also, are you sure that sprintf can accept NULL as the first argument?
Pol
I don't know that, I'm learning C
Pol
I did this: sprintf(str, "%s%s%d", str, separator, arr[i]);
Pol
But I'm absolutely not sure yet.
Pol
I'm a bit discouraged when I see that this kind of stuff is so complex in C
Pol
Got it working !
Pol
for (int i = 0; i < size; i++) { if (i == size - 1) { sprintf(str, "%s%d", str, arr[i]); } else { sprintf(str, "%s%d%s", str, arr[i], separator); } }
Danya🔥
for (int i = 0; i < size; i++) { const char* suffix = (i < size-1) ? separator : ""; sprintf(str, "%s%d%s", str, arr[i], suffix); }
Pol
beautiful, trying.
Danya🔥
for (int i = 0; i < size; i++) { const char* suffix = (i < size-1) ? separator : ""; str += sprintf(str, "%d%s", arr[i], suffix); }
Danya🔥
@drupol try this
Pol
@drupol try this
Doesn't print anything this time. But I agree that rewriting str is inefficient.
Danya🔥
Doesn't print anything this time. But I agree that rewriting str is inefficient.
Well, I'm not able to check it right now. If you could make it working, that'd be awesome
Danya🔥
I'm trying :) Thanks !
Oh, I guess I know what the problem is
Pol
ahh ?
Danya🔥
ahh ?
sprintf puts the terminating zero after writing to the string Probably
Danya🔥
for (int i = 0; i < size; i++) { const char* suffix = (i < size-1) ? separator : ""; str += sprintf(str, "%d%s", arr[i], suffix); str--; }
Danya🔥
So probably we can hack it with - 1
Pol
So probably we can hack it with - 1
nope, this only prints 1 digit, nothing else.
Danya🔥
The code is different
Pol
I could get rid of '\0' ?
Pol
trying
Pol
Same result.
Danya🔥
Weird
Danya🔥
for (int i = 0; i < size; i++) { const char* suffix = (i < size-1) ? separator : ""; sprintf(str, "%s%d%s", str, arr[i], suffix); }
Also, the initial example is not correct, @drupol If a call to sprintf or snprintf causes copying to take place between objects that overlap, the behavior is undefined (e.g. sprintf(buf, "%s text", buf);)
Pol
ok fair enough, going to fix this
Danya🔥
So I just suggest it to rewrite to strcat+itoa or make my last try working
Pol
Ok, checking this out.
Danya🔥
for (int i = 0; i < size; i++) { const char* suffix = (i < size-1) ? separator : ""; str += snprintf(str, size, "%d%s", arr[i], suffix); str--; // try with this line and without }
Danya🔥
This could work
Pol
Nope, only prints 1 single digit, nothing else.
Pol
I added a todo (in french!)
Pol
for (int i = 0; i < size; i++) { // TODO: Optimizer ce truc pour eviter de devoir re-ecrire str sprintf(str, "%s%d%s", str, arr[i], (i < size-1) ? separator : ""); }
Pol
I don't have itoa.
Pol
how come?
Anonymous
haha,just habit to write{0}
But it's better to adopt new language practices.
Pol
Going to use sprintf instead.
Anonymous
Is this the new standard?emm,just {} because the underlying initialization is 0 by default?
Anonymous
I don't have itoa.
itoa is not a standard library function. Some compiler implementations provide it in stdlib.h as an extension