OiO.lk Blog C# c code for solving an augmented matrix of size [N][N+1]
C#

c code for solving an augmented matrix of size [N][N+1]


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void pivot(double **matrix, int n, int row) {
    double maxElement = fabs(matrix[row][row]);
    int maxRow = row;

    for (int i = row + 1; i < n; i++) {
        if (fabs(matrix[i][row]) > maxElement) {
            maxElement = fabs(matrix[i][row]);
            maxRow = i;
        }
    }

    if (maxRow != row) {
        double *temp = matrix[row];
        matrix[row] = matrix[maxRow];
        matrix[maxRow] = temp;
    }
}

void gaussianElimination(double **matrix, int n) {
    for (int j = 0; j < n; j++) {
        pivot(matrix, n, j);

        for (int i = j + 1; i < n; i++) {
            double factor = matrix[i][j] / matrix[j][j];
            for (int k = j; k < n + 1; k++) {
                matrix[i][k] -= factor * matrix[j][k];
            }
        }
    }
}

void backSubstitution(double **matrix, int n, double *solution) {
    for (int i = n - 1; i >= 0; i--) {
        solution[i] = matrix[i][n]; 
        for (int j = i + 1; j < n; j++) {
            solution[i] -= matrix[i][j] * solution[j];
        }
    }
}

int main(int argc, char**argv){
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return EXIT_FAILURE;
    }

    FILE* fp = fopen(argv[1], "r");
    if(fp  == NULL){
        fprintf(stderr, "Error opening file\n");
        return EXIT_FAILURE;
    }

    int n = 0;
    char line[1024]; 

    while (fgets(line, sizeof(line), fp)) {
        if (line[0] != '\n') {
            n++;
        }
    }

    rewind(fp); 

    double **matrix = (double **)malloc(n*sizeof(double*));
    if(matrix == NULL){
        fprintf(stderr, "Memory allocation failed\n");
        fclose(fp);
        return EXIT_FAILURE;
    }
    for (int i=0; i < n; i++){
        matrix[i] = (double *)malloc((n+1)*sizeof(double));
        if(matrix[i] == NULL){
            fprintf(stderr, "Memory allocation failed\n");
            for(int j=0; j<i; j++){
                free(matrix[j]);
            }
            free(matrix);
            fclose(fp);
            return EXIT_FAILURE;
        }
    }

    for(int i=0; i < n; i++){
        for(int j=0; j < n+1; j++){
            if(fscanf(fp, "%lf", &matrix[i][j]) != 1){
                fprintf(stderr, "Error reading file\n");
                for(int k=0; k<n; k++){
                    free(matrix[k]);
                }
                free(matrix);
                fclose(fp);
                return EXIT_FAILURE;
            }
        }
    }

    gaussianElimination(matrix, n);

    double *solution = (double *)malloc(n * sizeof(double));
    backSubstitution(matrix, n, solution);

    for (int i = 0; i < n; i++) {
        printf("x%d = %f\n", i + 1, solution[i]);
    }

    for (int i = 0; i < n; i++) {
        free(matrix[i]);
    }
    free(matrix);
    free(solution);

    fclose(fp); 

    return 0;
}

The actual question is as follows:

Take an augmented matrix of [N][N+1] numerical values from a space separated file and solve using gaussian elimination and print the N values of x.

I was not able to find any errors in the code , when I tried a sample file which had (3 4 5) (1 2 1) as the values I was getting the solution as -7.666667 and 0.666667 instead of 3 and -1.
I tried few others too but getting way different solution from the actual one.



You need to sign in to view this answers

Exit mobile version