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;
}
dearfl
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;
}
what if I want the very first match element?
Peace
dearfl
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;
}
int mid = left + (right - left) / 2;
Anonymous
Nils
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
dearfl
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;
}
anyway, i suggest https://en.cppreference.com/w/cpp/algorithm/lower_bound
Anonymous
Anonymous
Is the lib statically linked?
Nils
Anonymous
Recompile it for release then
Nils
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
Nils
Nils
Nils
Nevermind! I got an "internal compiler error please report to https://xxxxxx"
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?
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?
...
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.
...
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?
yoo
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;
}
}
Ler
Anonymous
Anonymous
Ler
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;
}
}
Ler
Anonymous
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???
klimi
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
Mark
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
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");
}
ɛ n h ᴀ n c ɛ ґ 🧟♂️
ɛ n h ᴀ n c ɛ ґ 🧟♂️
Anonymous
Then u don't need to potentially assign 'upper' 3*50 times.
Anonymous
And make it one 'if'.
ɛ n h ᴀ n c ɛ ґ 🧟♂️
Anonymous
For no good reason.
Anonymous
So instead, do the opposite.
Start with upper = true and check if the password is incorrect.
ɛ n h ᴀ n c ɛ ґ 🧟♂️
Anonymous
good.
I'd show u an example, but i'm on a phone atm
Anonymous
Mustapha
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;