Iterative Technique in C++

iterative technique in C++

Iterative technique such as Jacobi's method and Guass-Seidel's method can be used to calculate simultaneous equations

04 Oct 2022

mathc++

Introduction

This program uses iterative technique such as Jacobi's method and Guass-Seidel's method to calculate simultaneous equations. Users can input the number of iterations and change the equations in the code.

Code

/*Eong Koungmeng 4/10/2022 Iterative Solutions*/
#include<iostream>

void JacobiMethod(int n)
{
    std::cout << "Jacobi's Method" << std::endl;
    float x=0, y = 0;
    float temp_x = 0;
    for (int i = 0; i < n; i++)
    {
        temp_x = -1.0f / 2 * y + 2;
        y = 1.0f / 3 * x + 5.0f / 3;
        x = temp_x;

        std::cout << i+1 <<". x:" << x << "\ty: " << y << std::endl;
    }

}

void GuassSeidelMethod(int n)
{
    std::cout << "Guass-Seidel's Method" << std::endl;
    float x = 0, y = 0;
    for (int i = 0; i < n; i++)
    {
        x = -1.0f / 2 * y + 2;
        y = 1.0f / 3 * x + 5.0f / 3;

        std::cout << i + 1 << ". x:" << x << "\ty: " << y << std::endl;
    }

}

int main()
{
    std::cout << "Please input the number of iteration for Jacobi's method: ";
    int n;
    std::cin >> n;
    JacobiMethod(n);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << std::endl;

    std::cout << "Please input the number of iteration for Guass-Seidel's method: ";
    std::cin >> n;
    GuassSeidelMethod(n);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}

Result