Kamis, 20 Desember 2012

Gauss Jordan Ordo 3x3 dengan C++


Ok, ni gan postingan saya yang terakhir di bulan Desember ini dan di tahun 2012 ini. Kali ini saya akan berbagi lagi program yang saya buat, yaitu program penyelesaian Sistem Persamaan Linier (SPL) dengan metode Gauss Jordan dalam bahasa C++. Nah langsung aja ni codingnya :  

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>

main()
{
float matriks[3][4];
float a, b, c, d, e, f, g, h, i, j, k, l;
int x, y;
char lagi;
atas:
clrscr();
cout<<"   Program Penyelesaian Matriks 3x3 dengan Metode Gauss Jordan\n";
cout<<" ===============================================================\n\n";
cout<<"\t\t| A11 A12 A13 || x |   | p |\n";
cout<<"\t\t|             ||   |   |   |\n";
cout<<"\t\t| A21 A22 A23 || y | = | q |\n";
cout<<"\t\t|             ||   |   |   |\n";
cout<<"\t\t| A31 A32 A33 || z |   | r |\n";
cout<<endl<<endl;
for (x=0; x<3; x++)
{
for (y=0; y<4; y++)
   {
    if (x==0 && y==3)
      {
      cout<<" Input nilai  p  : ";
      }
      else if (x==1 && y==3)
      {
      cout<<" Input nilai  q  : ";
      }
      else if (x==2 && y==3)
      {
      cout<<" Input nilai  z  : ";
      }
      else
      {
    cout<<" Input nilai A"<<x+1<<y+1<<" : ";
      }

      cin>>matriks[x][y];
      if (x==0 && y==3)
      {
      cout<<endl;
      }
      else if (x==1 && y==3)
      {
      cout<<endl;
      }
   }
}



a=matriks[0][0]; b=matriks[0][1]; c=matriks[0][2]; d=matriks[0][3];
e=matriks[1][0]; f=matriks[1][1]; g=matriks[1][2]; h=matriks[1][3];
i=matriks[2][0]; j=matriks[2][1]; k=matriks[2][2]; l=matriks[2][3];

cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<b<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<c<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<d<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<e<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<f<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<g<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<h<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<j<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<k<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<l<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<b/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<c/a<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<d/a<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<e<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<f<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<g<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<h<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<j<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<k<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<l<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<b/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<c/a<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<d/a<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<e-(a/a*e)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<f-(b/a*e)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<g-(c/a*e)<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<h-(d/a*e)<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i-(a/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<j-(b/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<k-(c/a*i)<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<l-(d/a*i)<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<b/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<c/a<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<d/a<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<(e-(a/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(f-(b/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(g-(c/a*e))/(f-(b/a*e))<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<(h-(d/a*e))/(f-(b/a*e))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i-(a/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<j-(b/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<k-(c/a*i)<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<l-(d/a*i)<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(b/a)-((f-(b/a*e))/(f-(b/a*e))*(b/a))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(c/a)-((g-(c/a*e))/(f-(b/a*e))*(b/a))<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<(d/a)-((h-(d/a*e))/(f-(b/a*e))*(b/a))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<(e-(a/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(f-(b/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(g-(c/a*e))/(f-(b/a*e))<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<(h-(d/a*e))/(f-(b/a*e))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i-(a/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(j-(b/a*i))-((f-(b/a*e))/(f-(b/a*e))*(j-(b/a*i)))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i)))<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<(l-(d/a*i))-((h-(d/a*e))/(f-(b/a*e))*(j-(b/a*i)))<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(b/a)-((f-(b/a*e))/(f-(b/a*e))*(b/a))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(c/a)-((g-(c/a*e))/(f-(b/a*e))*(b/a))<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<(d/a)-((h-(d/a*e))/(f-(b/a*e))*(b/a))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<(e-(a/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(f-(b/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(g-(c/a*e))/(f-(b/a*e))<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<(h-(d/a*e))/(f-(b/a*e))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i-(a/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(j-(b/a*i))-((f-(b/a*e))/(f-(b/a*e))*(j-(b/a*i)))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))/
((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<((l-(d/a*i))-((h-(d/a*e))/(f-(b/a*e))*(j-(b/a*i))))/
((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))<<" |\n";


cout<<endl<<endl;
cout<<" | "<<setw(10)<<setiosflags(ios::left)<<a/a<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(b/a)-((f-(b/a*e))/(f-(b/a*e))*(b/a))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<((c/a)-((g-(c/a*e))/(f-(b/a*e))*(b/a)))-
(((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/
(f-(b/a*e))*(j-(b/a*i))))*((c/a)-((g-(c/a*e))/(f-(b/a*e))*(b/a))))<<" | | x |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<((d/a)-((h-(d/a*e))/(f-(b/a*e))*(b/a)))-
(((l-(d/a*i))-((h-(d/a*e))/(f-(b/a*e))*(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/
(f-(b/a*e))*(j-(b/a*i))))*((c/a)-((g-(c/a*e))/(f-(b/a*e))*(b/a))))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<(e-(a/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(f-(b/a*e))/(f-(b/a*e))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<((g-(c/a*e))/(f-(b/a*e)))-(((k-(c/a*i))-
((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*
(j-(b/a*i))))*((g-(c/a*e))/(f-(b/a*e))))<<" | | y | = | ";
cout<<setw(10)<<setiosflags(ios::left)<<((h-(d/a*e))/(f-(b/a*e)))-(((l-(d/a*i))-
((h-(d/a*e))/(f-(b/a*e))*(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*
(j-(b/a*i))))*((g-(c/a*e))/(f-(b/a*e))))<<" |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";
cout<<" | "<<setw(32)<<setiosflags(ios::left)<<""<<" | |   |   |            |\n";

cout<<" | "<<setw(10)<<setiosflags(ios::left)<<i-(a/a*i)<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<(j-(b/a*i))-((f-(b/a*e))/(f-(b/a*e))*(j-(b/a*i)))<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*
(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))<<" | | z |   | ";
cout<<setw(10)<<setiosflags(ios::left)<<((l-(d/a*i))-((h-(d/a*e))/(f-(b/a*e))*
(j-(b/a*i))))/((k-(c/a*i))-((g-(c/a*e))/(f-(b/a*e))*(j-(b/a*i))))<<" |\n";

cout<<"\n\n  Tekan [1] untuk mengulang program atau tekan [2] untuk menutup program!";
lagi=getche();
if (lagi=='1')
{
goto atas;
}
else
{
exit(0);
}
getch();
}

Buat agan-agan yang tidak punya aplikasi untuk menjalankan program C++ jangan khawatir, program gauss jordan.exe bisa agan-agan langsung download di sini. Sekian dulu untuk postingan kali ini, semoga bermanfaat dan dapat membantu. Thanks buat kunjungannya, dan jangan lupa jempol serta kritik dan sarannya tulis aja di komentar :)
Sampai ketemu tahun depan :D

1 komentar: