Peace
Is this way of binary search is the best efficient version of binary search code ? #include <bits/stdc++.h> using namespace std; int binarySearch(int arr[], int key, int left , int right) { if(left<=right){ int midValue=left + (right-1)/2; if(arr[midValue]==key){ return midValue; } if(arr[midValue]<key){ return binarySearch(arr,key,midValue+1,right); } return binarySearch(arr, key, left,midValue-1); } return -1; } int main(){ int size,key; cout<<"Enter size of array: "; cin>>size; int arr[size]; cout<<"Enter elements of array(sorted array): "; for(int i=0;i<size;++i){ cin>>arr[i]; } cout<<"Enter key: "; cin>>key; cout<<binarySearch(arr,key,0,size-1); return 0; }
Peace
what if I want the very first match element?
I missed that base case 😜😜😜
Nils
hey, any idea what that means: _ZN8ChatFuse7Discord6Client11async_sleepEPZNS1_11async_sleepElE48_ZN8ChatFuse7Discord6Client11async_sleepEl.frame.destroy' referenced in section .rodata.cst8' of _deps/dcboost-build/libdcboost.a(http.cpp.o): defined in discarded section `.text._ZN8ChatFuse7Discord6Client11async_sleepEPZNS1_11async_sleepElE48_ZN8ChatFuse7Discord6Client11async_sleepEl.frame.destroy[_ZN8ChatFuse7Discord6Client11async_sleepEl]' of _deps/dcboost-build/libdcboost.a(http.cpp.o) _ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateEPZNS8_8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_E260_ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_.frame.destroy' referenced in section .rodata.cst8' of _deps/dcboost-build/libdcboost.a(http.cpp.o): defined in discarded section `.text._ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateEPZNS8_8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_E260_ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_.frame.destroy[_ZN8ChatFuse7Discord6Client11async_sleepEl]' of _deps/dcboost-build/libdcboost.a(http.cpp.o) ?
Anonymous
hey, any idea what that means: _ZN8ChatFuse7Discord6Client11async_sleepEPZNS1_11async_sleepElE48_ZN8ChatFuse7Discord6Client11async_sleepEl.frame.destroy' referenced in section .rodata.cst8' of _deps/dcboost-build/libdcboost.a(http.cpp.o): defined in discarded section `.text._ZN8ChatFuse7Discord6Client11async_sleepEPZNS1_11async_sleepElE48_ZN8ChatFuse7Discord6Client11async_sleepEl.frame.destroy[_ZN8ChatFuse7Discord6Client11async_sleepEl]' of _deps/dcboost-build/libdcboost.a(http.cpp.o) _ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateEPZNS8_8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_E260_ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_.frame.destroy' referenced in section .rodata.cst8' of _deps/dcboost-build/libdcboost.a(http.cpp.o): defined in discarded section `.text._ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateEPZNS8_8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_E260_ZN5boost4asio12async_resultINS0_15use_awaitable_tINS0_15any_io_executorEEEJFvNS_6system10error_codeEEEE8initiateINS0_20basic_waitable_timerINSt6chrono3_V212steady_clockENS0_11wait_traitsISD_EES3_E19initiate_async_waitEJEEENS0_9awaitableIvS3_EET_S4_DpT0_.frame.destroy[_ZN8ChatFuse7Discord6Client11async_sleepEl]' of _deps/dcboost-build/libdcboost.a(http.cpp.o) ?
I think that's not the whole error
Nils
That's litterally all of error I get
Anonymous
Maybe definition loop?
Nils
Maybe definition loop?
Uhm, what does that mean? But the error occurs when building in Release mode only. It works just fine in Debug and MinSizeRel
Anonymous
Is the lib statically linked?
Anonymous
Recompile it for release then
Nils
Everything is compiled for release
Anonymous
Emm have u tried praying to Jesus, Allah and Buddha at the same time?
Nils
I even tried a full make clean
Anonymous
Do u compile the lib with the project?
Nils
Emm have u tried praying to Jesus, Allah and Buddha at the same time?
Little update, compiling for Debug deadlocks my other machine
Nils
Little update, compiling for Debug deadlocks my other machine
Been stuck in gcc for quite a while now (about an hour)
Nils
Nevermind! I got an "internal compiler error please report to https://xxxxxx"
Nils
Do u compile the lib with the project?
I can share the full project if you want
Nils
https://gitlab.com/chatfuse/frontends/tobot-dc
Nils
When trying to configure you should to -DMODEL=api -DCORO_LEGACY_FIX=YES
B220059_Subhra_Bhanja
Anyone has labview?
Anonymous
Anyone has labview?
Gw punya cok mau yg c++ versi lama
klimi
Ler
hello, guys. I'm studying the heap memory in C++, and I've ran the code below using Apple clang version 11.0.3 (clang-1103.0.32.62) on x86_64-apple-darwin20.6.0. int main() { auto p0 = new int{42}; auto p1 = new int{56}; cout << "*p0: " << *p0 << endl; cout << "*p1: " << *p1 << endl; delete p0; delete p1; cout <<"\n"; cout << "*p0 after deleted: " << *p0 << endl; cout << "*p1 after deleted: " << *p1 << endl; return 0; }
Ler
And I got this result:
Ler
(base) leroychou@MBAofLeroy test % clang++ main.cpp -o main -std=c++11 (base) leroychou@MBAofLeroy test % ./main *p0: 42 *p1: 56 *p0 after deleted: 0 *p1 after deleted: 56
Ler
I'm very confused why the p1-pointed heap memory didn't release.
Ler
Could anyone help me, please?
...
I'm very confused why the p1-pointed heap memory didn't release.
it probably did, reading from free'd memory is just UB
Ler
Thank you, I know "p0 = nullptr;" is a must after pass p0 to delete. I wrote this to just to understand the memory recovery mechanism. But this result seems to tell me that p1-pointed heap memory didn't change at all. And could please tell me what UB stands for? Cause I'm not a native speaker.
...
Thank you, I know "p0 = nullptr;" is a must after pass p0 to delete. I wrote this to just to understand the memory recovery mechanism. But this result seems to tell me that p1-pointed heap memory didn't change at all. And could please tell me what UB stands for? Cause I'm not a native speaker.
(U)ndefined (B)ehavior. If you think about it, There's no reason for delete/free to write to the address you're passing to it, it would just have a negative impact on performance. however if i remember correctly, in debug builds, Microsoft's CRT writes 0xDD to free'd memory
Ler
Thanks again.This is indeed a good explanation from a performance point of view. Maybe it is a Compiler optimization?
yoo
Where I can learn c++ from beginner level to advance level for free?
Anonymous
Hello everyone! why this piece of code is correct? Why I have not redefinition error here?? switch (a) { case 1: { int data_1; } break; case 2: { int data_1; break; } }
Anonymous
hmm, and what we have in memory with scopes ? How it stores in memory? Why I can create same vars in same function, just use scopes? for example: main(void) { { int f = 0; } { int f = 0; } }
Anonymous
hmm, and what we have in memory with scopes ? How it stores in memory? Why I can create same vars in same function, just use scopes? for example: main(void) { { int f = 0; } { int f = 0; } }
When you create a new scope, it is not like it creates something new in memory as well. This is all the magic of the compiler. In a new local scope, when you create a variable, the compiler just allocates memory for it on the stack. When the scope ends, the compiler just releases this memory. Variables in different scopes at the same level can have the same name. Likewise variables in an inner scope can define a variable with the same name as one in outer scope. This hides the variable in the outer scope. When the inner scope ends, the variable in outer scope becomes visible again.
Huobi BTC Community
Hey guys, any cool suggestions on c++ projects to work on to boost my resume ?and also do I have to learn making GUI projects or just console applications ?
mayway
Whats the logic behind Num=rand()%(max-min+1)+min???
mayway
int num; Size_t count {10}; int min{1}; Int max{6}; Cout<<RAND_MAX<<endl; Stand(time(nullptr)); For(size_t i=1;i<=count;I++){ Num=rand()%(max-min+1)+min; Cout<<num<<endl; } Explain the logic behind this!!
Khaerul
No
Sleeves
Hey guys
Sleeves
Anyone willing to work jointly on a project with me , I’m trying to edit the source code for Bitcoin core. Basically customize it a lil bit
Anonymous
And 'trying' hints that you didn't succeed.. why?
Anonymous
Anyone here can help with flowchart and maybe Otto the Robot I'm confused and stuck and i need it for my exams next week
bunny
O(n)
Anonymous
Lol
ɛ n h ᴀ n c ɛ ґ 🧟‍♂️
#include <stdio.h> #include <ctype.h> #include <stdbool.h> int main() { bool upper, digit, dollar; char password[50]; printf("Password must include number, uppercase letter & a dollar sign"); printf("\n\nPlease Enter Your Password: "); scanf("%s", password); for(int i = 0; i <= 50 ; i ++){ if (isupper(password[i]) ){ upper = true; } if (isdigit(password[i])){ digit = true; } if ((password[i]) == '$'){ dollar = true; } } if(upper && digit && dollar){ printf("\nLoading...."); } else { printf("\nPassword must Include Capital letter, Number and Dollar sign!"); } system ("pause>0"); }
Anonymous
It takes password from user Password must include 1. Uppercase 2. Digit 3. Dollar sign
I think turn it upside down and start from the conclusion that the password is correct
Anonymous
Then u don't need to potentially assign 'upper' 3*50 times.
Anonymous
And make it one 'if'.
ɛ n h ᴀ n c ɛ ґ 🧟‍♂️
ɛ n h ᴀ n c ɛ ґ 🧟‍♂️
And make it one 'if'.
You mean i should check all at once?
Anonymous
Don’t understand this but though
You are assigning 'upper' to 'true' potentially 3*50 times
Anonymous
For no good reason.
Anonymous
So instead, do the opposite. Start with upper = true and check if the password is incorrect.
Anonymous
good. I'd show u an example, but i'm on a phone atm
ɛ n h ᴀ n c ɛ ґ 🧟‍♂️
good. I'd show u an example, but i'm on a phone atm
Okay anytime bro But I’ll try what you’re saying for the mean time Thanks!
Mustapha
#include <stdio.h> #include <ctype.h> #include <stdbool.h> int main() { bool upper, digit, dollar; char password[50]; printf("Password must include number, uppercase letter & a dollar sign"); printf("\n\nPlease Enter Your Password: "); scanf("%s", password); for(int i = 0; i <= 50 ; i ++){ if (isupper(password[i]) ){ upper = true; } if (isdigit(password[i])){ digit = true; } if ((password[i]) == '$'){ dollar = true; } } if(upper && digit && dollar){ printf("\nLoading...."); } else { printf("\nPassword must Include Capital letter, Number and Dollar sign!"); } return 0;