Parra
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
Parra
🙋🏻♂️
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!");
}
artemetra 🇺🇦
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?
Anonymous
artemetra 🇺🇦
artemetra 🇺🇦
artemetra 🇺🇦
🙋🏻♂️
Anonymous
? no
I mean the loop outside behind the for loop
🙋🏻♂️
But yeah, unique each time
🙋🏻♂️
🙋🏻♂️
artemetra 🇺🇦
But yeah, unique each time
https://stackoverflow.com/questions/1608181/unique-random-number-generation-in-an-integer-array
🙋🏻♂️
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>()?
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
Anonymous
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
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?
klimi
Prabhat
How do you overcome burnout
klimi
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
Hamed
klimi
okay, so you will need to build a parser for that
klimi
https://en.wikipedia.org/wiki/BMP_file_format
Hamed
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
Victor
Pritam
sasha
Talula
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.
Hima
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
Hima
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;
}
It's working fine.
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
M. Wahyu
Best IDE for software embedded system ?
Palinuro
M. Wahyu
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
olli
Anonymous