Program Invers Matriks C++

Program Invers Matriks C++

Berikut adalah contoh program C++ untuk menghasilkan invers dari sebuah matriks. Program ini menggunakan fungsi-fungsi bawaan dari library <iostream> dan <vector> untuk mempermudah operasi pada matriks.

C++
#include <iostream>
#include <vector>

using namespace std;

// Fungsi untuk mencetak matriks
void printMatrix(vector<vector<double>>& matrix) {
    int rows = matrix.size();
    int cols = matrix[0].size();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

// Fungsi untuk menghasilkan matriks identitas
vector<vector<double>> createIdentityMatrix(int size) {
    vector<vector<double>> identityMatrix(size, vector<double>(size, 0.0));

    for (int i = 0; i < size; i++) {
        identityMatrix[i][i] = 1.0;
    }

    return identityMatrix;
}

// Fungsi untuk mengalikan dua matriks
vector<vector<double>> multiplyMatrix(vector<vector<double>>& matrix1, vector<vector<double>>& matrix2) {
    int rows1 = matrix1.size();
    int cols1 = matrix1[0].size();
    int rows2 = matrix2.size();
    int cols2 = matrix2[0].size();

    vector<vector<double>> result(rows1, vector<double>(cols2, 0.0));

    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols2; j++) {
            for (int k = 0; k < cols1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    return result;
}

// Fungsi untuk mencari invers matriks
vector<vector<double>> inverseMatrix(vector<vector<double>>& matrix) {
    int size = matrix.size();
    vector<vector<double>> identityMatrix = createIdentityMatrix(size);

    for (int i = 0; i < size; i++) {
        double pivot = matrix[i][i];

        for (int j = 0; j < size; j++) {
            matrix[i][j] /= pivot;
            identityMatrix[i][j] /= pivot;
        }

        for (int j = 0; j < size; j++) {
            if (j != i) {
                double factor = matrix[j][i];

                for (int k = 0; k < size; k++) {
                    matrix[j][k] -= factor * matrix[i][k];
                    identityMatrix[j][k] -= factor * identityMatrix[i][k];
                }
            }
        }
    }

    return identityMatrix;
}

int main() {
    int size;
    cout << "Masukkan ukuran matriks: ";
    cin >> size;

    vector<vector<double>> matrix(size, vector<double>(size, 0.0));

    cout << "Masukkan elemen-elemen matriks: " << endl;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cin >> matrix[i][j];
        }
    }

    cout << "Matriks awal: " << endl;
    printMatrix(matrix);

    vector<vector<double>> inverse = inverseMatrix(matrix);

    cout << "Matriks invers: " << endl;
    printMatrix(inverse);

    return 0;
}
C++

Program di atas akan meminta input dari pengguna untuk ukuran matriks dan elemen-elemen matriks. Kemudian, program akan mencetak matriks awal dan matriks inversnya.

Contoh Lain

C++
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;
int main()
{
int a,b,j,k;
int x;
double matriks [10] [10], matriksY[10] [10], MatriksHasil [10] [10];
    cout<<"Masukkan baris A = ";
	cin>>a;

	cout<<"Masukkan kolom A = ";
	cin>>b;

	for(int bar=1; bar <=a; bar++)
	{
		for (int kol=1; kol<=b; kol++)
		{
			cout<<"mA["<<bar<<"]["<<kol<<"]= ";
			cin>>matriks[bar][kol];
		}
		cout<<endl;
	}

	 for(int bar=1; bar <=a; bar++)
	{
		for (int kol=1; kol<=b; kol++)
		{
			cout<<matriks[bar][kol]<<" ";

		}
		cout<<endl;
	}


	cout<<"Masukkan baris B = ";
	cin>>j;

	cout<<"Masukkan kolom B = ";
	cin>>k;

	for(int bar=1; bar <=j; bar++)
	{
		for (int kol=1; kol<=k; kol++)
		{
			cout<<"mB["<<bar<<"]["<<kol<<"]= ";
			cin>>matriks[bar][kol];
		}
		cout<<endl;
	}

	 for(int bar=1; bar <=j; bar++)
	{
		for (int kol=1; kol<=k; kol++)
		{
			cout<<matriks[bar][kol]<<" ";

		}
		cout<<endl;
	}


	int detb = ((matriks[1][1]*matriks[2][2]) - (matriks[1][2]*matriks[2][1]));
	cout<<endl;

	cout<<"("<<matriks[1][1]<<"*"<<matriks[2][2]<<")"<<"-"<<"("<<matriks[1][2]<<"*"<<matriks[2][1]<<")"<<"="<<detb<<endl;
	cout<<"Determinan Matriks B = "<<detb<<endl;

	if (detb == 0)
    {
        cout<<"Matriks B Tidak Memiliki Invers ";
		cout << endl;
    }
    else
    {
        cout<<"Matriks B Memiliki Invers";
		cout << endl;

        matriksY[1][1] = matriks[2][2];
        matriksY[1][2] = -(matriks[1][2]);
        matriksY[2][1] = -(matriks[2][1]);
        matriksY[2][2] = matriks[1][1];

        for(int bar=1; bar <=j; bar++)
        {
            for (int kol=1; kol<=k; kol++)
            {
                cout<<matriksY[bar][kol]<<" ";

            }
            cout<<endl;
        }

        for(int bar=1; bar<=j; bar++)
        {
            for (int kol=1; kol<=k; kol++)
            {
                MatriksHasil[bar][kol] = (matriksY[bar][kol]) /detb;
            }
            cout<<endl;
        }

        cout<<"Jumlah Invers Matriks B = ";
		cout << endl;

        for(int bar=1; bar<=j; bar++)
        {
            for (int kol=1; kol<=k; kol++)
            {
                cout<<MatriksHasil[bar][kol]<<"   ";
            }
            cout<<endl;
        }
		getchar ();
		getchar ();
}
}
C++

Silakan mencoba program di atas untuk menghasilkan invers dari matriks yang diinginkan.