Cod sursa(job #1642351)

Utilizator zVoxtyVasile Sebastian Costinel zVoxty Data 9 martie 2016 13:41:32
Problema Algoritmul lui Gauss Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include <stdio.h>
#define maximnr 310
#define erroare 0.0000001

int nrlin, nrcol;

double mat[maximnr][maximnr];
double X[maximnr];

void readMatrix(){
    freopen("gauss.in","r",stdin);
        scanf("%d %d",&nrlin, &nrcol);
            for(int i = 1; i <= nrlin; ++i){
                for(int j = 1; j <= nrcol + 1; ++j){
                    scanf("%lf",&mat[i][j]);
                }
            }
}

 int i = 1, j = 1, k;

void rezolvareGauss(){

    while(i <= nrlin && j << nrcol){
        for(k = i; k <= nrlin; ++k){
            if(mat[k][j] <- erroare || mat[k][j] > erroare)
                break;
        }

        if(k == nrlin + 1){
            ++j;
            continue;
        }

        if(k != i){
            for(int x = 1; x <= nrcol + 1; ++x){
                double aux = mat[i][x];
                mat[i][x] = mat[k][x];
                mat[k][x] = aux;
            }
        }

        for(int x = j + 1; x <= nrcol + 1; ++x){
            mat[i][x] /= mat[i][j];
        }

        mat[i][j] = 1;

        for(int u = i + 1; u <= nrlin; ++u){
            for(int l = j + 1; l <= nrcol + 1; ++l){
                mat[u][l] -= mat[u][j] * mat[i][l];
            }
            mat[u][j] = 0;
        }

        ++i;
        ++j;
    }
}

void afisare(){
    freopen("gauss.out","w",stdout);
    for(int i = 1; i <= nrcol; ++i){
        printf("%.8lf ",X[i]);
    }
    printf("\n");
}

int main(){

    readMatrix();
    rezolvareGauss();
    for(i = nrlin; i > 0; --i){
        for(j = 1; j <= nrcol + 1; ++j){
            if(mat[i][j] > erroare || mat[i][j] <- erroare){
                if(j == nrcol + 1){
                    printf("Fara solutie");
                    return 0;
                }

                X[j] = mat[i][nrcol + 1];
                for(k = j + 1; k <= nrcol; ++k){
                    X[j] -= X[k] * mat[i][k];
                }

                break;
            }
        }
    }
    afisare();
    return 0;
}