Contoh program sederhana invers c++

Kali ini saya akan share contoh program sederhana invers c++, semoga apa yang kami share bermanfaat bagi teman-teman semua, khusunya yang baru belajar dasar2 pemprogramman.

#include <iostream>

#include <windows.h>

using namespace std;



void gotoxy (int x, int y)

{

    COORD coord = {0, 0};

    coord.X = x; coord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}



void menu();

void matriks();

void loop();

void array1();

void exit();



void menu()

{

    system("cls");



    int pilih;

    gotoxy (35,10);

    cout<<" 1. Matriks \n";

    gotoxy (35,11);

    cout<<" 2. Loop \n";

    gotoxy (35,12);

    cout<<" 3. Array \n";

    gotoxy (35,13);

    cout<<" 4. Exit \n\n";

    gotoxy (35,14);

    cout<<"Pilih [1/2/3/4] = ";

    cin >>pilih;



    if(pilih == 1)

    {

        matriks();

    }



    else if (pilih == 2)

    {

        loop();

    }

    else if (pilih == 3)

    {

        array1();

    }

    else if (pilih == 4)

    {

        exit();

    }

    else

    {

        cout<<"Salah input ";

    }

}



void matriks()

{

    system("cls");



 int b, k, det;

    int matriksa[10][10], matriksY [10] [10], matriksHasil[10] [10];

   

    cout<<"Masukan Baris A = ";

    cin>>b;

    cout<<"Masukan Kolom A = ";

    cin>>k;



    for (int bar=1; bar<=b; bar++)

    {

        for (int kol=1; kol<=k; kol++)

        {

            cout<<"A["<<bar<<"]["<<kol<<"]= ";

            cin>>matriksa[bar][kol];

        }

       

        cout<<endl;

    }

   

    cout<<"Matriks A\n";

    for (int bar=1; bar<=b; bar++)

    {

        for (int kol=1; kol<=k; kol++)

        {

            cout<<matriksa[bar][kol]<<" ";

        }



        cout<<endl;

    }



    det = ((matriksa[1][1]*matriksa[2][2]) - (matriksa[1][2]*matriksa[2][1]));

    cout<<endl;

   

    gotoxy(30,12);

    cout<<"Determinan Matriks Adalah\n";

    gotoxy(35,13);

    cout<<"(" <<matriksa[1][1] <<"*" <<matriksa[2][2] <<")" <<" - " <<"("<<matriksa[1][2]<<" * "<<matriksa[2][1] <<")" <<"= "<<det<<endl;

   

 if (det==0)

    {

        cout<<"\n\n\nMatriks Tidak Mempunyai Invers\n";

    }

    else

    {

        cout<<"\n\n\nAdjoin Matriks Adalah\n";



        matriksY[1][1] = matriksa[2][2];

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

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

        matriksY[2][2] = matriksa [1][1];

       

        for (int bar=1; bar<=b; bar++)

        {

            for (int kol=1; kol<=k; kol++)

            {

                cout<<matriksY[bar][kol]<<" ";

            }

            cout<<endl;

        }

       

     cout<<"Hasil Invers Matriks Adalah\n";

        for (int bar=1; bar<=b; bar++)

            {

                for (int kol=1; kol<=k; kol++)

                {

                     matriksHasil[bar][kol] = (matriksY[bar][kol]) /det;

                }

               

                cout<<endl;

            }

       

            for(int bar=1; bar<=b; bar++)

            {

                for (int kol=1; kol<=k; kol++)

                {

                    cout<<matriksHasil[bar][kol]<<"   ";

                }

               

                cout<<endl;

            }

     }



     char kembali;



     cout<<"\n\nkembali ke menu [y/t] ? " ;

     cin>>kembali;



     if(kembali=='y')

     {

         matriks();

     }

     else

    {

         menu();

     }

}



void loop()

{



    system("cls");

    cout<<"for\n";



    char kembali;



    cout<<"kembali ke menu [y/t] ? " ;

    cin>>kembali;



    if(kembali=='y')

    {

        loop();

    }

    else

    {

       menu();

    }

}



void array1()

{



    system("cls");

    cout<<"array\n";



    char kembali;



    cout<<"kembali ke menu [y/t] ? " ;

    cin>>kembali;



    if(kembali=='y')

    {

        array1();

    }

    else

    {

       menu();

    }

}



void exit()

{

    system("cls");

    char pilihan;

    gotoxy (25,10);

    cout<<"Ingin Keluar Dari Program [y/n] ? =";

    cout<<endl;

    gotoxy (40,11);

    cin>>pilihan;

    if (pilihan == 'y')

    {

        return;

    }

    else if (pilihan =='n')

    {

        menu();

    }



}



main()

{

    menu();

}
Default image
Arman Basir
A New Journey. Menyukai hal-hal baru yang menantang dan kompleks untuk di pecahkan

Leave a Reply