Hi,please help me!

I wanna write a program for gauss jordon elimination ,for that I need to sort/exchange the rows such that the rows are in order of their increasing
zero elements before non zero element in each row like below

suppose,
           input    0 0 3 4 5
                    0 3 2 1 6
                    0 0 0 2 0
                    6 2 0 3 1

output should be    6 2 0 3 1
                    0 3 2 1 6
                    0 0 3 4 5
                    0 0 0 2 0

I have tried this, but is not working properly,please suggest/correct .

#include<stdio.h>

main(){
       int i,j,k,l,m,n,o,p,q,r,s,t,u,len,count=0;
       printf("Enter no.of unknowns:");
       scanf("%d",&m);
       printf("Enter no.of equations:");
       scanf("%d",&n);
       float a[m][n+1],v;  //I took n+1 because columns=rows+1,which includes value column of equations of gauss jordon
       int b[m];
       for(i=0;i<m;i++){                         //reading elements
                        for(j=0;j<(n+1);j++){
                                             printf(" ",i+1,j+1);
                                             scanf("%f",&a[i][j]);
                                             }
                                             printf("\n");
                                             }

       for(i=0;i<m;i++){                         //counting no.of zeros
                        count=0;
                        for(j=0;(a[i][j]==0)&&(j<(n+1));j++){

                                     }
                         b[i]=j;
                         printf("%d ",b[i]);  
                                 }  
           putchar('\n');
for(k=0;k<m;k++)                //sorting starts here
      for(i=0;i<(m);i++){
                     if(b[i]>b[i+1]){  
                       for(j=0;j<n+1;j++){
                                        v=a[i+1][j];
                                        a[i+1][j]=a[i][j];
                                        a[i][j]=v;
                                        }
                                        }
                                        }
       for(i=0;i<m;i++){                  //printing
                        for(j=0;j<(n+1);j++){
                                             printf("%.2f  ",a[i][j]);
                                             }
                                             printf("\n");
                                             }                                      



       getch();
       }

THANKS IN ADVANCE!

Dani AI

Generated

A reliable way to get the ordering shown is to compute, for each row, the index of its first non‑zero coefficient (look only at the coefficient columns, not the augmented column) and then reorder rows by that index (rows with no non‑zero coefficients get an index of n so they fall last). This is different from numerical pivoting: sorting by leading index is a structural reordering; for stable numeric elimination prefer partial pivoting (select the row with largest absolute value in the pivot column).

Problems seen in the thread: ’s zero‑count loop checks the array element before checking the bound and will run past the last column; the bubble sort compares b[i] with b[i+1] while allowing i==m-1 (out of bounds); the code never keeps the b[] array in sync with row swaps; and the augmented column should not be used when finding the first coefficient. ’s idea of treating columns as rows is creative, but the memcpy calls must use byte counts (cols * sizeof element) and the nested loops must avoid r+1 when r is last.

A simple, robust approach is: (1) compute lead[i] = first column c in [0..n-1] with |a[i][c]|>EPS, or lead[i]=n if none; (2) stable sort rows by lead[] (bubble sort is fine for small matrices) while swapping the entire row and the corresponding lead[] entry; (3) continue to Gauss–Jordan with proper pivot selection if numeric stability is required.

Example C technique (swap row pointers, keep lead[] in sync):

/* allocate float *data = malloc(m*(n+1)*sizeof(float));
   float **A = malloc(m*sizeof(float*)); for(i) A[i]=data + i*(n+1);
   read matrix into A[i][j] */
for(i=0;i<m;i++){
  lead[i]=n;
  for(j=0;j<n;j++) if(fabsf(A[i][j])>1e-9f){ lead[i]=j; break; }
}
for(pass=0; pass<m-1; ++pass)
  for(i=0;i<m-1-pass; ++i)
    if(lead[i] > lead[i+1]){
      float *t = A[i]; A[i]=A[i+1]; A[i+1]=t;
      int tmp = lead[i]; lead[i]=lead[i+1]; lead[i+1]=tmp;
    }

Note: if the goal is full numeric Gauss–Jordan, replace the static sort with column‑by‑column partial pivoting (pick the row with maximum |a[row][col]| and swap it into the pivot position) for correct and stable elimination.

Turn it over 90 degrees, and see the columns as rows, and the rows as column. memcpy helps a lot. ;)

#include <stdio.h>
#define R 4
#define C 5

int main() {
  int i,j, r, c,n; 
  int grid[R][C]={
  {0,0,3,4,5},
  {0,3,2,1,6},
  {0,0,0,2,0},
  {6,2,0,3,1},
  };
  int temp[R];

  printf("\n\n\n");
  for(r=0;r<R;r++) {
    for(c=0;c<C;c++) {
      printf("%d ", grid[r][c]);
    }
    putchar('\n');
  }
  //start sorting
  printf("\n\n\n");
  for(c=0;c<C;c++) {
    for(r=0;r<R;r++) {
      if(grid[r][c] < grid[r+1][c]) {
        memcpy(temp, grid[r], C);
        memcpy(grid[r], grid[r+1], C);
        memcpy(grid[r+1], temp, C);

      }
    }
  }

  for(r=0;r<R;r++) {
    for(c=0;c<C;c++) {
      printf("%d ", grid[r][c]);
    }
    putchar('\n');
  }
  

  printf("\n\n\t\t\t     press enter when ready");

  (void) getchar(); 
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.