Scientific Notation

Apr 6, 2009 at 2:34am
Hi Im makin a program in c++ for chemistry conversions, but I'm having trouble with the scientific notation of a constant heres the code
#include <iostream> // This program is for basic conversions in chemistry
#include <string>

using namespace std;

int main()
{
float moles;
float atoms;
float grams;
int choice;
double const avo = 6.022e+23;

cout<<"What would you like to convert ";
cout<<"Press 1 for moles to atoms ";
cout<<"Press 2 for atoms to moles ";
cout<<"Press 3 for grams to atoms ";
cout<<"Press 4 for atoms to grams ";
cout<<"Press 5 for moles to grams ";
cout<<"Press 6 for grams to moles ";
cin>> choice;

if ( choice == 1 ) {
cout.precision(4);
cout<<"Enter a number of moles ";
atoms = moles * avo;
cin>> moles;
cout<<"The number of atoms are "<< atoms << "\n";
cin.ignore();
}
cin.get();
return 0;
}

The code I think I'm having trouble with is the stuff in bold could some one please comment. This is not for school either my school, sadly, doesn't have a programming class which sucks, but any how please help. Thanks in advanced whOOper
Last edited on Apr 6, 2009 at 2:35am
Apr 6, 2009 at 3:30am
The constant is Avogadro's Number, which is used to figure out how many atoms there are in a given volume of some substance.
See
http://gemini.tntech.edu/~tfurtsch/scihist/avogadro.htm
and
http://en.wikipedia.org/wiki/Avogadro%27s_number


To express 6.022 x 1023 to a computer program, replace the "x 10" with an "e" and follow it with the exponent: 6.022e+23.

Another example: 1.6605387 x 10-27 kg becomes 1.6605387e-27 kg.

Hope this helps.
Apr 6, 2009 at 11:05am
Thanks for the post, but if you look in the above code I posted with the question I did this and Im getting answers that are way off. could it possibly just be a computer error although I doubt that.
Apr 6, 2009 at 11:36am
closed account (z05DSL3A)
You are doing the calculation atoms = moles * avo; before you have a value for moles.

This will calculate 6.022e+23 multiplied by a junk value producing a junk result.

Your compiler should have given you a warning about using an initialized variable...

You may want to change you floats to doubles as well.
Last edited on Apr 6, 2009 at 11:40am
Apr 6, 2009 at 5:52pm
Thanks grey wolf Im at school so i cant try the modifications, but thanks for replying
Apr 7, 2009 at 12:35am
Hello there,
I don't know if you're still checking up on this post but anyway I just wanted to make a little correction concerning the way you are calculating the number of atoms.

Your program is not scientificaly correct, I say this because the value you are obtaining with the operation moles * avo is the number of molecules, not atoms. In order to calculate the number of atoms you'd need to know what element we are talking about.
The oxygen molecule, O2, for example, is composed by two atoms of oxygen. 1 mol of oxygen doesn't have 6.02e+23 atoms, it has 6.02e+23 molecules and 12.04e+23 atoms, because in this case you multiply the Avogadro constant by two.
Another example, the methane molecule, CH4, it's 1 carbon atom and 4 Hydrogen linked together. 1 mol of methane has 6.02e+23 molecules, 6.02e+23 atoms of carbon and 4*6.02e+23 atoms of hydrogen, 26.10e+24 atoms in total (no calculator here, and i'm sleepy so I can't ensure my calculations' "rightness").

Hope this helps :)
Best Regards,
~Deimos
Apr 8, 2009 at 2:41pm
Hi thanks for that update I just found out about that problem yesterday Im going to my chem teacher to see if he has general equations I can use. Thanks for that
Apr 8, 2009 at 5:51pm
Glad to help!
Happy programming! :D
Topic archived. No new replies allowed.