Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
If I have a probability between 0 and 1 where 1 executes every time and 0 never executes, how would I implement that into my code?
For instance, I have a probability of 0.8, how would I program a section of code to run 80% of the time.
I assume we used the rand() function but I don’t know how to implement that.
I think this would be an example of how to implement what you're trying to do:
int main() {
double probability = 0.8;
srand(time(0));
double randomNumber = (double) rand() / RAND_MAX;
if (randomNumber < probability) {
// execute code
}
return 0;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
151A codeforces link
Here is my code written in C language. It has some problem
#include<stdio.h>
#include<conio.h>
int main ()
{
int n,k,l,c,d,p,nl,np,mm,per,tl,to,sum;
scanf("%d%d%d%d%d%d%d%d",&n,&k,&l,&c,&d,&p,&nl,&np);
mm = k*l;
per = mm/nl;
tl = c*d;
to = p/np;
sum=(per,tl,to)/n;
printf("%d",sum);
return 0;
}
You have to find the minimum value among per, tl, and to and divide that with n instead of just calculating to/n, which you are currently calculating with sum=(per,tl,to)/n;. (per and tl are ignored according to the definition of the comma operator)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is program for max heapify ,i have doubt int this algorithm,that if a already max heap is passed into this function MAX_HEAPIFY in that case largest will equal to i only and directly recursive call to MAX_HEAPIFY
will execute only in that recursion will go infinitely
i am assuming that array index is starting from 1 instead of zero
MAX_HEAPIFY(A,i,heapsize){
l=2i;
r=2i+1;
if(l<=heapsize&& A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heapsize&&A[r]>A[largest])
largest=r;
if(largest!=i)
swap(A[i],A[largest])
MAX_HEAPIFY(A,largest)
}
There may be some problem with indentation when copying this algorithm. But if you put MAX_HEAPIFY(A,largest) under this if(largest!=i) condition, then algorithm is correct. See the Algorithm below for more clarification:
MAX_HEAPIFY(A,i,heapsize){
l=2i;
r=2i+1;
if(l<=heapsize&& A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heapsize&&A[r]>A[largest])
largest=r;
if(largest!=i) {
swap(A[i],A[largest])
MAX_HEAPIFY(A,largest)
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
scanf("%lf",&alpha);
alpha = (alpha * PI)/180;
if(alpha==PI/2)
{
printf("0");
}
i also defined PI and declared alpha...it just skipping this if and i don't know why
Equality and floating point numbers do not go down well. You have rounding errors.
Need to put in some tolerance.
#include <stdio.h>
double PI = 3.14159265359;
int main (void)
{
double alpha = 90.0;
scanf("%lf",&alpha);
alpha = (alpha * PI)/180;
if(alpha==PI/2.0)
{
printf("0");
}
}
Enter 90 and the 0 gets printed.
Works as expected, did you declare alpha as float?
Then you would have to change the scanf to "%f" to get correct results.