# Thread: I need help for my homework. [so important]

1. ## I need help for my homework. [so important]

hello dear programmers,
i need help for my homework.

Question:
The rate of decay of a radioactive isotope is given in terms of its half-life H, the time lapse required for the isotope to decay to one-half of its original mass. The isotope cobalt-60 ( 60 Co) has a half-life of 5.272 years. Compute and print in table form the amount of this isotope that remains after each year for 5 years, given the initial presence of an amount in grams. The value of amount should be provided interactively. The amount of 60 Co remaining can be computed by using the following formula:

r = amount * C(y>H)

where amount is the initial amount in grams, C is expressed as e−0.693 ( e = 2.71828),
y is the number of years elapsed, and H is the half-life of the isotope in years.
__________

Example Code:
Code:
```#include <stdio.h>
#include <math.h>
int
main(void)
{
int year;
double cobalt_initial;
printf("Please enter the initial amount of Cobalt> ");
scanf("%lf", &cobalt_initial);
printf(" Year     Amount Total\n");
for (year = 0; year <= 5; year += 1){
cobalt_initial = cobalt_initial - cobalt_initial * exp((-.0693)*(year/(5.272)));
printf("%6c%d%8c%.2f\n", ' ', year, ' ', cobalt_initial);
}
return(0);
}``` 2. I see. So what exactly is the help that you need? 3. Originally Posted by laserlight I see. So what exactly is the help that you need?
my code is not working. i don't know where in problem. 4. Consider breaking out part of the calculation, so you can separately verify it.
Code:
```#include <stdio.h>
#include <math.h>
int main(void)
{
int year;
double cobalt_initial;
printf("Please enter the initial amount of Cobalt> ");
scanf("%lf", &cobalt_initial);
printf(" Year     Amount Decayed\n");
for (year = 0; year <= 5; year += 1) {
double decay = exp((-.0693) * (year / (5.272)));
cobalt_initial = cobalt_initial - cobalt_initial * decay;
printf("%6c%d%8c%.2f  %.2f\n", ' ', year, ' ', cobalt_initial, decay);
}
return (0);
}

\$ gcc -Wall main.c -lm
\$ ./a.out
Please enter the initial amount of Cobalt> 100
Year     Amount Decayed
0        0.00  1.00
1        0.00  0.99
2        0.00  0.97
3        0.00  0.96
4        0.00  0.95
5        0.00  0.94``` 5. As Salem said... break your code in small portions. Here's my implementation with comments for your study:
Code:
```// cobalt.c
//
//
//  \$ gcc -O2 -o cobalt cobalt.c  # add -lm if you use exp().
//

#include <stdio.h>
#include <stdlib.h>   // ony for EXIT_* symbols.

// FIX: Don't need this with precalculated constant.
//#include <math.h>

// Instead of exp(-0.693)/5.272, this C is precalculated printing
// this expression with 60 decimal places in double precision:
//
//  double C = exp(-0.693)/5.272;
//  printf( "C=%.60f\n", C );
//
#define C 0.09485462740815016335904630295772221870720386505126953125

int main ( void )
{
int year;
double initial;

printf ( "Initial amount of Cobalt (grams)> " );
fflush( stdout ); // previous print doesn't have a final '\n',
// so, it's prudent to flush stdout.

if ( scanf ( "%lf", &initial ) != 1 )
{
error:
fprintf( stderr, "Wrong amount.\n" );
return EXIT_FAILURE;
}

if ( initial <= 0.0 )
goto error;

printf ( " Years     Amount      Decay\n" );
//       "yyyyyy  aaaaaa.aa  dddddd.dd\n"
//         "% 6d  % 9.2f  % 9.2f"

// Calculate and show decay year by year.
for ( year = 0; year <= 5; year++ )
{
double remain, decay;

decay = C * year;
remain = initial * ( 1.0 - decay );

// We can format the output directly on the format argument.
// no need to use ' ' as arguments! See comment above, after printing
printf ( "% 6d  % 9.2f  % 9.2f\n", year, remain, decay );
}

return EXIT_SUCCESS;
}```
[]s
Fred Popular pages Recent additions amount, half-life, isotope, year, years 