Cod sursa(job #622980)

Utilizator rootsroots1 roots Data 18 octombrie 2011 20:16:11
Problema Algoritmul lui Gauss Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <fstream>
#include <cstring>
#include <iomanip>

#define EWidth 301
#define EHeight 301

#define SSize 301

using namespace std;

ifstream in;
ofstream out;

double S[SSize];
double E[EHeight][EWidth];

int main()
{
    int M,N,pos;

    in.open("gauss.in");

    in>>M>>N;
    for(int i=1;i<=M;++i)
    {
        for(int j=1;j<=N;++j) in>>E[i][j];
        in>>S[i];
    }

    in.close();

    out.open("gauss.out");

    if(M<N) out<<"Imposibil\n";
    else
    {
        for(int i=1;i<=N;++i)
        {
            if(E[i][i]==0)
            {
                for(int j=i+1;j<=M;++j)
                    if(E[j][j]!=0)
                    {
                        pos=j;
                        break;
                    }

                for(int aux,j=i;j<=N;++j)
                {
                    aux=E[i][j];
                    E[i][j]=E[pos][j];
                    E[pos][j]=aux;
                }
            }

            for(int j=i+1;j<=N;++j) E[i][j]/=E[i][i];
            S[i]/=E[i][i];
            E[i][i]=1;

            for(int j=i+1;j<=M;++j)
                if(E[j][i]!=0)
                {
                    for(int k=i+1;k<=N;++k)
                        E[j][k]-=E[j][i]*E[i][k];
                    S[j]-=S[i]*E[j][i];
                    E[j][i]=0;
                }
        }

        for(int i=N-1;i;--i)
            for(int j=i+1;j<=N;++j)
                S[i]-=S[j]*E[i][j];

        out<<fixed;
        for(int i=1;i<N;++i)
            out<<setprecision(8)<<S[i]<<' ';
        out<<setprecision(8)<<S[N]<<'\n';
    }

    /*for(int i=1;i<=N;++i)
    {
        for(int j=1;j<=N;++j)
            out<<E[i][j]<<' ';
        out<<S[i]<<'\n';
    }*/

    out.close();

    return 0;
}