Women in STEM

View Original

Cub Codes [Episode 1]: Coding an Ionic and Covalent Element Finder.

Produced by Kaylee Ashley Inglis

Genre: Science and Coding

Please note this is basic code and this code was written by Kaylee Inglis WiSTEM Website Manager! This is NOT A LESSON BUT A REPORT (with the code)!


I have a close chemistry with coding, leading me to code an Ionic and Covalent finder in C++. So, why not smash a subject I love with despicable chemistry? I originally wanted to code this in Python, but C++ is much prettier and cleaner. Please note I'm more interested in diving into logic than visual aspects of coding. Open your terminals, and let's splash into a simplified version of an Ionic and Covalent finder!


To code something, you need to know what you want to code. Since I am not as experienced with chemistry, I had to conduct simple research to code my finder. I needed a periodic table and a table of electronegativity values. I found two pictures that provided this information from our best friend, Chrome. 

The first thing we must code is the electronegativity database. My database will be using std::unordered_map. If you are unaware of maps in C++, you have a keyword, and the treasure that keyword unlocks. In our case, the keyword will be the element, and the treasure will be the electronegativity value. To receive the electronegativity, I created a function called get_electronegativity. This function looks up elements(from the periodic table) and retrieves the electronegativity. The most important part of the code is the determined bond type function. In the rules of chem:

  • "Nonpolar Covalent" is when the difference is 0.4 or less. 

  • "Polar Covalent" is when the difference is between 0.4 and 1.6.

  • "Ionic" if the difference is more than 1.6.

So, the function follows those guidelines using a simple mathematical formula. The user interface uses a simple print function that prints out the periodic table so the user knows what elements are available to calculate. For the user interaction, I made a function called user, which basically uses basic std::cout to receive the user data. The final code is below: feel free to use the code!

See this content in the original post