I am working on a equation balancer for my 12th class computer science project.But somehow i am not able to balance the chemical equation.Please if anyone can provide me the source code it will be a great help.Urgently needed!

0

0

I am intruiged with this assignment.. although it's been years since I balanced a chemical equation.. I would find this to be a fun challenge.

You teach me how to balance chemical equations.. I'll give you some code.

Start me off with something simple.. and I'll code you an answer (as much as I can in accordance with daniweb policy.)

0

Balancing a equation is quite easy.

Like take this equation for an example

```
Al + O2 = Al2O3
```

Oxygen gas is diatomic, which means that the oxygen atoms, like policemen, go around in pairs. A molecule of aluminium oxide consists of two aluminium atoms combined with three oxygen atoms. Actually, technically the word "molecule" is inappropriate in that previous sentence. The formula simply tells us the ratio of aluminium atoms to oxygen atoms in the compound.

We can balance the equation by mutiplying the different atoms and molecules on each side by different amounts.

Firstly, multiply the aluminium atoms on the left side by 2.Now there are the same number of aluminium atoms on each side of the equation. We could also multiply the number of oxygen molecules on each side by one and a half (1.5), which would give three oxygen atoms on the left side (1.5 x 2 = 3) to match the three oxygen atoms on the right side.This is now balanced, but that 1.5 is a horrible thing to have in an equation - how can you have one and a half molecules? We can solve this problem by multiplying everything throughout by 2.

Now the balanced equation is

```
4 Al + 3 O2 = 2 Al2O3
```

If you count the number of atoms on each side, you will find that there are four aluminium atoms on each side and six oxygen atoms.

*Edited
by Reverend Jim*: Fixed formatting

0

There are also redox reactions which include balancing through electrons and oxidation state,but that is not needed because it will make the program very complex and lengthy.

0

How far have you gotten on your own？ Can I see your code？

0

This brings back memories.. I found the website you are thusly describing to me in your tutorial btw. Your copy/paste skills are impressive.

Based on the above example, we can crank out a possible pseduo code:

1. We'll have to prompt for an equation that we can understand. We can identify individual reactants easily. We can assume than any compound that begins with a number is a coefficient, any subsequent numbers in the compound will be subscripts. With this protocol established, we can make life a little simpler for the user in that they can enter formulas like so:

C2H6 + O2 **2**CO2 + **3**H2O

The first character of each term will have to be tested, if isdigit(), then handle the leading coefficient.

2. There are several ways to go at this point, but I will be decisive and implement my own scheme. Since the nature of an equation is "left side vs. right side", I am going to parse each side into a container:

```
#include<string>
string equation;
string left_side;
string right_side;
int pos = 0;
cout << "Enter equation to balance: ";
cin >> string equation;
pos = equation.find('=');
left_side = equation.sub_str(0, (pos-1));
right_side = equation.sub_str( (pos+1), (equation.size() - (pos-1)) );
```

3. Again, being decisive with my scheme, I decided that it would be easy to peform equation analysis if everything was packaged up nicely, so I am going to package each 'term' of the equation:

```
struct chemical
{
string element_name;
int subscript;
};
struct term
{
double leading_coefficient;
vector<chemical> element;
int element_count;
bool left_side;
};
```

4. Now we have to package the terms of the equation:

```
void pack_terms(string& bunch_of_terms, vector<term>& equation, bool& is_left)
{
int size = bunch_of_terms.size();
int subscript = 0;
int i = 0;
string leading_co;
string symbol;
string temp;
term package;
chemical compound;
//Extract any leading coefficient
while(isdigit(bunch_of_terms[i]) || ispunct(bunch_of_terms[i]))
{
leading_co += bunch_of_terms[i];
i++;
}
do{
//Extract Chemical Symbology
while(isalpha(bunch_of_terms[i]))
{
symbol += bunch_of_terms[i];
i++;
}
//Extract subscripts
while(isdigit(bunch_of_terms[i]))
{
subscript += bunch_of_terms[i];
i++;
}
//Populate reactants
compound.element_name = symbol;
istringstream str_to_int(subscript);
str_to_int >> compound.subscript;
package.element.push_back(compound);
package.element_count++;
symbol.clear();
subscript.clear();
}while(!isspace(bunch_of_terms[i]) && bunch_of_terms[i] != '+' && i < size);
//build package
istringstream str_to_int(leading_co);
str_to_int >> package.leading_coefficient;
package.left_side = is_left;
equation.push_back(package);
//Any more terms to extract?
temp = bunch_of_terms.sub_str((i+1), (size - i));
if(!temp.size())
return;
else
pack_terms(temp, equation, is_left);
}
```

5. Pack the left side and right side of the equation:

```
#define LEFT TRUE;
#define RIGHT FALSE;
vector<term> lft_side_eq;
vector<term> rt_side_eq;
pack_terms(left_side, lft_side_eq, LEFT);
pack_terms(right_side, rt_side_eq, RIGHT);
```

5. Now we can begin effective evaluation of the chemical equation:

- Obtain element count for each element on both sides of the equation
- Apply leading coefficeints in order to obtain balance
- If a fraction exists among coefficients, distribute a factor through the equation in order to obtain whole numbers.

This should be enough to give you something to think about. There might be better ways to do this. What is your opinion? Do you have a different method in mind?

Since I have more free time than I know what to do with, I will be working on my own solution.

Also, the above code is untest and may contain errors. The code is just an effort to illustrate a possible implementation for the task at hand. Also, keep in mind that this solution is catered to balancing the most basic of chemical reations.

*Edited
by Clinton Portis*: turky + duck + chicken -> turducken

0

1. We'll have to prompt for an equation that we can understand. We can identify individual reactants easily. We can assume than any compound that begins with a number is a coefficient, any subsequent numbers in the compound will be subscripts. With this protocol established, we can make life a little simpler for the user in that they can enter formulas like so:

C2H6 + O2 2CO2 + 3H2O

Should be:

C2H6 + O2 = 2CO2 + 3H2O

*Edited
by Clinton Portis*: n/a

0

This:

```
#define LEFT TRUE;
#define RIGHT FALSE;
```

should be this:

```
#define LEFT TRUE
#define RIGHT FALSE
```

0

thanks clinton portis the code was really a big help and at last i created 1st of my list of projects and ya thanks again for complementing my copy/paste skills.I use turbo C++ and many of the functions that you defined were not available in it but it was not a big problem.Cheers

-1

How to modify this for C ?

There is no scope resolution operator in C.

Please help

0

To clarify terminology, I think of the scope resolution operator in C++ as the double colon, ::. I don't see that in Clinton's code, but I may have missed it.

That being said:

1) Don't resurrect dead code. If you want to base your code off of this post, then start your own post sections of code you don't understand.

2) Porting code from one language to another can be tricky. You may be able to find some program that would do it for you, but more than likely you will have to (at least consider) doing it yourself. There are lots of things used in Clinton's code that don't have direct counter parts in C. For example, there is no Standard Template Library for C, so you can't use the vector or the STL string classes. One option would be to use lists for vectors and C style strings for STL style strings. You have to write your own code for lists in C (unless you can find a good third party library to include) and you would have to write your own C style string manipulation processes if the built in ones won't do what you want. Doing all this requires a fairly decent understanding of both languages, and even then, it isn't for the faint of heart.

0

Hi Maria - see the rules (https://www.daniweb.com/welcome/rules) in particular: provide evidence of having done some work yourself if posting questions from school or work assignments.

Now, bearing that in mind, would you care to show us how far you've got so far in terms of your code and explain exactly where it is you are getting stuck?

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics