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

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    Question 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.
    __________

    not C++ please C.

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I see. So what exactly is the help that you need?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    2
    Quote Originally Posted by laserlight View Post
    I see. So what exactly is the help that you need?
    my code is not working. i don't know where in problem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    As Salem said... break your code in small portions. Here's my implementation with comments for your study:
    Code:
    // cobalt.c
    //
    // Compile and link:
    //
    //  $ 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.
    
      // Check for your inputs!
      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
        // the 'header'.
        printf ( "% 6d  % 9.2f  % 9.2f\n", year, remain, decay );
      }
    
      return EXIT_SUCCESS;
    }
    []s
    Fred
    Last edited by flp1969; 11-20-2020 at 12:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with this please (IMPORTANT)
    By damha in forum C Programming
    Replies: 11
    Last Post: 04-06-2011, 10:30 AM
  2. Homework guidence, very important
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2002, 07:28 PM
  3. &...how important?
    By CAP in forum C Programming
    Replies: 1
    Last Post: 07-16-2002, 02:02 AM
  4. Is it more important...
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-09-2002, 05:01 AM

Tags for this Thread