A Simple Interactive Program that Can Solve A Linear System of Equations

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite
3












So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could implement it as a program. The result is the following:



/*
*
* Solve a system of equations in two variables, x and y
*
*/

#include <iostream>

struct LinearEquation

double xCoefficient;
double yCoefficient;
double constantTerm;
;

struct Solution

double x;
double y;
;

LinearEquation readEquation();
double findDeterminant(const LinearEquation& a, const LinearEquation& b);
Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant);

int main()

std::cout << "Welcome to the linear equation solver! Enter the following inputs for two equations of the form ax + by = c.nn";

LinearEquation one, two;

std::cout << "Equation #1n";
one = readEquation();

std::cout << "Equation #2n";
two = readEquation();

double det = findDeterminant(one, two);
if (det != 0)

Solution solution = solveEquation(one, two, det);
std::cout << "The solution:n";
std::cout << "x = " << solution.x << "n";
std::cout << "y = " << solution.y << "n";

else

std::cout << "This linear equation has no unique solutions!n";



LinearEquation readEquation()

LinearEquation result;
std::cout << "X coefficient: ";
std::cin >> result.xCoefficient;
std::cout << "Y coefficient: ";
std::cin >> result.yCoefficient;
std::cout << "Constant term: ";
std::cin >> result.constantTerm;

return result;


double findDeterminant(const LinearEquation& a, const LinearEquation& b)

return a.xCoefficient * b.yCoefficient - a.yCoefficient * b.xCoefficient;


Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant)

// Calculating the inverse
double matrixTL = b.yCoefficient / determinant;
double matrixTR = -1 * (a.yCoefficient / determinant);
double matrixBL = -1 * (b.xCoefficient / determinant);
double matrixBR = a.xCoefficient / determinant;

Solution result;
// Matrix multiplication
result.x = matrixTL * a.constantTerm + matrixTR * b.constantTerm;
result.y = matrixBL * a.constantTerm + matrixBR * b.constantTerm;

return result;



This program simply goes through each coefficient and constant term and basically applies a "formula" to solve the equation. It works as far, but it could have minor bugs. I am looking for ways this program can be improved in terms of program size or efficiency. Another problem I want to solve is in terms to making this program handle more equations/variables, which would currently require manually adding parts of the matrix.







