Classic Computer Magazine Archive ANTIC VOL. 5, NO. 1 / MAY 1986

MOLECULAR WEIGHT CALCULATOR

Chemist's Anti Drudgery Tool

by JIM PIERSON-PERRY

Chemistry students exult! MW-CALC will calculate the molecular weight of virtually any chemical formula. This BASIC program works on all Atari 8-bit computers of any memory size, with disk or cassette.

Anyone taking a chemistry class quickly finds out that determining the molecular weight of a chemical formula is a repetitive and boring task. In addition, it seems as if all calculations in chemistry are based on molecular weight. I have faithfully done this for years, but no longer! Now my Atari computer has taken over this chore with MW-CALC.

This program accepts a chemical formula as input, checks it for errors and then displays the molecular weight and an elemental composition table (percentage of each element in the total molecular weight). Note that chemical element symbols use both upper and lower case letters, so be sure that the lower case keys are toggled on, or you will be flagged for errors.

My previous BASIC programming approach had been to quickly hammer out code and hope that any needed corrections could be done later by patching. This worked, but it yielded a messy program which was difficult to follow during the debugging phase.

With MW-CALC, I changed to a structured programming approach-- plan the overall task, break it into smaller tasks, code the small pieces and logically assemble them following the overall plan. I found that this method greatly speeded up my software writing. And by having an organized program structure, debugging time was a bare minimum (an ounce of prevention. . .).

BACKGROUND BUZZWORDS

To use MW-CALC and understand its structure, you need some simple knowledge of a few chemistry terms. (Chemistry students go directly to USING THE PROGRAM and do not collect $200).

All substances are made up of combinations of building blocks called elements. For example, table salt is made up of the elements sodium (Na) and chlorine (Cl). Each elements has its own atomic weight. When elements join together to make a substance, the result is called a molecule (such as NaCl--salt) and its molecular weight is the sum of all the atomic weights of the elements in it.

To complicate things, sometimes small groups of elements combine to make a unit called a radical which acts just as if it were an element. An example is NH4 which is part of the molecule (NH4)2SO4 (ammonium sulfate, a fertilizer).

Radicals are enclosed within parentheses and may have a subscript like a regular element. Nested radicals are not allowed. Finally, a molecule may have some number of a smaller molcule associated with it. This is called (at least by me) a hydration complex. An example of this is Na2B4O7*9H2O (borax). Only one hydration complex (if any) can be in a formula.

Another useful piece of information about a molecule is its elemental composition. This is a table showing what percentage of the total molecular weight comes from each element in the molecular formula. For this calculation, all occurrences of an element within the formula are lumped together.

In the borax formula above there are 16 occurrences of the element oxygen (O), seven in the main molecule and nine in the hydration complex.

USING THE PROGRAM

MW-CALC is a BASIC program that will accept a chemical formula and display its molecular weight and elemental composition. It is error trapped to prevent use of invalid element symbols or expressions and will flag them in the input string (if present) for easy correction.

The input formula is limited to no more than 39 characters and no more than 12 different elements. This should not be a problem, since the largest formula I know of is only 23-characters long and the largest number of elements in a formula that I could find was eight. If more elements are present, the molecular weight will be calculated but the elemental composition will not be displayed (to prevent messing up the display screen).

Type in Listing 1, MWCALC.BAS. Check it with TYPO II and SAVE a copy before you RUN it.

When you RUN the program, it will first display an introductory screen, then take a few seconds to initialize variables and arrays. An input screen will then appear and wait for you to type in the chemical formula.

Again, be sure to use correct upper/lower case letters for the element symbols or you will be flagged for invalid elements. If any errors are detected, the buzzer will sound and an error message will be printed along with arrows pointing to the offending character(s). Press any key to re-enter the corrected formula.

After a few seconds, the display screen will appear with the results and an option to enter another formula. Answering "No" to the option will terminate the program.

PROGRAM TAKE-APART

Here is an overview of the program structure. I put the often used subroutines at the front of the code to speed up execution time.

2900-3900            Subroutine to get subscripts. 
4200-5200            Subroutine to get element symbol and test for validity. 
5600-6000            Subroutine to signal start of radical.
6500-7300            Subroutine to signal end of radical.
7800-8400            Subroutine to signal start of hydration complex.
8700-9900            Start of main program/initialization.
10000-11700          Formula input.
11900-15000          Main loop to evaluate formula and build element composition table.
15600-16900          Complete element composition table and merge redundant element   
                     entries.
17100-19200          Display results.   
19800-21000          Error handling routine.
21400-22100          Element atomic weight data. 

After initialization, the program displays the input prompt screen and waits for the string input (F$). The string is tested to be sure it is not null or exceeds 39 characters, then the main evaluation loop begins.

A pointer (P1) is set to the first character in the string. Only the following characters are allowed:

( or [                   Start of a radical
) or ]                   End of a radical
* or .                   Start of a hydration
                         complex
Capital letter           Start of a radical
                         symbol

If the character is a capital letter, the next character is also tested to see if it is a lower case letter. (If not, the second character is assumed to be a null.) Chemical symbols are either a capital letter, or a capital followed by a lower case letter.

This test symbol (E$) is then compared with an array of valid element symbols (SYM$) and gives an index number (ATNUM) into the atomic weights array (WT). The pointer is moved to the next character after the test symbol and a subscript is obtained (if present, else default value=1).

The atomic weight is then multiplied by the subscript and added into the running molecular weight sum (MW). The main loop is then continued until the pointer exceeds the formula string length.

If a radical is detected, a flag (RFLAG) is set and the elements within the radical are added into a temporary radical sum (RTEMP). At the end of the radical a subscript (RSUB) is obtained, the flag is reset and the radical weight is multiplied by the subscript and added to the overall molecular weight. A hydration complex is handled like a radical except that the subscript comes first.

While debugging, I ran into the Atari BASIC bug of computing a negative zero value (A=0:PRINT -A). Although this bug has been documented elsewhere (e.g. The Atari BASIC Source Book), it was my first encounter with it. The problem came in line 13900 and I got around it by using the equivalent expression 0-A instead of -A. The bug does not occur with BASIC XL.

Jim Pierson-Perry is a research chemist with DuPont. Although Jim has programmed a variety of computers over a 10-year period, he became an Atari evangelist when his daughter's school began using them in 1982.

Listing:MWCALC.BAS Download