Contact Learn C
Copy

Program 141:Arrange Rows and Columns in Ascending order (Method II)

Program 141:

#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[50][50],dummy2[50][50];

  //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 arranging 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 we can do transpose and apply above logic and again transpose it to get final output
  //First transpose the original matrix stored in dummy and stored in dummy2
   temp=rows;
 rows=col;
 col=temp;
 for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   dummy2[i][j]=dummy[j][i];
  }
 }
}
  //Applying the same logic as above for dummy2
 for(i=0;i<rows;i++)
 {
    for(j=0;j<col;j++)
    {
     for(k=j+1;k<col;k++)
     {
      if(dummy2[i][j]>dummy2[i][k])
      {
       temp=dummy2[i][j];
       dummy2[i][j]=dummy2[i][k];
       dummy2[i][k]=temp;
      }
     }
    }
 }  
 //Again transpose of above matrix and store in dummy
  temp=rows;
 rows=col;
 col=temp;
   for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   dummy[i][j]=dummy2[j][i];
  }
 }
  printf("After arranging 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 in Ascending order (Method II)




 
Donate

Download App and Learn when ever you want

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