Now we can rewrite this system in matrix form: Here we have five equations for four unknowns, however, the last one is dependent on the fourth, so it can be omitted. They will form a system of linear equations: Then we write the balance equations for each element in terms of the unknowns: We start by introducing unknown coefficients: Let me illustrate this method by example. Therefore this method could be used for any type of chemical reaction (including redox reactions). So, you just need to create a set of algebraic equations expressing the number of atoms of each element involved in the reaction and solve it. Balancing chemical equations is the process of ensuring the conservation of matter. Therefore, the number of each type of atom on each side of a chemical equation must be the same. The algebraic method is based on the Law of Conservation of Mass – that matter can neither be created nor destroyed. This chemical equation balancer uses the algebraic method – which is usually quite complex for manual calculations, however, it fits the computer program perfectly. The last two are used for redox reactions. Ion-electron method, or half-reaction method.Inspection method, or "hit & trial" method.Your program must terminate in reasonable time for all reasonable inputsĬ (gcc), 442 bytes u,t char*s,*m c,v i,j,n ī(k) else if (i != coEffs.length-1) out += "+" Ĭonsole.log(solve("Al+Fe2O4->Fe+Al2O3")) Ĭonsole.There are several methods of balancing chemical equations: This is code-golf, so shortest code wins! A sample input that has no solution is Pb->Au For instance, the following is illegal: 40Al+20Fe2O3->40Fe+20Al2O3 Your program must yield numbers such that their sum is minimal. A number must always be a positive integer. If the number for a molecule is 1, drop it. For the example above, a valid output is: 2Al+Fe2O3->2Fe+Al2O3 The number of atoms for each element must be the same on both sides of the arrow. The output of your program is the input equation augmented with extra numbers. Your program may either read the input from standard input, from the first argument or in an implementation defined way at runtime if neither is possible. You may assume, that the input is never longer than 1024 characters. If the input is invalid, your program may do whatever you like. The input is terminated with a newline and won't contain any spaces. The molecules are separated with + signs, an ASCII-art arrow -> is inserted between both sides of the equation: Al+Fe2O4->Fe+Al2O3 Element names always start with a capital letter and may be followed by a minuscule. In order to make this possible in pure ASCII, we write any subscriptions as ordinary numbers. The input is a chemical equation without amounts. Since you are Bernd's tutor, it is your job to help him! Write a program, that calculates the amount of each substance needed to get a valid chemical equation. Since mathematics isn't exactly Bernd's strongest subject, he often has a hard time finding the exact ratios between the pro- and educts of the reaction. In class he has to design chemical equations for some experiments they are doing, such as the combustion of heptane C7H16: Bernd is a high school student who has some problems in chemistry.
0 Comments
Leave a Reply. |