CS 107 - Machine Problem 4
Solving Multivariable Algebraic Equations using Fractions
Due: 4/7/2003 at 2:00 pm
Write-up for an alternate (and simplier) algorithm for validating
the series of equations.
For this assignment, you are to solve a series of N algebraic equations
of N variables. The following is an example of this where N = 3.
2a + 3b + 1c = 11
4a + -2b + 3c = 9
3a + 5c + -3c = 4
In solving the above series of equations we need to translate the
series into the form:
1a + 0b + 0c = v1
0a + 1b + 0c = v2
0a + 0b + 1c = v3
Thus showing the solution is a = v1, b = v2 and c = v3. The translation
requires each equation to have the coefficient of one of the variables
changed to 1 and the remaining coefficients change to 0. Each equation
in the final series will have a different variable's coefficient changed to
1.
To change the coefficient of a variable to 1, we just need to divide all
of the coeffients and the result of the equation by the current coefficient
of the variable that we wish to have a coefficient of 1. For example, take
the equation:
2a + 3b + 1c = 11
To set the coefficient of the variable a to 1, divide each
coefficient and the result of the equation by 2 to get:
1a + 3/2b + 1/2c = 11/2
This equation would replace the original equation in the series of
equations.
To zero out the coefficient of a variable in one equation, we must
use a second equation whose coefficent for that variable is 1.
This second equation is created by the process mentioned above.
(Note:
You can use an equation whose coefficient is not 1, but it requires
a division by this coefficient. So you might as well do the division
before this step. One fact is that the equation cannot have a coefficient
of zero. This would cause a division by zero.)
Once the two equations to be used have been determined, multiple
the second equation (whose coefficient for that variable is 1)
by the coefficient for that variable from the first equation. When
multiplying the equation by a value, all coefficients and the result
must be mutliplied by the value. Then subtract the equation obtained
by the multiplication from the first equation. When subtracting, subtract
the coefficients of like variables and subtract the results separately
to obtain a new equation. This new equation should have a coefficient
of zero for the intended variable and is to replace the first equation
in the series of equations. For example, we want to zero out the
coefficient of a in the following equation (to be referred to as
the first equation).
4a + -2b + 3c = 9
and we have the second equation of:
1a + 3/2b + 1/2c = 11/2
When multiplying the second equation by 4 (the coefficient of a
in the first equation), we get:
4a + 6b + 2c = 22
This equation is subtracted from the first equation to get:
0a + -8b + c = -13
This final equation would replace the equation of 4a +-2b +3c = 9 in the
series of equations.
Your program is to solve a series of algebraic equations that may have up
to 10 variables (with the same number of equations). Your program is to
prompt the user for a filename that will contain the information for the
series of equations. The first line of the file will contain a single
integer value which informs the program of the number N for the series.
This is the number of variable in each equation and the number of equations
in the series. The next N lines in the file will contain N+1 integer
values that correspond to the coefficients and the result for each
equation. The last value on the line will be the result of the equation.
An example data file is:
3
2 3 1 11
4 -2 3 9
3 5 -3 4
This data file would correspond to the following series of equations:
2a + 3b + 1c = 11
4a + -2b + 3c = 9
3a + 5c + -3c = 4
If the file fails to open, print a descriptive error message
stating what happened
(include the name of the file in the message)
and quit the program.
If the value of N (the first integer read in) is not within the
range from 1 to 10, print a descriptive error message
stating what happened
(include the name of the file in the message)
and quit the program.
If you program encounters
any errors when reading the data file (such as non-integer values or
not enough integer values), print a descriptive error message
stating what happened
(include the name of the file in the message)
and quit the program.
The following is a list of data files. These data files do not
test all possible inputs, so you should create some data files
of your own.
When reading in the data, you may find it a good idea to store the
equations in a two dimensional array. Since there may be at most
10 equations with 11 values (the coefficients and result) in each
an array of size 10x11 would be large enough to store any possible
series of equations read into your program. If the value of N is
not 10, then just use the first N rows and N+1 columns of this array.
Once your program has read in the data file, print out the series of
equations as given in the data file.
The first variable should be the letter a and the
remaining variables should follow in order though the alphabet
(the second variable being b, the third variable being c, etc.).
Your output should align the variables in neat columns.
Now your program should prompt the user to run the program in step
mode or in continious mode. Have the user enter in either an s
or a c. If the user enters something else, print an error message
and reprompt the user. Continue doing this until the user enters an s
or a c.
In step mode, your program should print out the series of equations at
regular points though your program as the series of equations is solved
before printing the solution (or a message stating no solution exists).
In continious mode, your program should just print the solution (or
a message stating no solution exists).
To begin solving the series of equations, we must first make sure that
the coeffecients for each variable are not all zero. This is the case
when a column in the array that stores the coefficients are not all
zeros. If this occurs, then the series does not really have N variables.
Instead it has N-1 variables (assuming only one column is all zeros).
Note that it is fine if the results of all of the equations are zero.
When this occurs, print out an descriptive error message
stating what happened
and which variable was unused
(include the name of the file in the message)
and quit the program.
Alternate Algorithm
I found a different algorithm to deal with the validation of the
sequence of equation. You may use whichever you like. I think
the newer algorithm will be easier to code.
- Original Algorithm and Description
Now we must first make sure that
the coefficients "along the main diagonal" are not zero. The "main diagonal"
here refers to the first coefficient of the first equation, the second
coefficient of the second equation, etc. If one of these coefficients
is zero, we must exchange that equation with another equation so that
there will not be any zeros only the "main diagonal". Let us assume
that the i-th equation has a zero for its i-th coefficient. We can
exchange the i-th equation with the j-th equation, if the i-th coefficient
in the j-th equation is not zero and the j-th coefficient in the i-th
equation is not zero. If this j-th equation does not exist,
print out an descriptive error message
stating what happened
and which equation caused the problem
(include the name of the file in the message)
and quit the program.
If the j-th equation does exist, exchange the i-th and j-th equations.
If your program is running in step mode, print a message stating that
equations i and j were exchanged (specify the proper values for i and j
in this message) and redisplay the series of equations showing this change.
If your program is running is continious mode, only print a message stating
that
equations i and j were exchanged (specify the proper values for i and j
in this message) but DO NOT redisplay the series of equations
showing this change. To help understand this, look at the following
series of equations.
3a + 0b + 1c + 1d = 7
2a + 0b + 0c + 1d = 3
0a + 3b + -1c + 1d = 4
1a + 1b + 1c + 1d = 7
The second equation has a zero coefficient for its second variable.
This equation can't be exchanged with the first equation, since the
first equation also has a zero for its second coefficient (this exchange would
not resolve the problem). This equation can't be exanged with the third
equation, since the second equation has a zero for its third coefficient
(this exchange would just move the problem to the "new" third equation).
We could exchange the second equation with the fourth equation and continue
with the program. Now note the following series of equations.
3a + 0b + 1c = 6
2a + 0b + 0c = 2
0a + 3b + -1c = 3
The second equation has a zero coefficient for its second variable.
This equation can't be exchanged with the first equation, since the
first equation also has a zero for its second coefficient (this exchange would
not resolve the problem). This equation can't be exanged with the third
equation, since the second equation has a zero for its third coefficient
(this exchange would just move the problem to the "new" third equation).
In this case, your program cannot resolve this problem with a single
exchange. So your program should print an error message and quit.
You may note that putting the second equation first, the third equation
second and the first equation third would allow the program to be solved
but your program is not required to figure this out.
Once your program has checked for and resolved the above situations,
you may begin to translate the series of equations into the form needed
to solve the problem. It will take N steps to solve the problem,
one for each variable in the equation. For the i-th step, the coefficent
of the i-th variable in the i-th equation should be changed to 1 and the
coefficents of the i-th variables in the other equations should be zeroed
out. If your program is running in step mode, you should print out the
translated series of equations at the end of every step. This means
you should print out N series of equations showing the progress toward the
solution. If your program is running is continious mode, you should NOT
print out these series of equations.
There are two more error cases your program must be able to handle.
This occurs when the i-th coefficient of the i-th equation becomes
zero as a result of zeroing out another coefficent. If this happens,
then you must check the value of the result of the i-th equation.
If the result is non-zero, then the series of equations has no solution.
If this occurs, print out a descriptive error message
stating what happened
and which equation caused the problem
(include the name of the file in the message)
and quit the program.
If the result is zero, the the series of equation has multiple solutions.
What occurs here is that two equations in the series were multiples
of each other.
If this occurs, print out a descriptive error message
stating what happened
and which equation caused the problem
(include the name of the file in the message)
and quit the program.
- New Algorithm
This algorithm performs a simplier "pivot" operation before processing
the i-th equation instead of checking and rearranging all N equations before
any processing is done. This also removes all of the i-th coeffecient of the
j-th equation stuff.
Before processing the i-th equation, if the i-th coefficient is
zero, check the i-th coeffecients for all
equations from i to N and find the equation with the largest
absolute value for its i-th coefficient. If the i-th coefficient is
not zero, proceed as normal.
If this coeffecient with the largest absolute value
is zero, then we have an error. If this happens, then you must check
the value of the result of the i-th equation. If the result is non-zero,
then the series of equations has no solution. If this occurs, print out
a descriptive error message stating what happened and which equation
caused the problem (include the name of the file in the message) and quit
the program. If the result is zero, the the series of equation has multiple
solutions.
If the coefficient with the largest absolute value is not zero, exchange
this equation with the i-th equation and then process the equations as
with this new i-th equation. Check out the following example. This
series has already processed varaible a.
1a + 0b + 1/3c = 2
0a + 0b + -2/3c = -2
0a + 3b + -1c = 3
Before processing variable b, compare the coefficients for
variable b from equations 2 and 3 (in this case i is 2 and N
is 3). Equation 3 has the largest absolute value, so we exchange
equations 2 and 3 to get:
1a + 0b + 1/3c = 2
0a + 3b + -1c = 3
0a + 0b + -2/3c = -2
We can now proceed to set the 2nd coefficient of the 2nd equation to
1 and zero out the 2nd coefficients in the other equation.
When your program is running is step mode, it should print out the
series of equations twice for each variable. Once after it has does o
the exchange of equations and again after zeroing-out the other
equations.
If your program makes it to the final form (the i-th coefficient of
the i-th equation is 1 and the other coefficient are zero for all
equations), the result values (in column N+1) are the solutions to
the series of equations. This result value in the first equation
is the value of the first variable, the result value in the second
equation is the value for the second variable, etc. Your program is
to print out these values is some readable form when your program
is running in both step mode and continious mode. Once your program
has print ou the solution, your program may quit.
Fraction Class
To earn full credit for this assignment, create a class called Fraction
that will keep store all of the coefficients and results in the form
of a fraction (an integer numerator and an integer denominator). This
fraction must always express the number with the lowest possible
denominator. When printing out the fraction, if the denominator is
1, only print the numerator. If the denominator is not 1, print the
numerator followed immediately by the slash characher '/' followed
immediately by the denomintor (no spaces before or after the slash).
This Fraction class will be worth about 30% of the assignment. Those
programs that solve the series of equations using floating point numbers
will lose these points. However, it may be wise to first write a
program that solves problem using an array of floating point numbers
and then replace the floating point numbers with fractions after the
rest of the program is working.
Your program is to be written using functions and methods. You should have a
function for reading in the series of equations and another for
printing the series of equations. Your program should have other
functions, but you must decide what they should be. Perhaps you
can have a function for changing a coefficient to 1 and another for
zeroing out a coefficient. Other suggestions include functions for
checking for the error cases involving coefficients with zero value.
Your program must be written using good programming style. This includes
(but is not limitted to) such items as:
- A header block comment that explains who wrote the program, why and
what the program does.
- A function block comment for each function that explains what the
function does, what is returns and what each parameter is.
- In-line comments that explain specific sections of code
- Blank lines
- Indentation
- Meaningful identifer names
- Readable and understandable output
Your program will be submitted electronically on the icarus system
using the turnin command. The project name for this will be
mp4. Your program will graded based on how it executes on the
icarus system using the g++ compiler. Getting a program to work on
another system or using another compiler does NOT mean it will work
on the icarus system using the g++ compiler. Programs that do not
work properly on the icarus system using the g++ compiler will lose
points. Also programs that are not submitted via the turnin command
will also lose points. Emailing a program is not an acceptable form
of electronic submission.