2 by 2 Simultaneous Equations Code in C++
Well, it has not been much of algebra so far; We are about to have
our first taste of it.
Simultaneous equations can be solved using either
Elimination or Substitution method.
We will be using the Elimination method.
Algorithm Steps to Follow for Simultaneous Equations with 2 unknowns in C++
Consider the equations:
2x + 3y = 13; and
5x - y = 7
Step 1:
Pick a variable to eliminate, either x or y.
Our code will always eliminate the y variable.
Step 2:
Multiply equation 1 by the coefficient of variable y
in equation 2.
⇒
-1 X (2x + 3y = 13)
⇒
-2x - 3y = -13
Step 3:
Multiply equation 2 by the coefficient of variable y
in equation 1.
⇒
3 X (5x - y = 7)
⇒
15x - 3y = 21
Step 4:
Subtract the new equations obtained from Steps 2 and 3.
-2x - 3y = -13
-
15x - 3y = 21
⇒
-17x = -34
Step 5:
Divide the R.H.S. from Step 4 by the coefficient of
x to obtain x.
⇒
x = -34/-17 = 2;
Step 6:
Obtain y by solving for y from any of the
original equations, using the found value of x.
2x + 3y = 13
⇒
2(2) + 3y = 13;
⇒
3y = 13 - 4 = 9;
⇒
y = 9/3 = 3;
Create a new class file;
call it Simultaneous2Unknown.
Type out the adjoining C++ code for solving simultaneous equations with 2 unknowns.
Note: You can comment out the SortFraction C++ object
code in the main class from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of x_coefficients,
y_coefficients and equals values for 2 by 2 Simultaneous Equations.
C++ code for Simultaneous2Unknown Header File
#pragma once
#include <iostream>
#include <exception>
class Simultaneous2Unknown {
public:
Simultaneous2Unknown(double[], double[], double[]);
void solveSimultaneous();
double x_variable;
double y_variable;
virtual ~Simultaneous2Unknown();
private:
double x_coefficients[2];
double y_coefficients[2];
double equals[2];
double eliminator[2][2];
};
Simultaneous2Unknown Class File
#include "stdafx.h"
#include "Simultaneous2Unknown.h"
Simultaneous2Unknown::Simultaneous2Unknown(double x_coeff[], double y_coeff[], double eq[]) {
x_coefficients[0] = x_coeff[0];
x_coefficients[1] = x_coeff[1];
y_coefficients[0] = y_coeff[0];
y_coefficients[1] = y_coeff[1];
equals[0] = eq[0];
equals[1] = eq[1];
}
void Simultaneous2Unknown::solveSimultaneous() {
eliminator[0][0] = y_coefficients[1] * x_coefficients[0];
eliminator[0][1] = y_coefficients[1] * equals[0];
eliminator[1][0] = y_coefficients[0] * x_coefficients[1];
eliminator[1][1] = y_coefficients[0] * equals[1];
try {
if (eliminator[0][0] - eliminator[1][0] == 0) throw 0;
x_variable = (eliminator[0][1] - eliminator[1][1]) / (eliminator[0][0] - eliminator[1][0]);
if (y_coefficients[0] == 0) throw 0;
y_variable = (equals[0] - x_coefficients[0] * x_variable) / y_coefficients[0];
}
catch (double e) {
throw e;
}
}
Simultaneous2Unknown::~Simultaneous2Unknown()
{
}
main class
#include "stdafx.h"
#include "Simultaneous2Unknown.h"
#include <iostream>
#include <vector>
#include <exception>
using namespace std;
int main() {
cout << "\n Welcome to our demonstration sequels";
cout << "\n Hope you enjoy (and follow) the lessons.\n\n";
vector<unsigned> numerators;
vector<unsigned> denominators;
try {
char * infinity = "infinity";
char operators2D[]{ '+', '+' };
double x_coeff2D[]{ 2, 1 };
double y_coeff2D[]{ -1, 1 };
double equals2D[]{ 1, 2 };
if (y_coeff2D[0] < 0) {
operators2D[0] = '-';
}
if (y_coeff2D[1] < 0) {
operators2D[1] = '-';
}
cout << "\n Solving simultaneously the equations:\n";
printf("%40.2fx %c %.2fy = %.2f\n", x_coeff2D[0], operators2D[0], abs(y_coeff2D[0]), equals2D[0]);
printf("%40.2fx %c %.2fy = %.2f\n", x_coeff2D[1], operators2D[1], abs(y_coeff2D[1]), equals2D[1]);
printf("\n%30s\n%40s", "Yields:", "(x, y) = ");
try {
Simultaneous2Unknown sim2unk(x_coeff2D, y_coeff2D, equals2D);
sim2unk.solveSimultaneous();
printf("(%.4f, %.4f)\n", sim2unk.x_variable, sim2unk.y_variable);
}
catch (...) {
printf("(%s, %s)\n", infinity, infinity);
}
cout << "\n\n";
} catch (exception& e) {
cout << "\n" << e.what() << "\n";
}
return 0;
}