#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
int cek();
void gaus();
void hitung();
private:
float A[10][10], x[10];
int i, j, k, baris, kolom;
};
istream& operator >> (istream& in, matrix& A){
cout<<"masukkan jumlah persamaan :";
in>>A.baris;
A.kolom=A.baris;
A.baris-=1;
cout<<"masukkan konstanta\n";
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==A.kolom) cout<<"b"<<A.i+1<<" = ";
else cout<<"x"<<A.j+1<<" = ";
cin>>A.A[A.i][A.j];
}
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==(A.kolom-1))cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" = ";
else if (A.j==(A.kolom))cout<<A.A[A.i][A.j]<<"\n";
else cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" + ";
}
}
cout<<"matrix yang dibuat adalah\n";
return in;
}
void matrix::gaus(){
float g;
for(i=1;i<=baris;i++)
for(j=0;j<=kolom;j++){
if (j<i && A[i][j]!=0){
g=A[i][j]/A[j][j];
for(k=j;k<=kolom;k++){
A[i][k]=A[i][k]-A[j][k]*g;
}
}
}
for(i=0;i<=baris;i++){
g=A[i][i];
for(j=0;j<=kolom;j++){
A[i][j]=A[i][j]/g;
}
}
}
int matrix::cek(){
for(i=0;i<=baris;i++){
if(A[i][i]!=1) return 0;
for(j=0;j<i;j++){
if(A[i][j]!=0) return 0;
}
}
return 1;
}
void matrix::hitung(){
x[baris]=A[baris][kolom];
for(i=(baris-1);i>=0;i--){
x[i]=A[i][kolom];
for(j=(i+1);j<(kolom);j++){
x[i]-=(A[i][j]*x[j]);
}
}
}
ostream& operator << (ostream& out, matrix& A){
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++)
cout<<setw(10)<<(A.A[A.i][A.j])<<"";
cout<<endl;
}
if (A.cek()) {
A.hitung();
cout<<"maka nilai dari tiap variabel adalah";
for(A.i=0;A.i<=A.baris;A.i++){
cout<<"\nx"<<A.i+1<<"="<<A.x[A.i];
}
}
return out;
}
main(){
matrix m;
cout<<"masukkan data matrix 1\n";
cin>>m;
cout<<"matrixnya\n";
cout<<m;
m.gaus();
cout<<"hasil eliminasi gaus\n";
cout<<"matrix segitiga atas\n"<<m<<"\n";
getch();
return 0;
}
0 komentar:
Post a Comment