Question

Hello I'm having trouble writing a solution code for my homework. I am required to ask the user to input 6 variable for two equations. After successfully pulling numbers only, I need to find the slope of each line, the y-intercept for each line, two points on each line(ordered pair such as (2,1)), if there is a solution. Also what the relationship is. I have searched and searched for mostly number checking and equations. What I am having trouble with is finding the points and solutions to the equations.

#include <iostream>
#include <limits>

int main()
{
std::cout<<"This program is designed to test two linear equations. \n";
std::cout<<"In order to best solve the system, \n";
std::cout<<"equations will be in the form of a*x + b*y = c. \n";
std::cout<<"and d*x + e*y =f. \n";
std::cout<<"Please enter an integer for a, b, and c. \n";
double a, b, c, d, e, f;

while ((std::cout << "Enter a.") 
     && !(std::cin >> a))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter b.")
          && !(std::cin >> b))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter c.")
          && !(std::cin >> c))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout<<"Equation 1 is "<<a<<"x +"<<b<<"y ="<<c;

std::cout<<"Please enter an integer for d, e, and f. \n";

while ((std::cout << "Enter d.")
         && !(std::cin >> d))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter e.")
          && !(std::cin >> e))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter f.")
          && !(std::cin >> f))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cout<<"Equation 2 is "<<d<<"x +"<<e<<"y ="<<f;

    double slope1, slope2;
    double x1, x2, y1, y2;
    slope1 = (b / a);
    slope2 = (e / d);
    std::cout<<" Slope of Equation 1 is "<<slope1<<"\n";
    std::cout<<" Slope of Equation 2 is "<<slope2<<"\n";

    x1 = ((c*e)-(b*f))/ ((a*e)-(b*d));
    y1 = ((a*f)-(c*d))/ ((a*e)-(b*d));

    return 0;
}
Was it helpful?

Solution

Something else you should think about is the use of matrices to solve linear equations.

A lot of comptuers use calculations on the echelon form of an augmented matrix.

ie.

2x + 3y = 36 x + 9y = 8

[2 3 36] [1 9 8]

So this is your augmented matrix and then you work on it to transform it into echelon form. My linear algebra professor tells me that this is the most common method that programmers use to write calculations on systems of equations.

I'm not exactly qualified to teach it so here is a nifty article.

http://stattrek.com/matrix-algebra/echelon-form.aspx

OTHER TIPS

See Solving Systems of Equations Using Determinants

enter image description here

Have user enter A, B, C, and D, E, F and accept that using cin as you are already doing. (Make the code simpler though!) (Suggestion: Use a 3-element array for each equation.)

Once you have that, you can use the determinant based formula to directly compute the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top