- 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:
/
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.
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.