Alex
Use loop for
Alex
It's aimple
Pavel
Can anyone tell me how does range based for loop works with array of pointers Suppose i have an array of pointers which has pointers pointing to integers. for(auto a:array) *a = 0; All the elements will become zero. But by default range based for loop(without reference) passes copies right. Then how did it affect the real ones?
The pointer is just an address to memory, if you want to modify the object in this memory it doesn't really matter how you got the address. When you copy a pointer, only the address will be copied. It analoguous to this: int a = 8; // original value int* b = &a; // pointer you store in array int* c = b; // pointer you copy in range based loop *c = 0; // your operation
Pavel
Can anyone tell me how does range based for loop works with array of pointers Suppose i have an array of pointers which has pointers pointing to integers. for(auto a:array) *a = 0; All the elements will become zero. But by default range based for loop(without reference) passes copies right. Then how did it affect the real ones?
And by the way, "copy by default" is result of using auto here, because auto doesn't deduce references. Range based loops provide references, but auto drops it. You can use auto& here, or specify the type.
Anonymous
how do i detect if a value is less than X, for example, given an array of {2, 4, 6, 8} how do i know if my value went from: 10 to 8 (max) or 7, 6 (min) to 5, 2 (min) to 1, assuming ranges of min 2 max 4, min 4 max 6, min 6 max 8
Anonymous
cant
Anonymous
the array is to be interpreted as a sequence of min/max
Talula
cant
https://www.geeksforgeeks.org/cpp-program-for-quicksort/
Anonymous
eg {1 2 3} itteration 0 min 1 max 2 itteration 1 min 2 max 3
Talula
the array is to be interpreted as a sequence of min/max
Sort it and the number above is min and number below is max...
Anonymous
basically it is meant to be interpereted as this
Talula
Yes, 1 is above 2 and 2 is above 3... so just display it like that.
Anonymous
for (size_t i = size-1; i > 0; i--) { float min_ = values[i - 1]; float max_ = values[i];
Anonymous
i can detect if the value goes up (eg 2 to 3, 4 to 7) by just doing if (val > min_ && val < max_ && cached_val_max != max_) but i doing the same for down doesnt work
Abhishek
cbook
Anonymous
Is any c related couse for beginners available in this channel?
klimi
#cs50 is kinda nice, but is in to only about C
Sam
I started learning c++ through youtube and some materials but i am not improving, is there any other way.
Talula
I started learning c++ through youtube and some materials but i am not improving, is there any other way.
Imagine a project and work on it... For example make a project to detect image or something really complex, using Google, Stockexchange and stuff to learn how you should do something... on YouTube you'll get your basics corrected...
ꍏꈤꀸ
Hi, I have GUI, which is sending from multiple threads some messages to message queue. Then engine reads that messages and sends some replies, or data, errors, statuses in other message queue. Messages, sent from GUI have unique ID, so when Engine send reply back, it uses this ID. How can i handle Engine replies? So, i need to identify to which GUI request, Engine sends reply. Engine can send not only replies, but also as i mentioned, some statuses, errors... I need to be able to handle this in GUI too. It mostly about DESIGN. What would you recommend to achieve what i need?
Sergeant Stedenko
I started learning c++ through youtube and some materials but i am not improving, is there any other way.
Success is 98% perspiration...you gotta work hard. When I was learning, I just looked at each command, read up on it, then wrote loads of examples using the new command until I understood. It's a good way. 👍🏻
ꍏꈤꀸ
Are the GUI threads doing similar work i.e are they just sharing the workload to make it faster or are they meant to do different tasks that are independent of each other?
They are intended to to different tasks. It's like a GUI threads. One thread can operate over user input, while other is drawing something (just example) But for the future would be great to include solutions for both this cases,
Anonymous
They are intended to to different tasks. It's like a GUI threads. One thread can operate over user input, while other is drawing something (just example) But for the future would be great to include solutions for both this cases,
In that case have a queue for the engine where these GUI threads will be posting their messages. The engine can process them sequentially or even have multiple threads of its own reading off this queue. Each GUI thread (since it is independent) can have a separate queue of its own where the Engine can post it's response. Have a register mechanism on the Engine where these GUI threads can register their queues. The Engine can return a unique ID to these GUI threads which can be used to identify the queues. When a GUI thread posts a message into the engine, it should include this unique Id in the message. This is the simplest way to achieve what you intend to do.
Anonymous
Ok then have another thread in the GUI library that reads the engine responses from the queue meant for GUI and have it pass the message to the correct GUI thread using a callback mechanism. In this case the GUI threads don't have to register with the engine.
Anonymous
Yeah, i have already sender and receiver threads in GUI, Can you, please, describe with more details your vision about callback mechanism in current case?
Callback mechanism is a popular concept in multithreading and elsewhere too. There are many resources for it online. Would be more easy to learn about it that way rather than posting all the details here
ꍏꈤꀸ
Callback mechanism is a popular concept in multithreading and elsewhere too. There are many resources for it online. Would be more easy to learn about it that way rather than posting all the details here
I understand what you are talking about. I'm wondering about proper way to integrate them for my issue. Example: GUI sends from thread1, request1, then start waiting for reply1. In the same time GUI sends from thread2, request2, but it don't wait to reply (but as soon as it arrives from Engine, GUI should for example notify user) Then Engine, pops, let's say, request1, but before sending reply1, it sends others replies(like result1, result2, result3...) after which it finally sends reply1. Same can happens with thread2. So, i'm trying to say, that i don't need single request-response. I need also to be able to operate over others replies from Engine.
Anonymous
I understand what you are talking about. I'm wondering about proper way to integrate them for my issue. Example: GUI sends from thread1, request1, then start waiting for reply1. In the same time GUI sends from thread2, request2, but it don't wait to reply (but as soon as it arrives from Engine, GUI should for example notify user) Then Engine, pops, let's say, request1, but before sending reply1, it sends others replies(like result1, result2, result3...) after which it finally sends reply1. Same can happens with thread2. So, i'm trying to say, that i don't need single request-response. I need also to be able to operate over others replies from Engine.
Forget the engine. Assume the engine sends responses in random order and that it doesn't necessarily send responses in the same order as requests which is likely what will happen in real world. The GUI Task threads can either fire a request to the engine and wait for it's response or they may send an async request and process the response when it arrives. In the latter case you need a store and forward mechanism (the GUI thread handling GUI queue should store the responses from the engine and forward them to the GUI thread meant to handle it) for the responses to be processed. A better way instead would be if you could use a promise/future combination with the future holding all the information that the GUI thread needs (i.e. the response details from the engine) and the GUI thread will maintain the future objects for all the requests that it sent out. The GUI thread responsible for the GUI queue in which the engine responses will be stored will pick these responses (in the case of promise/future combo this will just a request response id which is just a notification from the engine that the request has been processed. The response details will be in the future object associated with the promise) and then use the callback of that GUI thread. This callback will typically set an interruptible state informing the GUI thread that there are responses from engine that need to be processed. The GUI thread after finishing what it has done (or if the interrupt is of higher priority stop doing what it is currently doing) andcould check the response id and use the corresponding future object to retrieve the responses for the engine, process it and then go back to doing what it was doing earlier. If there are many responses, then it could process all of them based on their priorities.
Anonymous
👍
ꍏꈤꀸ
Forget the engine. Assume the engine sends responses in random order and that it doesn't necessarily send responses in the same order as requests which is likely what will happen in real world. The GUI Task threads can either fire a request to the engine and wait for it's response or they may send an async request and process the response when it arrives. In the latter case you need a store and forward mechanism (the GUI thread handling GUI queue should store the responses from the engine and forward them to the GUI thread meant to handle it) for the responses to be processed. A better way instead would be if you could use a promise/future combination with the future holding all the information that the GUI thread needs (i.e. the response details from the engine) and the GUI thread will maintain the future objects for all the requests that it sent out. The GUI thread responsible for the GUI queue in which the engine responses will be stored will pick these responses (in the case of promise/future combo this will just a request response id which is just a notification from the engine that the request has been processed. The response details will be in the future object associated with the promise) and then use the callback of that GUI thread. This callback will typically set an interruptible state informing the GUI thread that there are responses from engine that need to be processed. The GUI thread after finishing what it has done (or if the interrupt is of higher priority stop doing what it is currently doing) andcould check the response id and use the corresponding future object to retrieve the responses for the engine, process it and then go back to doing what it was doing earlier. If there are many responses, then it could process all of them based on their priorities.
Sounds good. Thank you.
Sachin
Q generate all binary strings of n bits length
Sachin
#include <bits/stdc++.h> using namespace std; void func(string ans,int n) { if(ans.size()==n) { cout<<ans<<" = "<<endl; return ; } ans+="0"; func(ans,n); ans.pop_back(); ans=+"1"; func(ans,n); ans.pop_back(); } int main() { int n; cin>>n; string ans=""; func("",n); }
Dimitris
If someone know anything about Qt please send me a message. I have many questions!
Mazen
If someone know anything about Qt please send me a message. I have many questions!
https://youtube.com/user/VoidRealms This guy knows everything about Qt
Dimitris
https://youtube.com/user/VoidRealms This guy knows everything about Qt
Thank you but I have a question about why my app doesn't running!
Shuhash
Will anyone tell me logic to print prime numbers in the array followed by the non prime number
Pavel
You mean sum of sizes of all the vectors?
Pavel
You can do std::accumulate with a custom lambda https://ideone.com/OLjUo7
Anonymous
#include<stdio.h> #include<stdint.h> #include "bitmacro.h" // Local header file /* WAP to determine a given signed number negative or positive without using < or >*/ // Hint For a Signed negative number, MSB = 1 int32_t num; bool_t msb; int main(void) { printf("\nEnter a Number:"); scanf("%d",&num); msb = Get_Bit(num, 31); if(1 == msb) { printf("\nGiven Number is Negative"); } else { printf("\nGiven number is positive"); } return 0; }
Anonymous
May I please know the reason if I enter the input as alphabets then why it will give the result as positive ? (Everything else works fine.) Even though I put negative sign (-) prefix to alphabet then it will execute result like "Given number is positive".
Anonymous
Yeah..if I enter the positive or negative number then I am getting the correct corresponding results.
Pavel
Yeah..if I enter the positive or negative number then I am getting the correct corresponding results.
Ah, I've misread it, what do you mean by "alphabet"? You mean non-number, like "a"? In this case it's an error case and scanf should return some special error value (and you should not rely on the value of num). I don't remember what value is returned from scanf in this case..
Anonymous
Yeah
Pavel
Yeah...Got it, but why it taking alphabet as positive ? Why don't it print as negative ?
minus doesn't make sense with not numbers, I guess it just gives some garbage value. In my case it gives zero always, doesn't matter what symbols I enter.
Anonymous
#include <stdio.h> /* finding prime factors of a number n*/ int main(void) { int n, i, j, count = 0; printf("enter n\n"); scanf("%d", &n); printf("prime factors of %d are\n", n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { /* prime has only two factors, count = 2 */ if (i % j == 0) count++; } /* if prime and also a factor of n */ if (count == 2 && n % i == 0) { printf("%d\n", i); } } return (0); } Pls can someone help with this code? It is not giving right output although it compiles
Anonymous
Prime Factorization is NP Hard so there is no known polynomial time algorithm. The easiest one in your case would be Pollard's rho algorithm (you can search for it online). If you want to make your algorithm more efficient, you can skip even numbers (by incrementing your inner loop counter by 2) and also by iterating your inner loop till j*j < i insead of all the way till i
Anonymous
If I skip even numbers, 2 will be affected right? But 2 is a prime number
You can skip all even numbers except 2...check for divisibility by 2 outside the inner for loop and start iterating from 3 and skip even numbers
Anonymous
You can make it even more efficient by removing the isPrime check and while it's in the NP-class, integer factorization is not known or believed to be NP hard.
He wants prime factors. So how can he skip the isPrime check? And integer factorization is believed to be NP Complete even though there is no known proof. I should have said NP Complete
olli
He wants prime factors. So how can he skip the isPrime check? And integer factorization is believed to be NP Complete even though there is no known proof. I should have said NP Complete
by something like this, once you know the number is not divisible by any number smaller than c but is divisible by c, c must be a prime, e.g. void printPrimeFactors(int n) { for (int c = 2; n != 1; c += 1) { while ((n % c) == 0) { printf("%d ", c); n /= c; } } }
Anonymous
This is too expensive and I don't understand
For ex if you are finding the prime factors of 60 You will do this: 60%2 = 0 So 2 is a prime factor. Then 60/2 = 30 30%2 = 0 So 2 is another prime factor. Then 30/2 = 15 15%2 != 0. So 2 is no longer a prime factor. You move on to 2+1 = 3 15%3 = 0. 3 is a prime factor. 15/3 = 5 5%3 != 0. So 3 i not a prime factor more than once. You move on to 3+1 = 4 (You can skip even numbers here as well) 5%4 != 0. So move on to 4+1 = 5 5%5 = 0. S0 5 is a prime factor. 5/5 = 1. So the loop ends with 2,2,3,5 as the prime factors. This is the method we used in school to find prime factors.
Anonymous
And after it outputs the prime factors, how do you find the highest factor?
Anonymous
And after it outputs the prime factors, how do you find the highest factor?
The number which sets n to 1 will be the highest factor. In the example that I gave above 5 will be the highest factor
Anonymous
Anonymous
But if I want to write more efficient codes, what should I do?
Anonymous
Is there a trick?
Anonymous
Implement probabilistic algorithms...there is no trick here. Prime Factorization (a simplified version of integer factorization) is an area of research. If you can implement an efficient algorithm then cryptography as we know it today will be at risk.
Anonymous
I have to go to YouTube and learn more about probabilistic algorithms
Tomay
Hello