next up previous
Next: Assessment Up: Program Structure Previous: Program Structure

Coulomb's Law

Coulomb's law relates the energy of interaction between a pair of ions and the distance between their centers and their numerical ion charges. The energy interaction in Jules $E$ between a pair of ions is given by

\begin{displaymath}E = 2.31 \times 10^{-19} \frac{ Q_1 Q_2}{r} .\end{displaymath}

Where r is the distance between the ion centers in nm, and $Q_1$ and $Q_2$ are the numerical ion charges.

A simple main() program that receives as input the distance between the ion centers and the numerical ion charges, and outputs the energy interaction is presented below. The input is from the file Coulomb.txt. The results are sent to the file EnergyInter.txt.

/* Begin program file */
/* Begin preprocessor and header file section */
#include <fstream> /* suitable I/O file stream */

#include <cmath> /* when necessary for invocation of certain
                     Mathematical functions */
/* End   preprocessor and header file section */

using std::ifstream;
using std::ofstream;

using std::endl;


ifstream fin("Coulomb.txt");
ofstream fout("EnergyInter.txt");

/* Program preamble which describes the object of the program
           its required input and its output */
/* Coulomb's law relates the energy of interaction between a pair
of ions and the distance between
their centers and their numerical ion charges.
the program that receives as input the distance between the ion
centers and the
numerical ion charges, and outputs the energy interaction.
----------------------------------------------------------------
The variables are :  Q1, Q2 are the numerical ion charges. 
                     r is the distance between the ion centers in nm.
                     EnergInter is the energy interaction.
*/

int main() 
{
/* Variable declaration section
   All C++ variables must be declared before use.  */
   float Q1, Q2, r, EnergInter;



/* Variable initialization and input section */

   fin >> Q1 >> Q2 ;
/* Statements carrying out the calculations */

   EnergInter = 2.31E-9 * Q1*Q2 /r ;

/* Output section */

   fout << "The Energy Interaction is " << EnergInter << endl;
}
/* End of Program file */



J. C. Diaz 2004-01-19