[Code Turbo C++]
#include "iostream.h"
#define max 100
// ham nhap ma tran
void Nhap(float A[max][max],int n) {
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++) {
cout<<"a["<<i<<"]["<<j<<"] = ";
cin>>A[i][j];
}
}
// ham xuat ma tran
void XuatMaTran(float A[max][max], int n) {
cout<<"\n";
for(int i=0 ; i<n; i++){
cout<<endl;
for(int j=0 ; j<n; j++)
cout<<A[i][j]<<"\t";
}
}
/*Phan ra A = LU*/
void PhanRaLU(float A[max][max], float L[max][max], float U[max][max], int n) {
for(int k =0; k<n; k++) {
U[k][k] = A[k][k];
L[k][k] = 1;
for(int i=k+1; i<n; i++) {
L[i][k] = A[i][k]/U[k][k];
U[k][i] = A[k][i];
U[i][k] = 0;
L[k][i] = 0;
}
for(i = k+1; i<n; i++)
for(int j = k+1; j<n; j++)
A[i][j] = A[i][j]-L[i][k]*U[k][j];
}
}
// ham chinh
void main() {
int n;
float A[max][max],L[max][max],U[max][max];
clrscr();
cout<<"Nhap n = ";
cin>>n;
cout<<"Nhap ma tran A\n";
Nhap(A,n);
cout<<"Ma tran A vua nhap";
XuatMaTran(A,n);
PhanRaLU(A,L,U,n);
cout<<"\nMa tran L";
XuatMaTran(L,n);
cout<<"\nMa tran U";
XuatMaTran(U,n);
getch();
}