share|improve this question



























    up vote
    5
    down vote

    favorite
    3












    So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could implement it as a program. The result is the following:



    /*
    *
    * Solve a system of equations in two variables, x and y
    *
    */

    #include <iostream>

    struct LinearEquation

    double xCoefficient;
    double yCoefficient;
    double constantTerm;
    ;

    struct Solution

    double x;
    double y;
    ;

    LinearEquation readEquation();
    double findDeterminant(const LinearEquation& a, const LinearEquation& b);
    Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant);

    int main()

    std::cout << "Welcome to the linear equation solver! Enter the following inputs for two equations of the form ax + by = c.nn";

    LinearEquation one, two;

    std::cout << "Equation #1n";
    one = readEquation();

    std::cout << "Equation #2n";
    two = readEquation();

    double det = findDeterminant(one, two);
    if (det != 0)

    Solution solution = solveEquation(one, two, det);
    std::cout << "The solution:n";
    std::cout << "x = " << solution.x << "n";
    std::cout << "y = " << solution.y << "n";

    else

    std::cout << "This linear equation has no unique solutions!n";



    LinearEquation readEquation()

    LinearEquation result;
    std::cout << "X coefficient: ";
    std::cin >> result.xCoefficient;
    std::cout << "Y coefficient: ";
    std::cin >> result.yCoefficient;
    std::cout << "Constant term: ";
    std::cin >> result.constantTerm;

    return result;


    double findDeterminant(const LinearEquation& a, const LinearEquation& b)

    return a.xCoefficient * b.yCoefficient - a.yCoefficient * b.xCoefficient;


    Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant)

    // Calculating the inverse
    double matrixTL = b.yCoefficient / determinant;
    double matrixTR = -1 * (a.yCoefficient / determinant);
    double matrixBL = -1 * (b.xCoefficient / determinant);
    double matrixBR = a.xCoefficient / determinant;

    Solution result;
    // Matrix multiplication
    result.x = matrixTL * a.constantTerm + matrixTR * b.constantTerm;
    result.y = matrixBL * a.constantTerm + matrixBR * b.constantTerm;

    return result;



    This program simply goes through each coefficient and constant term and basically applies a "formula" to solve the equation. It works as far, but it could have minor bugs. I am looking for ways this program can be improved in terms of program size or efficiency. Another problem I want to solve is in terms to making this program handle more equations/variables, which would currently require manually adding parts of the matrix.







    share|improve this question























      up vote
      5
      down vote

      favorite
      3









      up vote
      5
      down vote

      favorite
      3






      3





      So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could implement it as a program. The result is the following:



      /*
      *
      * Solve a system of equations in two variables, x and y
      *
      */

      #include <iostream>

      struct LinearEquation

      double xCoefficient;
      double yCoefficient;
      double constantTerm;
      ;

      struct Solution

      double x;
      double y;
      ;

      LinearEquation readEquation();
      double findDeterminant(const LinearEquation& a, const LinearEquation& b);
      Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant);

      int main()

      std::cout << "Welcome to the linear equation solver! Enter the following inputs for two equations of the form ax + by = c.nn";

      LinearEquation one, two;

      std::cout << "Equation #1n";
      one = readEquation();

      std::cout << "Equation #2n";
      two = readEquation();

      double det = findDeterminant(one, two);
      if (det != 0)

      Solution solution = solveEquation(one, two, det);
      std::cout << "The solution:n";
      std::cout << "x = " << solution.x << "n";
      std::cout << "y = " << solution.y << "n";

      else

      std::cout << "This linear equation has no unique solutions!n";



      LinearEquation readEquation()

      LinearEquation result;
      std::cout << "X coefficient: ";
      std::cin >> result.xCoefficient;
      std::cout << "Y coefficient: ";
      std::cin >> result.yCoefficient;
      std::cout << "Constant term: ";
      std::cin >> result.constantTerm;

      return result;


      double findDeterminant(const LinearEquation& a, const LinearEquation& b)

      return a.xCoefficient * b.yCoefficient - a.yCoefficient * b.xCoefficient;


      Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant)

      // Calculating the inverse
      double matrixTL = b.yCoefficient / determinant;
      double matrixTR = -1 * (a.yCoefficient / determinant);
      double matrixBL = -1 * (b.xCoefficient / determinant);
      double matrixBR = a.xCoefficient / determinant;

      Solution result;
      // Matrix multiplication
      result.x = matrixTL * a.constantTerm + matrixTR * b.constantTerm;
      result.y = matrixBL * a.constantTerm + matrixBR * b.constantTerm;

      return result;



      This program simply goes through each coefficient and constant term and basically applies a "formula" to solve the equation. It works as far, but it could have minor bugs. I am looking for ways this program can be improved in terms of program size or efficiency. Another problem I want to solve is in terms to making this program handle more equations/variables, which would currently require manually adding parts of the matrix.







      share|improve this question













      So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could implement it as a program. The result is the following:



      /*
      *
      * Solve a system of equations in two variables, x and y
      *
      */

      #include <iostream>

      struct LinearEquation

      double xCoefficient;
      double yCoefficient;
      double constantTerm;
      ;

      struct Solution

      double x;
      double y;
      ;

      LinearEquation readEquation();
      double findDeterminant(const LinearEquation& a, const LinearEquation& b);
      Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant);

      int main()

      std::cout << "Welcome to the linear equation solver! Enter the following inputs for two equations of the form ax + by = c.nn";

      LinearEquation one, two;

      std::cout << "Equation #1n";
      one = readEquation();

      std::cout << "Equation #2n";
      two = readEquation();

      double det = findDeterminant(one, two);
      if (det != 0)

      Solution solution = solveEquation(one, two, det);
      std::cout << "The solution:n";
      std::cout << "x = " << solution.x << "n";
      std::cout << "y = " << solution.y << "n";

      else

      std::cout << "This linear equation has no unique solutions!n";



      LinearEquation readEquation()

      LinearEquation result;
      std::cout << "X coefficient: ";
      std::cin >> result.xCoefficient;
      std::cout << "Y coefficient: ";
      std::cin >> result.yCoefficient;
      std::cout << "Constant term: ";
      std::cin >> result.constantTerm;

      return result;


      double findDeterminant(const LinearEquation& a, const LinearEquation& b)

      return a.xCoefficient * b.yCoefficient - a.yCoefficient * b.xCoefficient;


      Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant)

      // Calculating the inverse
      double matrixTL = b.yCoefficient / determinant;
      double matrixTR = -1 * (a.yCoefficient / determinant);
      double matrixBL = -1 * (b.xCoefficient / determinant);
      double matrixBR = a.xCoefficient / determinant;

      Solution result;
      // Matrix multiplication
      result.x = matrixTL * a.constantTerm + matrixTR * b.constantTerm;
      result.y = matrixBL * a.constantTerm + matrixBR * b.constantTerm;

      return result;



      This program simply goes through each coefficient and constant term and basically applies a "formula" to solve the equation. It works as far, but it could have minor bugs. I am looking for ways this program can be improved in terms of program size or efficiency. Another problem I want to solve is in terms to making this program handle more equations/variables, which would currently require manually adding parts of the matrix.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 7 at 14:11
























      asked Jun 7 at 12:00









      Arnav Borborah

      622119




      622119




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Why not do the arithmetic in parentheses? That way, it will be clearer.
          The rest of the code is very clear and understandable, I didn't find any bugs.






          share|improve this answer























          • What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
            – Arnav Borborah
            Jun 7 at 14:05










          • It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 15:33










          • I get the second part, but why is it convenient for the compiler? (Do you have a source?)
            – Arnav Borborah
            Jun 7 at 15:43










          • you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 16:32










          • Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
            – Arnav Borborah
            Jun 7 at 17:40










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196029%2fa-simple-interactive-program-that-can-solve-a-linear-system-of-equations%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Why not do the arithmetic in parentheses? That way, it will be clearer.
          The rest of the code is very clear and understandable, I didn't find any bugs.






          share|improve this answer























          • What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
            – Arnav Borborah
            Jun 7 at 14:05










          • It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 15:33










          • I get the second part, but why is it convenient for the compiler? (Do you have a source?)
            – Arnav Borborah
            Jun 7 at 15:43










          • you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 16:32










          • Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
            – Arnav Borborah
            Jun 7 at 17:40














          up vote
          1
          down vote













          Why not do the arithmetic in parentheses? That way, it will be clearer.
          The rest of the code is very clear and understandable, I didn't find any bugs.






          share|improve this answer























          • What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
            – Arnav Borborah
            Jun 7 at 14:05










          • It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 15:33










          • I get the second part, but why is it convenient for the compiler? (Do you have a source?)
            – Arnav Borborah
            Jun 7 at 15:43










          • you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 16:32










          • Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
            – Arnav Borborah
            Jun 7 at 17:40












          up vote
          1
          down vote










          up vote
          1
          down vote









          Why not do the arithmetic in parentheses? That way, it will be clearer.
          The rest of the code is very clear and understandable, I didn't find any bugs.






          share|improve this answer















          Why not do the arithmetic in parentheses? That way, it will be clearer.
          The rest of the code is very clear and understandable, I didn't find any bugs.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 7 at 13:16









          Daniel

          4,1132836




          4,1132836











          answered Jun 7 at 13:03









          Марк Стариков

          436




          436











          • What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
            – Arnav Borborah
            Jun 7 at 14:05










          • It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 15:33










          • I get the second part, but why is it convenient for the compiler? (Do you have a source?)
            – Arnav Borborah
            Jun 7 at 15:43










          • you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 16:32










          • Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
            – Arnav Borborah
            Jun 7 at 17:40
















          • What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
            – Arnav Borborah
            Jun 7 at 14:05










          • It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 15:33










          • I get the second part, but why is it convenient for the compiler? (Do you have a source?)
            – Arnav Borborah
            Jun 7 at 15:43










          • you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
            – ÐœÐ°Ñ€Ðº Стариков
            Jun 7 at 16:32










          • Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
            – Arnav Borborah
            Jun 7 at 17:40















          What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
          – Arnav Borborah
          Jun 7 at 14:05




          What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant?
          – Arnav Borborah
          Jun 7 at 14:05












          It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
          – ÐœÐ°Ñ€Ðº Стариков
          Jun 7 at 15:33




          It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like
          – ÐœÐ°Ñ€Ðº Стариков
          Jun 7 at 15:33












          I get the second part, but why is it convenient for the compiler? (Do you have a source?)
          – Arnav Borborah
          Jun 7 at 15:43




          I get the second part, but why is it convenient for the compiler? (Do you have a source?)
          – Arnav Borborah
          Jun 7 at 15:43












          you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
          – ÐœÐ°Ñ€Ðº Стариков
          Jun 7 at 16:32




          you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion
          – ÐœÐ°Ñ€Ðº Стариков
          Jun 7 at 16:32












          Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
          – Arnav Borborah
          Jun 7 at 17:40




          Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying!
          – Arnav Borborah
          Jun 7 at 17:40












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196029%2fa-simple-interactive-program-that-can-solve-a-linear-system-of-equations%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation