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.

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:

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.