A Simple Interactive Program that Can Solve A Linear System of Equations
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
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.
c++ matrix
add a comment |Â
up vote
5
down vote
favorite
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.
c++ matrix
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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.
c++ matrix
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.
c++ matrix
edited Jun 7 at 14:11
asked Jun 7 at 12:00
Arnav Borborah
622119
622119
add a comment |Â
add a comment |Â
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.
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
 |Â
show 1 more comment
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.
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
 |Â
show 1 more comment
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.
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
 |Â
show 1 more comment
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.
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.
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
 |Â
show 1 more comment
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
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password