Sabtu, 27 April 2013

Program Eliminasi Metode Gauss dengan Bahasa C

Pada kali ini saya akan memposting listing code program eliminasi metode Gauss (atau Gauss Naive) dengan menggunakan bahasa pemrograman C. So, cek this out:


#include
#include
#include

void input();
void tampil();

float A[25][25]; //{{1,1,1,6},{1,2,-1,2},{2,1,2,10}}
float x[25];
int n;

void main()
{
int i,j,k;
float sigma, flag, b[25];

puts("Program Eliminasi Gauss\n");

input();

for(j=0; j
{
for(i=j+1; i
{
flag = A[i][j] / A[j][j];

for(k=0;k
{
A[i][k] = A[i][k] - flag * A[j][k];
}
}
}

for(i=0;i
{
b[i] = A[i][n];
}

x[n-1] = b[n-1]/A[n-1][n-1];

for(k=n-2; k >= 0; k--)
{
sigma = 0;

for(j=k+1; j < n ; j++)
{
sigma = sigma + A[k][j] * x[j];
}

x[k] = (b[k] - sigma) / A[k][k];
}

printf("Matriks setalah dilakukan OBE\n\n");
tampil();

printf("\nHasil Penyelesaian persamaan simultan\n\n");
for(i=0;i
{
printf("x%d = %g\n",i+1,x[i]);
}
puts("\n----- END -----\n");
puts("");
}

void input()
{
int i,j;
float vek[100];

printf("Input ordo matriks : ");

scanf("%d",&n);
puts("");

for(i=0;i
{
for(j=0;j
{
printf("matrik A[%d][%d] = ",i+1,j+1);
scanf("%f",&A[i][j]);
}
}

printf("\nInput vektor matriks : \n");

for(i=0;i
{
printf("vektor [%d] = ",i+1);
scanf("%f",&vek[i]);
A[i][j] = vek[i];
}

printf("\nMatriks awal:\n");
tampil();
puts("\n");
}

void tampil()
{
static int i,j;

puts("===========================================");
for(i=0;i
{
for(j=0;j<=n;j++)
{
printf("%4g",A[i][j]);
}
printf("\n");
}
puts("===========================================");
puts("");
}

Output:


Demikian postingan kali ini, selamat mencoba dan semoga bermanfaat

10 komentar: