Contact Learn C
Copy

Program 138: Arrange Rows and Columns of Matrix in Ascending order

Program 138:

#include<stdio.h>
main()
{ 
 int i,j,rows,col,k,temp;
 printf("Enter number of rows and columns of a matrix\n");
 scanf("%d %d",&rows,&col);
int a[rows][col],dummy[rows][col];

  //Taking input of matrix
    printf("Enter Matrix 1\n");
    for(i=0;i<rows;i++)
    {
       for(j=0;j<col;j++)
      {
     scanf("%d",&a[i][j]);
      }
    }
    
    printf("Given /matrix is\n");
        for(i=0;i<rows;i++)
         {
         for(j=0;j<col;j++)
         {
           printf("%d\t",a[i][j]);
           dummy[i][j]=a[i][j];//copying of array into another for temporary storage
           }
  
           printf("\n");
           }
 
   //Ascending order for rows
 for(i=0;i<rows;i++)
 {
    for(j=0;j<col;j++)
    {
     for(k=j+1;k<col;k++)
     {
      if(a[i][j]>a[i][k])
      {
       temp=a[i][j];
       a[i][j]=a[i][k];
       a[i][k]=temp;
      }
     }
    }
 }  
 
 printf("After arranginf rows in ascending order\n");
     for(i=0;i<rows;i++)
         {
         for(j=0;j<col;j++)
         {
           printf("%d\t",a[i][j]);
           }
  
           printf("\n");
           }
  //For arranging columns in ascending order
  
   for(j=0;j<col;j++)
 {
    for(i=0;i<rows;i++)
    {
     for(k=i+1;k<rows;k++)
     {
      if(dummy[i][j]>dummy[k][j])
      {
       temp=dummy[i][j];
       dummy[i][j]=dummy[k][j];
       dummy[k][j]=temp;
      }
     }
    }
 }
 
  printf("After arranginf columns in ascending order\n");
     for(i=0;i<rows;i++)
         {
         for(j=0;j<col;j++)
         {
           printf("%d\t",dummy[i][j]);
           }
  
           printf("\n");
           }  

}
Explanation:

//Coming Soon...

Output:

Arrange Rows and Columns of Matrix in Ascending order



 
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide