Echelon form of a Matrix using Gauss-Elimination [C Programming]

452 Views Asked by At

I'm trying to reduce a matrix to an Echelon form.. here's my attempt so far. It doesnt work when the elements of 2 rows are in proportion and I get a zero row before the code is done executing..

Also Since I used the diagonal elements as a reference, my code fails if its a lets say 2 x 4 Matrix :( please help me to find a suitable solution for the problem.


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

int main() {
    int m,n;

    int i,j,k;
    float a[10][10];

    printf("Enter the order of Matrix: ");
    scanf("%d%d",&m,&n);

    printf("\n Enter the elements of Matrix : ");
    for(i=0;i<m;i++){
        for(j=0;j<n;j++) {
            scanf("%f",&a[i][j]);
        }
    }

    for(i=0;i<m;i++) {
        if((a[i][i])!=1) {
            float temp=a[i][i];
            if(temp==0) {
                break; // break or continue ??
            }
            for(j=0;j<n;j++) {
                a[i][j]=((a[i][j])/temp);
            }
        }

        for(k=i+1;k<m;k++) {
            float p=a[k][i];
            for(j=i;j<3;j++) {
                a[k][j]=a[k][j]-(a[i][j]*p);
                }
        }

    }

     for(i=0;i<m;i++) {
        for(j=0;j<n;j++) {
            printf("%.2f\t",a[i][j]);
        }
        printf("\n");
    }
    int flag,fl=0;
    for(i=0;i<m;i++) {
            flag=0;
        for(j=0;j<n;j++) {
            if(a[i][j]==0) {
                flag++;
            }
        if(flag==3)
            fl++;
        }
    }


    printf("\n\n\n Rank of your Matrix is: %d \n",(n-fl));

    return 0;
} ```
0

There are 0 best solutions below