0

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!

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Adak
0

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.