Parra
you don't need to code in c++ style even if you use c++, you can continue doing things like in C, it's not a "best practice" but it's completely doable
Anonymous
iam really honest.For me is multithreading a big thing and this is my first step to that.I want create in the future a Multiplayer game and multi threading in this area realy important and i need this experience
🙋🏻‍♂️
Guys, I'm making a guessing the number game. You should shoose a number between 1 and 10 and PC would guess bu it's not working as expected. Can anyone help?
🙋🏻‍♂️
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ srand(time(NULL)); char guess; printf("Think a number between 1 and 10. There are 3 chances, I'm gonna try to guess. \n\nIf it's true type 'Y', if it's wrong type 'N'."); for(int i=0;i<3;i++){ printf("\n\nThe number is %d, right? ",rand()%10+1); scanf("%c",&guess); if(guess=='N'){ continue; } else if(guess=='Y'){ break; } } printf("\n\nThanks for playing!"); }
Anonymous
Why you use Continue and break use an loop bro
🙋🏻‍♂️
what's not working as expected
Think a number between 1 and 10. There are 3 chances, I'm gonna try to guess. If it's true type 'Y', if it's wrong type 'N'. The number is 2, right? N The number is 3, right? The number is 2, right?
🙋🏻‍♂️
Why you use Continue and break use an loop bro
Won't 'break' break the for loop?
Anonymous
artemetra 🇺🇦
Why you use Continue and break use an loop bro
it's okay to use those, wdym
artemetra 🇺🇦
🙋🏻‍♂️
Anonymous
? no
I mean the loop outside behind the for loop
🙋🏻‍♂️
But yeah, unique each time
artemetra 🇺🇦
Not my guesses, PC's guesses
well yeah, that's what i meant, sorry
🙋🏻‍♂️
artemetra 🇺🇦
But yeah, unique each time
keep track of them, and check each random number if it appears in a list
🙋🏻‍♂️
artemetra 🇺🇦
But yeah, unique each time
https://stackoverflow.com/questions/1608181/unique-random-number-generation-in-an-integer-array
🙋🏻‍♂️
Okay but how to fix scanf's problem?
It reads only once, not second or third Y or N
Anonymous
Is there a reason why priority_queue expects a template argument, as in greater<int> while the sort function needs a temporary object like greater<int>()?
Anonymous
Is there a reason why priority_queue expects a template argument, as in greater<int> while the sort function needs a temporary object like greater<int>()?
Because in the latter case, sort is a function and it requires a concrete argument. In the former case, it is a template argument and the concrete instance will be created inside the class
Aisha
Hello.... I'm a beginner at coding [c language]......i Haven't really learnt much yet can someone plaese help me
olli
Is there a reason why priority_queue expects a template argument, as in greater<int> while the sort function needs a temporary object like greater<int>()?
Because the first one is a type and the second one is an object. If you want to, you can create a priority_queue using an object as well, e.g. #include <queue> int main() { // std::vector<int> is a type using Container_t = std::vector<int>; // std::greater<int> is a type using Comp_t = std::greater<int>; // comp is an object Comp_t comp{}; // container is an object Container_t container{}; // Use the objects to create a priority queue auto Q0 = std::priority_queue(comp, container); // Let the constructor create the objects // automatically based on the passed types std::priority_queue<int, Container_t, Comp_t> Q1; return 0; }
Anonymous
Hey guys 👋🏻 I want {{Hello world}} as output of this code it's from an app can some one fix the code and explain the answer?? Code is: #include <stdio.h> #include <string.h> void myStrcat(char *a, char *b) { int m = strlen(a); int n = strlen(b); int i; for (i = 0; i < n; i++) a[m + i] = b[i]; } int main() { char *str1 = "Hello "; char *str2 = "world"; myStrcat(str1, str2); printf("%s", str1); return 0; } Thanks 🙏🏻
Mahabub Alam
Hey, can anyone suggest me any book for c++? so that i can learn deeply about c++
Anonymous
Hey guys 👋🏻 I want {{Hello world}} as output of this code it's from an app can some one fix the code and explain the answer?? Code is: #include <stdio.h> #include <string.h> void myStrcat(char *a, char *b) { int m = strlen(a); int n = strlen(b); int i; for (i = 0; i < n; i++) a[m + i] = b[i]; } int main() { char *str1 = "Hello "; char *str2 = "world"; myStrcat(str1, str2); printf("%s", str1); return 0; } Thanks 🙏🏻
First, pointers to strings in C are constant by default. Change them to char str1[] and char str2[] Now, your code might give the correct output, but it's undefined behavior because str1 won't have enough space to accomodate str2 (try making str2 big) For that, you need to declare str1 like : char str1[12] Just enough space to accommodate str2, but it can be more than 12
Anonymous
Is there a reason why priority_queue expects a template argument, as in greater<int> while the sort function needs a temporary object like greater<int>()?
@SilhouetteInDark @ollirz regarding this, the difference in the kind of comparator they accept is by choice, right? It didn't need to be one or the other.
olli
@SilhouetteInDark @ollirz regarding this, the difference in the kind of comparator they accept is by choice, right? It didn't need to be one or the other.
not sure I understood the follow-up question. But it's pretty much the same as this: You can write std::vector<int> V; and V.push_back(int{}); because you have a vector of types int and push back an object (int{}). Writing V.push_back(int); however does not work, because you can't 'add' a type, the same goes for std::vector<int{}> V;.
olli
@SilhouetteInDark @ollirz regarding this, the difference in the kind of comparator they accept is by choice, right? It didn't need to be one or the other.
but also yes, you could implement sort like this template< class RandomIt, class Compare > void mySort( RandomIt first, RandomIt last); and call it like this std::vector<int> V; mySort< std::vector<int>::iterator, std::greater<int> >(V.begin(), V.end()); but honestly that's pretty painful to write. std::sort forces you to pass the comp parameter so it's able to deduct the template type for you and prevents you from typing all the types. e.g. this is valid too, but it's hard to write and hard to read std::sort< std::vector<int>::iterator, std::greater<int> >(V.begin(), V.end(), std::greater<int>{});
Anonymous
@SilhouetteInDark @ollirz regarding this, the difference in the kind of comparator they accept is by choice, right? It didn't need to be one or the other.
It is not by choice. Template functions that don't accept default arguments require you to always pass a concrete instance. Even in the example that @ollirz gave where you pass in specific concrete instances to priority_queue, you passed in these concrete instances to the constructor. It is not an either or choice. If you use templates in a scenario where passing in a template type is good enough then yes you can do it just like how you can do it with a priority_queue. In cases where you can't do so like the call to std::sort function, you will have to pass in a concrete instance.
shriman_deepak
Which language and semester ?
Hamed
Hello I want to convert a bitmap file in to a 2d array / matrix without using any third library In the other word, I want to do it with pure c++ Would you mind helping me?
Prabhat
How do you overcome burnout
Prabhat
sorry?
Since 2 weeks I'm coding more than 7h/day.. now I'm feeling frustrated :)
klimi
i see... welcome to the programming world
Hamed
in what format is the bitmap?
Yes the format is bitmap
klimi
Yes the format is bitmap
so.... is it PPN or bmp? or png? jpg?
klimi
okay, so you will need to build a parser for that
klimi
https://en.wikipedia.org/wiki/BMP_file_format
klimi
like you need to process the header and the metadata
klimi
then you can read the pixel data into your array
Hamed
I got it, thanks
Victor
Please which compiler is best for coding on vscode
Pritam
For c++
gcc/g++ compiler
sasha
I'm talking about the picture you posted above, it is a warning that you're not using the blocked_range.h ... so... the main error is gone...
If you want to know, I fixed this by updating MinGW to higher version. It turns out, that <execution> is not supported by versions below 9.1
Shahrukh
#include<iostream> #include<bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n,k;// 9 6 int a[n];// 9 for(int k =0; k<n; k++) { cin>>a[k];// 1 2 3 10 12 20 } int i,index=2,x=15; for(i = k-1;i>=index;i--) { a[k+1]=a[k]; } a[index]=x; for(int j=0;j<n;j++) { cout<<a[j]<<" "; } return 0; } // - - - - -
Shahrukh
What's wrong in this code I'm trying to add element at index 2
Anonymous
#include <queue> using namespace std; template <typename T> struct comp { bool operator()(T a, T b) { return a > b; } }; int main() { priority_queue<int, vector<int>, comp<int>> p; return 0; } How can I declare comp so that I can plug it into priority_queue's class Compare without the template argument? Like, priority_queue<int, vector<int>, greater<>> does not need greater<int> This probably falls under template specialization and I'm having trouble understanding it.
Pulkit
My program is running yet not showing any output or terminal activity Here is the code :- #include<stdio.h> #include<math.h> int main() { float p, r, t, a, si, ci; printf("Enter Principle="); scanf("%f",&p); printf("Enter Rate="); scanf("%f",&r); printf("Enter Time="); scanf("%f",&t); si=(p*r*t)/100; printf("Simple Interest=%.2f \n",si); a = p*(pow((1 + r / 100), t)); ci = a - p; printf("Compound Interest=%.2f",ci); return 0; }
Pulkit
its a very basic interest calculator
Pulkit
It's working fine.
idk no c program is showing output
Pulkit
but they are working fine with cmd
Pulkit
this is a recurring problem with my vs code
Pulkit
can anyone suggest a remedy
Anonymous
Seems like a problem with vscode
Mazen
can anyone suggest a remedy
https://youtu.be/6QVjIZTDG8c
M. Wahyu
Best IDE for software embedded system ?
Palinuro
M. Wahyu
what about qtcreator?
Nice. noted : )
Alessandro
The one shipped with your microcontroller
Davi
Best IDE for software embedded system ?
Eclipse is enough for C/C++. You can customize the toolchain used, and if your micro uses the GNU one, it is basically the same as programming and debugging a desktop app. Many IDEs shipped by the manufacturers are worsened editions of Eclipse IDE, so I recommend learning the original one. You can get away with vim+plugins, but it takes time to learn how to use it.
NagaRaju
I'm trying to convert 12 hour time to 24 hour in C This is my code : #include <stdio.h> char* milltaryTime (char* s) { if (s[8] == 'A') { if (s[0] == '1' && s[1] == '2') { s[0] = '0'; s[1] = '0'; } } else { if (s[1] == '8') { s[0] = '2'; s[1] = '0'; } else if (s[1] == '9') { s[0] = '2'; s[1] = '1'; } else if (!(s[0] == '1' && s[1] == '2')) { s[0] += 1; s[1] += 2; } } s[8] = '\0'; return s; } int main () { printf (" %s\n", milltaryTime ("01:20:35PM")); printf (" %s\n", milltaryTime ("02:20:35PM")); printf (" %s\n", milltaryTime ("03:20:35PM")); printf (" %s\n", milltaryTime ("04:20:35PM")); return 0; } I'm using clang complier in linux During compilation I'm getting "segmentation fault''. Any help will be appreciated.
MRT
we have any algorithm like md5 get unique letters for object,But give a int(not list int) instead of a string?
Jos
Hiii