Untitled

 avatar
unknown
plain_text
a year ago
1.5 kB
5
Indexable
#include <stdio.h>
#include <conio.h>

int main() {
    clrscr();
    int n, i, j, k;
    float a[10][10], x[10];
    
    printf("Enter the no. of equations: ");
    scanf("%d", &n);
    
    printf("Enter the elements of the augmented-matrix row-wise:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j <= n; j++) {
            scanf("%f", &a[i][j]);
        }
    }
    
    printf("\nThe entered matrix is:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j <= n; j++) {
            printf("%.2f\t", a[i][j]);
        }
        printf("\n");
    }
    
    for (i = 0; i < n - 1; i++) {
        for (k = i + 1; k < n; k++) {
            double t = a[k][i] / a[i][i];
            for (j = 0; j <= n; j++) {
                a[k][j] = a[k][j] - t * a[i][j];
            }
        }
    }
    
    printf("\n\nThe matrix after gauss-elimination is as follows:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j <= n; j++) {
            printf("%.2f\t", a[i][j]);
        }
        printf("\n");
    }
    
    for (i = n - 1; i >= 0; i--) {
        x[i] = a[i][n];
        for (j = i + 1; j < n; j++) {
            if (j != i)
                x[i] = x[i] - a[i][j] * x[j];
        }
        x[i] = x[i] / a[i][i];
    }
    
    printf("\nThe values of the variables are as follows:\n");
    for (i = 0; i < n; i++)
        printf("%.2f\n", x[i]);

    getch();
    return 0;
}
Editor is loading...
Leave a Comment