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 */