How to fix this code?

Joined
Sep 22, 2023
Messages
1
Reaction score
0
Hi, I'm trying to calculate the half-life of some hipotetical element using this program:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

#define    lambda    0.079        // Decay constant (min^-1)
#define    N0        1000        // Unstable nuclei t = 0
#define dt        1e-6        // Time interval

main()
{   
    srand(time(NULL));

    double N=N0;     // Number of nuclei
    double t=0;        // Time counter
    int i;

    while(N>N0/2){
        for(i=N0;i>0;i--){
            if(rand()/RAND_MAX>lambda*dt) N--;
        }
        t=t+dt;
        printf("%.2f\n",N);
    }

    printf("The half-life is %.3f minutos",t);
}
/

The output that I get is 0.016 minutes, but the real value is 8.77 minutes and I do not undestand this error. I must point out that the code cannot change. I know there are two flaws in the program, so I just want to fix them. My intention is not to change the code, but rather to fix these flaws so that I get the correct value.
 
Joined
Sep 21, 2022
Messages
121
Reaction score
15
Code:
problem 1
if(rand()/RAND_MAX>lambda*dt)

change > to <

problem 2
for(i=N0;i>0;i--) {

change N0 to N
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top