// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab5 /* Simple "carbon-14 Age calculation Program" The program computes the approximate age of an artifact using carbon-14 dating. It allows the user to continue doing this as many times as they want. The carbon-14 dating formula is the following : Age = -5700 * (ln(percentage_of_carbon14_remaining)/ ln(2) ) where age is the approximate age in years and the percentage of carbon14 remaining is a fraction of 1.0. */ #include #include using namespace std; void main() { /* declaration of variables */ float pct_carbon; // the percentage of carbon14 remaining float age; // the approximation of the age calculated char answer; // the users answer to the question if he wants to continue or not do { // the idea is to keep user's entry validation locally. We will insist on ansking him // the same que while (true) { cout << "Please enter percentage of carbon14 remaining [0.0-1.0] : "; cin >> pct_carbon; // stop only if pct_carbon ranges from 0.0-1.0 if (pct_carbon>0.0 && pct_carbon<=1.0) { break; } cout << "Warning! - Percentage of carbon14 can only be between (0.0,1.0]\n"; } // calculating age based on the given formula age = -5700 * ( log(pct_carbon) / log(2.0) ); // output answer cout << "The approximation of the age (based on the carbon14 formula is : " << age << endl; // check users answer on question whether to continue or not do { cout << "\n\nDo you want to do the calculation again (y/n) :"; cin >> answer; } while (answer!='y' && answer!='n'); } while (answer=='y' || answer=='Y'); // main loop }