Anonymous
Basically suppose you have a sorted array, say for example, 16 21 32 43 75 96. Now what you want to do is that check if 75 is in the array or not. Of course one thing you can do is that you traverse through the array from beginning to end and stop only if you find 75, and if you travel the full array without finding 75, then it is not present. This approach is called linear search. But a better approach would be to narrow down your search space as you go. First of all, you go to the middle element (which is 32 or 43 in this case, depending on your implementation) and see if it is less than or equal to 75. Since it is less, now you discard the first half and only look at the second half of the array. In the second step you have 43 75 96 to work with, and you go to the middle and you find 75 here, so you stop. This is the binary search approach and as you can see, it takes only 2 steps in comparison to the 5 steps of checking in linear search. @bitkrypton0
Anonymous
I hope that helps
Anonymous
int position = -1; for (int i = 0; i < 25; i++) { if(prime[i] == x) { position = i; break; } }
Anonymous
This is a simple linear search algorithm where x is the integer you want to search
Anonymous
If position is -1 at the end of the search that means x is not in the array
Anonymous
A binary search algorithm would be as follows
AK
Thanks a lot Sarthak
Anonymous
int search(int prime[], int a, int b, int x) { if(b>=a) { int mid = a+(b-a)/2; if(prime[mid]==x) return mid; if(prime[mid]>x) return search(prime,a,mid-1,x); return search(prime,mid+1,b,x); } return -1; }
Anonymous
you would call the function as follows: search(prime, prime[0], prime[24], 75) for suppose x = 75
Anonymous
but this approach works only if the array is sorted
AK
Ok
Anonymous
The code is clear, I hope?
Anonymous
Or is there a part you do not follow?
Anonymous
position is an integer that indicates the index of the array in which the value you're looking for is present. It is -1 if the value is not present in the array.
AK
position is an integer that indicates the index of the array in which the value you're looking for is present. It is -1 if the value is not present in the array.
#include<iostream> using namespace std; int main () { int primenumber[25]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; int guess; int totaltries=0; int guesslimit=2; bool remainingguess = true; int position=0; int i = -1; for (i=0; i<25; i++) { if (primenumber[i]==guess) { position = i; break; } } while (guess != primenumber[position] && remainingguess) { if (totaltries<=guesslimit) { cout<<"Guess the number between 1-100:"<<endl; cin>>guess; totaltries++; } else { remainingguess = false; } } if (!remainingguess) { cout<<"You lose :("; } else { cout<<"You Win :)"<<endl<<"Tries = "<<totaltries; } }
AK
Why i = -1?
Ooh it was position... that's my mistake sorry
Anonymous
Yes, other than that it seems to be fine
Anonymous
C or C++?
AK
Yes, other than that it seems to be fine
Now it gives error "i is not declared in this scope"
Anonymous
Yes obviously you have to declare it
Anonymous
Replace int position = 0 with int position = 1 and replace int i = -1 with int i
Anonymous
C
I'm afraid I can't help you much with C
AK
Replace int position = 0 with int position = 1 and replace int i = -1 with int i
#include<iostream> using namespace std; int main () { int primenumber[25]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; int guess; int totaltries=0; int guesslimit=2; bool remainingguess = true; int i; int position = -1; for (i=0; i<25; i++) { if (primenumber[i]==guess) { position = i; break; } while (guess != primenumber[position] && remainingguess) { if (totaltries<=guesslimit) { cout<<"Guess the number between 1-100:"<<endl; cin>>guess; totaltries++; } else { remainingguess = false; } } if (!remainingguess) { cout<<"You lose :("; } else { cout<<"You Win :)"<<endl<<"Tries = "<<totaltries; } }}
Anonymous
Seems so, yes
AK
Seems so, yes
But when i run it prints "you win" 25 times :(
.
في حد شاطر ب فيزيا ١؟
.
Hello, can you help me with Physia 101؟
Anonymous
Of course @bitkrypton0 you have to search after the input is taken, right
Anonymous
Well you are inputting guess
Anonymous
You need to have the input first, and then check
Anonymous
But when i run it prints "you win" 25 times :(
It prints 25 times "you win" it never satisfies tour while statatement : guess != primenumber[position] && remainingguess , because this while loop never executes you are not assigning guess to anything thus your if (primenumber[i]==guess) { position = i; break; }
Anonymous
this if : is never satisfied primenumber[i] cant be equal to un unset guess thus position is allways equal to -1 which is not a valid index for the array ,
AK
this if : is never satisfied primenumber[i] cant be equal to un unset guess thus position is allways equal to -1 which is not a valid index for the array ,
Bro let me know if we can do it without for loop as my teacher haven't taught for loop so i can't use it
Anonymous
https://nekobin.com/fepuhecemi
Zeshan
How to allocate dynamic memory in pointers and vice versa?
Igor🇺🇦
How to allocate dynamic memory in pointers and vice versa?
Did you try searching before asking https://www.ecosia.org/search?q=How+to+allocate+dynamic+memory
Anonymous
I should have done that differently... const int last = len-1; int x = a[(idx < last) ? idx++ : last];
telegram
Can anyone just tell what is conditional preprocessors in C
telegram
Tomorrow I have a exam please help me out
Nomid Íkorni-Sciurus
What features does D offer that C++ does not and how is it more modern?
- Pattern matching - Actual pure functions (C++ consts are NOT pure as in they allow other states to be modified) - A working package manager - Actual types (C++ types lie to the programmer. Nobody should ever be allowed to do something as creepy as dynamic_cast<> or void*) - Attributes (although C++ does support metaprogramming, it is not context-aware) - Contracts, and thus Dependent Types (C++ does support constexpr and other related cool stuff, but lacks of dependent types)
Nomid Íkorni-Sciurus
Ah, right. D also supports inline unit testing (much like jUnit in Java) and Traits And also, D is garbage collected (C++ technically does have something very similar, but you need to manually guide it and usually manually specifying destructors and move/copy constructors allow for better performance)
Nomid Íkorni-Sciurus
Ah, and also has by-design support for utf8, utf16, utf32 strings
Nomid Íkorni-Sciurus
https://twitter.com/mcypark/status/1352022455846506497
Hm... it's pattern matching at least
Nomid Íkorni-Sciurus
experimental, but still.
Nomid Íkorni-Sciurus
what about the other points?
olli
what about the other points?
Those are points I do not care too much about. For native development I will refrain from using a GC collected language. I remember the Discord blog post where they ditched Go in favor of Rust and measured latencies in us instead of ms. Deterministic object destruction is something a lot of languages lack, but it is really useful. in D it is not even guaranteed that GC calls the destructor for all objects. C++ supports Attributes as well, has way more powerful templates, compile time programming with constexpr and a strongly typed system. What's your issue with dynamic_cast ? I makes sure the cast succeeds. void* is useful when you deal with hardware and I don't think a package manager and testing framework should be part of the language. I'd like to see updates to my package manager/testing framework without waiting for a new language revision to be released.
Anonymous
Tomorrow I have a exam please help me out
#ifdef and similar https://en.cppreference.com/w/cpp/preprocessor/conditional Typically used for sections of platform specific code or debug/tracing code. The #ifdef, #else (if any) and #endif need to be on own line, the conditional code is lines in between (including #define lines if any).
Nomid Íkorni-Sciurus
Those are points I do not care too much about. For native development I will refrain from using a GC collected language. I remember the Discord blog post where they ditched Go in favor of Rust and measured latencies in us instead of ms. Deterministic object destruction is something a lot of languages lack, but it is really useful. in D it is not even guaranteed that GC calls the destructor for all objects. C++ supports Attributes as well, has way more powerful templates, compile time programming with constexpr and a strongly typed system. What's your issue with dynamic_cast ? I makes sure the cast succeeds. void* is useful when you deal with hardware and I don't think a package manager and testing framework should be part of the language. I'd like to see updates to my package manager/testing framework without waiting for a new language revision to be released.
Those points make cleaner (although a bit less readable in some cases) code, and is also a less error-prone approach. Hm... I agree about D's GC. C++ attributes are not context-aware "Besides the standard attributes listed below, implementations may support arbitrary non-standard attributes with implementation-defined behavior. All attributes unknown to an implementation are ignored without causing an error." - https://en.cppreference.com/w/cpp/language/attributes Also, D's attributes are numerous and very powerful (see @system, @nogc, @trusted and @inout) I agree for constexpr, that's why I specified it in the previous message. I don't agree about the "strongly typed system". The moment you dynamic_cast<>, you're basically... ahhh wait. I meant reinterpret_cast<>. It can turn a Table into a Penguin which really is not very safe. Yeah I agree you make dynamic_cast<> succeed (otherwise C++ OOP's system would be flawed). void* is useful sure, but I meant casting by and with it. In my opinion nobody should ever (void*) something. The package manager is not part of the language; actually, dmc's pm is being developed separately and handles references / linkage well. About testing I was just adding other stuff and actually I agree with you.
IAmMADMAX
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { int *piData; char *pcName; } Info; int main() { Info MyInfo; MyInfo.piData = malloc(sizeof(int)); if((MyInfo.piData) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } *MyInfo.piData; scanf("%d",&*MyInfo.piData); printf("MyInfo.piData = %d\n",*MyInfo.piData); MyInfo.pcName = malloc(sizeof(char) * (*MyInfo.piData )); if((MyInfo.pcName) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } strncpy(MyInfo.pcName,"programmer",(*MyInfo.piData)); printf("MyInfo.pcName = %s\n",MyInfo.pcName); return 0; }
Anonymous
Why everytime i get a different output when i input any even number?
It seems to work just fine... Have you tried lengths [0, 12]? There is a memory leak but the program exits before an issue.
Yusuf
whats up folks
klimi
whats up folks
Programming
Programmer
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { int *piData; char *pcName; } Info; int main() { Info MyInfo; MyInfo.piData = malloc(sizeof(int)); if((MyInfo.piData) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } *MyInfo.piData; scanf("%d",&*MyInfo.piData); printf("MyInfo.piData = %d\n",*MyInfo.piData); MyInfo.pcName = malloc(sizeof(char) * (*MyInfo.piData )); if((MyInfo.pcName) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } strncpy(MyInfo.pcName,"programmer",(*MyInfo.piData)); printf("MyInfo.pcName = %s\n",MyInfo.pcName); return 0; }
I changed it to work in VS: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { int* piData; char* pcName; } Info; int main() { Info MyInfo; MyInfo.piData = (int*)malloc(sizeof(int)); if ((MyInfo.piData) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } *MyInfo.piData; scanf("%d", MyInfo.piData); printf("MyInfo.piData = %d\n", *MyInfo.piData); MyInfo.pcName = (char*)malloc(sizeof(char) * (*MyInfo.piData + 1)); if ((MyInfo.pcName) == NULL) { printf("FAILED TO ALLOCATE MEMORY\n"); return 0; } strncpy(MyInfo.pcName, "programmer", (*MyInfo.piData)); MyInfo.pcName[*MyInfo.piData] = 0; printf("MyInfo.pcName = %s\n", MyInfo.pcName); system("Pause"); }
Anonymous
You can call free(NULL); without harm, it is an idiom to goto fail; and fail: free(ptr1); free(ptr2); free(ptr3); return NULL; and put that after the normal return statement.
Anonymous
The goto label has a colon ":" instead of semi-colon ";" after it. Some people have put it at the start of a line without indenting. Your compiler may or may not complain if there isn't at least one statement after a goto target label.
Ausir
Hi guys, I'm using shmget(), I quote the man page "IPC_CREAT Create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permissions[..]" I NEED to use shmget without IPC_CREAT but compiler return few arguments. What should I do?
Anonymous
http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/process.html
AmR
Any one know gui code counter for Linux like cccc ?
Anil
Which software is best than VS Code...?
Anil
Please tell me🙏🙏