usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

Solving 3x3 Simultaneous Equations in C++ | Elimination Method Algorithm



Solving Simultaneous Equations in C++: A Junior Secondary Guide

Welcome to this junior secondary C++ math project! In this tutorial, you'll learn how to solve simultaneous equations with three unknowns using C++. This is a great way to combine your coding skills with algebra and logic.
You'll also learn the following:

  • How to use C++ to solve 3x3 simultaneous equations
  • Applying the elimination method step-by-step
  • Using LCM (Least Common Multiple) to simplify equations
  • Writing a C++ class to automate the solving process
Solving equations is a key part of algebra. By coding the solution in C++, you'll not only understand the math better; you'll also build a useful tool. This project is perfect for students looking to explore C++ math algorithms, or teachers seeking C++ algebra exercises for the classroom.


How to Solve Three-Variable Algebra Problems | Maths Explanation for C++ Kids

To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our C++ Code for Simultaneous Equations with 2 Unknowns module.



Step-by-Step Guide to Solve Three-Variable Algebra Equations | Elimination Method C++ Algorithm

Let's try to draft a C++ algorithm that solves simultaneous equations with 2 unknowns, using the elimination method, with the following set of equations in consideration.
         x + 2y - z = 2; and
         3x - y + 2z = 4
         2x + 3y + 4z = 9
These steps will help the student understand both the math and the logic behind the code.

Step 1:

Using the Find LCM in C++ class from the Primary Category, find the LCM of the coefficients of variable z. Multiply equations 1, 2 & 3 by the LCM of the coefficients of variable z, divided by the z coefficient of the respective equation.
         (4/-1) X (x + 2y - z = 2)
    ⇒     -4x - 8y + 4z = -8
         (4/2) X (3x - y + 2z = 4)
    ⇒     6x - 2y + 4z = 8
         (4/4) X (2x + 3y + 4z = 9)
    ⇒     2x + 3y + 4z = 9

Step 2:

Subtract the new equations obtained in Step 2; eqn (2) from eqn (1) and eqn (3) from eqn (2).
         -4x - 8y + 4z = -8
    -     6x - 2y + 4z = 8
    ⇒     -10x - 6y = -16
         6x - 2y + 4z = 8
    -     2x + 3y + 4z = 9     ⇒     4x - 5y = -1

Step 3:

Call out to our C++ Code for Simultaneous Equations with 2 Unknowns module to solve for x and y.
⇒         (x, y) = (1, 1);

Step 4:

Obtain z by solving for z from any of the original equations, using the found values of x and y.
         x + 2y - z = 2
⇒         1 + 2(1) - z = 2;
⇒         -z = 2 - 3 = -1;
⇒         z = -1/-1 = 1;


Create a new C++ class file; call it Simultaneous3Unknown.
Type out the adjoining C++ code for solving simultaneous equations with 3 unknowns.


Note: The code module for finding LCM in C++ has been explained in the Primary Category.

You can comment out the Simultaneous2Unknown C++ object code in the main class from the previous lesson or simply continue from where it stopped.



So! C++ Fun Practice Exercise - Simultaneous Equations with 3 Unknowns

As a fun practice exercise, feel free to try out your own set of x_coefficients, y_coefficients and equals values, and see how the C++ code solves the resulting 3x3 Simultaneous Equations.







C++ Code for Solving Simultaneous Equations with 3 Unknowns - Header File

#pragma once

#include "Simultaneous2Unknown.h"
#include "LCM.h"
#include <vector>

class Simultaneous3Unknown {
public:
    Simultaneous3Unknown(int[], int[], int[], int[]);
    virtual ~Simultaneous3Unknown();
    void solveSimultaneous();
    double x_variable;
    double y_variable;
    double z_variable;
private:
    int x_coefficients[3];
    int y_coefficients[3];
    int z_coefficients[3];
    int equals[3];
    double eliminator[3][3];

};


C++ Code for Solving Simultaneous Equations with 3 Unknowns - Class File

#include "stdafx.h"
#include "Simultaneous3Unknown.h"


Simultaneous3Unknown::Simultaneous3Unknown(int x_coeff[], int y_coeff[], int z_coeff[], int eq[]) {
    for (unsigned i = 0; i < 3; i++) {
        x_coefficients[i] = x_coeff[i];
        y_coefficients[i] = y_coeff[i];
        z_coefficients[i] = z_coeff[i];
        equals[i] = eq[i];
    }
}

