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!

C++
#include <iostream>
#include <cmath>
#include <string>
#include <unordered_map>

std::unordered_map<std::string, double > electronegativities = {
    {"H", 2.20}, {"He", 2.58}, {"Li", 0.98}, 
    {"Na", 0.93}, {"K", 0.82}, {"Rb", 0.82}, 
    {"Cs", 0.79}, {"Be", 1.57}, {"Mg", 1.31}, 
    {"Ca", 1.00}, {"Sr", 0.95}, {"Ba", 0.89}, 
    {"I", 2.66}, {"B", 2.04}, {"C", 2.55}, 
    {"N", 3.04}, {"O", 3.44}, {"F", 3.98}, 
    {"Si", 1.90}, {"P", 2.19}, {"S", 2.58}, 
    {"Cl", 3.16}, {"Ge", 2.01}, {"As", 2.18}, 
    {"Se", 2.55}, {"Br", 2.96}, {"Te", 2.10}, 
    {"Xe", 2.60}, {"Kr", 3.00}, {"Br", 2.96}
};

static const double invalid_negativity = -2.00;

double get_electronegativity(const std::string& element) {
    if (electronegativities.find(element) != electronegativities.end()) {
        return electronegativities[element];
    }
    return invalid_negativity;
}

std::string determine_bond(const std::string& element1, const std::string& element2) {
    double en1 = get_electronegativity(element1);
    double en2 = get_electronegativity(element2);
    if (en1 == invalid_negativity || en2 == invalid_negativity) {
        return "Unknown element!";
    }
    double diff = std::fabs(en1 - en2);
    if (diff <= 0.4) return "Nonpolar Covalent";
    else if (diff <= 1.6) return "Polar Covalent";
    else return "Ionic";
}

// ... Additional functions and main method ...
//what they see
void clear(){
    std::system("clear");//clears screen
}
      void user() {

    clear();
    print();

    std::string element1, element2;

    while (element1.empty()) {

        std::cout << "\n\nEnter the element 1 symbol: ";
        element1 = checker();
    }

    while (element2.empty()) {
        std::cout << "\n\nEnter the second element 2 symbol: ";
        element2 = checker();
    }

    clear();
    print();

    std::cout << "\n\nBond Type between " << element1 << " and " << element2 << ": "
            << determine_bond(element1, element2) << "\n";
}

//... printing periodic table and other functions ...

int main(){
    user();
    return 0;
}
    
 
Women in STEM