Contoh Program Matrix 4x4 C++


Hai Sobat Blogger !! :)


Kalau sebelumnya saya mengshare contoh program Matrix 3x3, kali ini saya akan mengshare contoh program Matrix 4x4 :)
Disini saya menggunakan aplikasi CodeBlock.

Bagi yang belum punya aplikasinya bisa download disini 
Server 1 : >>Link<<
Server 2 : >>Link<<
 
ini screanshot nya bisa dilihat dibawah + kode programnya :)
 
 




Program:
#include<iostream>
#include<iomanip>
using namespace std;
float det3(float a, float b, float c, float d, float e, float f, float g, float h, float i)
{
    float n=a*e*i+b*f*g+c*d*h;
    float m=g*e*c+d*b*i+a*h*f;
    cout<<a<<b<<c<<endl;
    cout<<d<<e<<f<<"\t "<<n-m<<endl;
    cout<<g<<h<<i<<endl<<endl;
    return n-m;
}

int main()
{
    int i, j;
    cout<<"---------- Program Matriks 4x4 ----------\n\n";
    cout<<"Masukkan Matriks\n";
    float a[4][4], b[4][4], det;
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
        cout<<"["<<i<<"]"<<"["<<j<<"]= ";
        cin>>a[i][j];
        }
    }
    cout<<endl<<endl<<endl<<endl<<endl<<endl;
    b[0][0]=det3(a[1][1],a[1][2],a[1][3],a[2][1],a[2][2],a[2][3],a[3][1],a[3][2],a[3][3]);
    b[0][1]=det3(a[1][0],a[1][2],a[1][3],a[2][0],a[2][2],a[2][3],a[3][0],a[3][2],a[3][3])*-1;
    b[0][2]=det3(a[1][0],a[1][1],a[1][3],a[2][0],a[2][1],a[2][3],a[3][0],a[3][1],a[3][3]);
    b[0][3]=det3(a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2],a[3][0],a[3][1],a[3][2])*-1;
    b[1][0]=det3(a[0][1],a[0][2],a[0][3],a[2][1],a[2][2],a[2][3],a[3][1],a[3][2],a[3][3])*-1;
    b[1][1]=det3(a[0][0],a[0][2],a[0][3],a[2][0],a[2][2],a[2][3],a[3][0],a[3][2],a[3][3]);
    b[1][2]=det3(a[0][0],a[0][1],a[0][3],a[2][0],a[2][1],a[2][3],a[3][0],a[3][1],a[3][3])*-1;
    b[1][3]=det3(a[0][0],a[0][1],a[0][2],a[2][0],a[2][1],a[2][2],a[3][0],a[3][1],a[3][2]);
    b[2][0]=det3(a[0][1],a[0][2],a[0][3],a[1][1],a[1][2],a[1][3],a[3][1],a[3][2],a[3][3]);
    b[2][1]=det3(a[0][0],a[0][2],a[0][3],a[1][0],a[1][2],a[1][3],a[3][0],a[3][2],a[3][3])*-1;
    b[2][2]=det3(a[0][0],a[0][1],a[0][3],a[1][0],a[1][1],a[1][3],a[3][0],a[3][1],a[3][3]);
    b[2][3]=det3(a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[3][0],a[3][1],a[3][2])*-1;
    b[3][0]=det3(a[0][1],a[0][2],a[0][3],a[1][1],a[1][2],a[1][3],a[2][1],a[2][2],a[2][3])*-1;
    b[3][1]=det3(a[0][0],a[0][2],a[0][3],a[1][0],a[1][2],a[1][3],a[2][0],a[2][2],a[2][3]);
    b[3][2]=det3(a[0][0],a[0][1],a[0][3],a[1][0],a[1][1],a[1][3],a[2][0],a[2][1],a[2][3])*-1;
    b[3][3]=det3(a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2]);

    det=a[0][0]*b[0][0]+a[0][1]*b[0][1]+a[0][2]*b[0][2]+a[0][3]*b[0][3];

    cout<<endl<<"Matriks nya adalah: \n";
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            cout<<setw(4)<<a[i][j];
        }
    cout<<endl;
    }

    cout<<endl<<endl<<"Matriks kofaktor nya adalah: \n";
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            cout<<setw(4)<<b[i][j];
        }
    cout<<endl;
    }

    cout<<endl<<endl<<"Matriks adjoint nya adalah: \n";
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            cout<<setw(4)<<b[j][i];
        }
    cout<<endl;
    }

    cout<<"\nDeterminannya adalah: \n"<<det<<"\n\n Matriks inversnya adalah: \n";
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            cout<<setprecision(2)<<setw(7)<<b[j][i]/det;
        }
    cout<<endl;
    }
}

0 komentar:

Posting Komentar

 
Copyright © Tutorial Danzen