void Simultaneous3Unknown::solveSimultaneous() {
    int lcm;
    vector<unsigned> z_arg = {
        (unsigned)abs(z_coefficients[0]),
        (unsigned)abs(z_coefficients[1]),
        (unsigned)abs(z_coefficients[2])
    };
    LCM l_c_m(z_arg);
    lcm = l_c_m.getLCM();

    // STEP 1:
    eliminator[0][0] = (lcm * x_coefficients[0]) / z_coefficients[0];
    eliminator[0][1] = (lcm * y_coefficients[0]) / z_coefficients[0];
    eliminator[0][2] = (lcm * equals[0]) / z_coefficients[0];

    eliminator[1][0] = (lcm * x_coefficients[1]) / z_coefficients[1];
    eliminator[1][1] = (lcm * y_coefficients[1]) / z_coefficients[1];
    eliminator[1][2] = (lcm * equals[1]) / z_coefficients[1];

    eliminator[2][0] = (lcm * x_coefficients[2]) / z_coefficients[2];
    eliminator[2][1] = (lcm * y_coefficients[2]) / z_coefficients[2];
    eliminator[2][2] = (lcm * equals[2]) / z_coefficients[2];

    // STEP 2:
    double new_x[] = {
        eliminator[0][0] - eliminator[1][0],
        eliminator[1][0] - eliminator[2][0]
    };
    double new_y[] = {
        eliminator[0][1] - eliminator[1][1],
        eliminator[1][1] - eliminator[2][1]
    };
    double new_eq[] = {
        eliminator[0][2] - eliminator[1][2],
        eliminator[1][2] - eliminator[2][2]
    };

    try {
        // STEP 3
        Simultaneous2Unknown s2u(new_x, new_y, new_eq);
        s2u.solveSimultaneous();

        x_variable = s2u.x_variable;
        y_variable = s2u.y_variable;
        if (z_coefficients[0] == 0) throw 0;
        // STEP 4:
        z_variable = (equals[0] - x_coefficients[0] * x_variable - y_coefficients[0] * y_variable) / z_coefficients[0];
    }
    catch (int e) {
        throw e;
    }
}

Simultaneous3Unknown::~Simultaneous3Unknown()
{
}

C++ Code for Solving Simultaneous Equations with 3 Unknowns - Main Class

// Algebra.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Simultaneous3Unknown.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 {
        /*
        * Simultaneous Equations with 3 unknowns
        */

        wchar_t infinity = '\U00000097';
        char operators3D[3][2];

        int x_coeff3D[]{ 2, 4, 2 };
        int y_coeff3D[]{ 1, -1, 3 };
        int z_coeff3D[]{ 1, -2, -8 };
        int equals3D[]{ 4, 1, -3 };

        for (int i = 0; i < 3; i++) {
            operators3D[i][0] = '+';
            if (y_coeff3D[i] < 0) {
                operators3D[i][0] = '-';
            }
            operators3D[i][1] = '+';
            if (z_coeff3D[i] < 0) {
                operators3D[i][1] = '-';
            }
        }

        cout << "\n    Solving simultaneously the equations:\n";
        // Print as an equation
        printf(
            "%40dx %c %dy %c %dz = %d\n", x_coeff3D[0], operators3D[0][0],
            abs(y_coeff3D[0]), operators3D[0][1], abs(z_coeff3D[0]), equals3D[0]
        );
        printf(
            "%40dx %c %dy %c %dz = %d\n", x_coeff3D[1], operators3D[1][0],
            abs(y_coeff3D[1]), operators3D[1][1], abs(z_coeff3D[1]), equals3D[1]
        );
        printf(
            "%40dx %c %dy %c %dz = %d\n", x_coeff3D[2], operators3D[2][0],
            abs(y_coeff3D[2]), operators3D[2][1], abs(z_coeff3D[2]), equals3D[2]
        );
        printf("\n%30s\n%40s""Yields:""(x, y, z)  =  ");

        try {
            Simultaneous3Unknown sim3unk(x_coeff3D, y_coeff3D, z_coeff3D, equals3D);
            sim3unk.solveSimultaneous();

            printf("(%.4f, %.4f, %.4f)\n", sim3unk.x_variable, sim3unk.y_variable, sim3unk.z_variable);

        }
        catch (...) {
            printf("(%c, %c, %c)\n", infinity, infinity, infinity);
        }

        cout << "\n\n";

    } catch (exception& e) {
        cout << "\n" << e.what() << "\n";
    }

    return 0;
}




<< Previous Next >>