Skip to content

Instantly share code, notes, and snippets.

@bwedding
Last active May 24, 2021 05:29
Show Gist options
  • Save bwedding/cb64733c600e7615dbfec6076c26b505 to your computer and use it in GitHub Desktop.
Save bwedding/cb64733c600e7615dbfec6076c26b505 to your computer and use it in GitHub Desktop.
Simple C++ program to solve 2 variable systems of equations using elimination method.
// SystemsOfEquations.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
struct Equation
{
double xCoefficent;
double yCoefficent;
double result;
};
struct Equations
{
Equation eq1;
Equation eq2;
};
struct Answer
{
double x;
double y;
bool solved = false;
};
// Solve systems of equations by elimination
Answer SystemsOfEquationsSolver(const Equations equ)
{
Answer answer;
// Determine a value to multiply equation 2 by which will eliminate
// the y coefficients when the equations are added together
// So we're doing 50.2 / 0.55 = 91.2727, then we're negating it
const auto eliminator = -(equ.eq1.yCoefficent / equ.eq2.yCoefficent);
// We're going to multiply equation 2 by this value which will yield
// -91.2727 * 2.1x + -91.2727 * 0.55y = -91.2727 * 5.9
// -191.6727X + -50.2y = -538.5090
// ^^^^^^
// See what I did there? Now y is gone when we add this to equation1,
// that's why I call it the eliminator
// Add them:
// 3.4x + 50.2y = 44.5
// -191.6727x + -50.2y = -538.5090
// _________________________________
// -188.2727x + 0y = -494.009090
// Add the x's after multiplying by the eliminator yields -188.2727x
const auto XSum = equ.eq2.xCoefficent * eliminator + equ.eq1.xCoefficent;
// Add the results after multiplying by the eliminator yields -494.009090
const auto ResultSum = equ.eq2.result * eliminator + equ.eq1.result;
// After eliminating y, equation 2 now is:
// -188.2727x = -494.009090
// Divide both sides by the x coefficient -188.2727 to solve for x
// x = 2.6239
const auto x = ResultSum / XSum;
// Ok, now we know X so plug it into our original equation 2 (or 1)
// As a reminder, equation 2 is:
// 2.1x + 0.55y = 5.9
// So substituting in our x value:
// 2.1 * 0.33459 + 0.55y = 5.9
// Simplifies to:
// 0.70265 + 0.55y = 5.9
// Plug the solved x back into equation 2 and multiply by its coefficient
const auto x2 = equ.eq2.xCoefficent * x; // 2.1 * 2.6239 = 0.70265
// Subtract x from both sides to eliminate the constant
// 0.55y = 5.9 - (2.1 * 0.33459)
// 0.55y = 0.38980
const auto val = equ.eq2.result - x2;
// Divide by 0.55 to isolate y
// y = 0.38980 / 0.55
// y = 0.7087
const auto y = val / equ.eq2.yCoefficent;
// Package the results and send them home
answer.x = x;
answer.y = y;
answer.solved = true;
return answer;
}
int main()
{
const Equation equ1 = { 3.4, 50.2, 44.5 };
const Equation equ2 = { 2.1, 0.55, 5.9 };
const Equations equations
{
equ1, equ2
};
std::cout << "Solving: " << equ1.xCoefficent << "X + " << equ1.yCoefficent << "Y = " << equ1.result << "\n";
std::cout << "And: " << equ2.xCoefficent << "X + " << equ2.yCoefficent << "Y = " << equ2.result << "\n";
const auto answer = SystemsOfEquationsSolver(equations);
std::cout << "The solution is: x = " << answer.x << " y = " << answer.y << "\n";
// Test results
std::cout << "Testing Solution 3.4 * " << answer.x << " + 50.2 * " << answer.y <<
" = " << answer.x * 3.4 + answer.y * 50.2 << "\n";
std::cout << "Testing Solution 2.1 * " << answer.x << " + 0.55 * " << answer.y <<
" = " << answer.x * 2.1 + answer.y * 0.55 << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment