#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <conio.h>
double A[10][10],T[10][10],X[10],Y[10],C[10][10],D[10];
int n;
double E;
void vvodsort()
{
int i,j,k,max;
cout<<"Vvedi pogreshnosti ";
cin>>E;
cout<<"\nVvedi n ";
cin>>n;
cout<<"\nVvedi matritu koeffitientov A \n";
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>A[i][j];
T[i][j]=A[i][j];
}
cout<<"\nVvedi matritu svob koeffitientov B\n";
for(i=0;i<n;i++) cin>>A[i][j];
for(i=0;i<n;i++){
for(j=i;j<n;j++){
if(fabs(A[j][i])>fabs(A[i][i]))max=j;
}
for(k=0;k<n;k++)
Y[k]=A[i][k];
int y;
y=A[i][n];
A[i][n]=A[max][n];
A[max][n]=y;
for(k=0;k<n;k++)
{
A[i][k]=A[max][k];
A[max][k]=Y[k];
}
}
}
int proverka()
{
int i,j,k,z=0;double sum;
for(i=0;i<n;i++){
sum=0;
for(j=0;j<n;j++)
sum=sum+fabs(A[i][j]);
if(fabs(A[i][i])<fabs(sum-fabs(A[i][i]))){z=1;break;}
}
if(z==0)return 1; else return 0;
}
void zegeli()
{
int i,j;
for(i=0;i<n;i++)
{
Y[i]=X[i]=A[i][n]/A[i][i];
D[i]=A[i][n]/A[i][i];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)C[i][j]=0;
else C[i][j]=A[i][j]/(-A[i][i]);
}
int z=0,c=0;
while(z!=n)
{
for(i=0;i<n;i++)
Y[i]=X[i];
double t=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
t=t+C[i][j]*X[j];
X[i]=t+D[i];
t=0;
}
z=0;
for(i=0;i<n;i++)
if(X[i]-Y[i]<E)z++;
c++;
}
cout<<"\nKorni \n ";
for(i=0;i<n;i++)
cout<<X[i]<<" ";
cout<<"\nKolicestvo iteratii = "<<c;
cout<<"\nProverka ";
z=n;
double t=0;
for(i=0;i<n;i++){
cout<<"\n";
for(j=0;j<n;j++){
cout<<A[i][j]<<"X"<<j+1;
if(A[i][j+1]<0);
else if(j!=2)cout<<"+";
t=t+A[i][j]*X[j];
}
cout<<"="<<t;
t=0;
}
}
void main()
{
clrscr();
vvodsort();
if(proverka()) {
cout<<"\nUslovie vipolniaetsea ! ";
zegeli();
}
else cout<<"\nUslovie ne vipolniaetsea ! ";
getch();
}