Oct 3, 2011

C Program to sort the matrix rows and columns.

C Program to sort the matrix rows and columns. This C program accept a order MxN Matrix, and sort all rows of the matrix in ascending order and all columns in descending order . In this program, we use the for statement to read two dimension arrays. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/


#include <stdio.h>

void main ()
{
 static int ma[10][10],mb[10][10];
 int i,j,k,a,m,n;

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

 printf ("Enter co-efficients of the matrix \n");
 for (i=0;i<m;++i)
 {
  for (j=0;j<n;++j)
  {
   scanf ("%d",&ma[i][j]);
   mb[i][j] = ma[i][j];
  }
 }
 printf ("The given matrix is \n");
 for (i=0;i<m;++i)
 {
  for (j=0;j<n;++j)
  {
   printf (" %d",ma[i][j]);
  }
  printf ("\n");
 }

 printf ("After arranging rows in ascending order\n");
 for (i=0;i<m;++i)
 {
  for (j=0;j<n;++j)
  {
   for (k=(j+1);k<n;++k)
   {
    if (ma[i][j] > ma[i][k])
    {
     a = ma[i][j];
     ma[i][j] = ma[i][k];
     ma[i][k] = a;
    }
   }
  }
 }      /* End of outer for loop*/

 for (i=0;i<m;++i)
 {
  for (j=0;j<n;++j)
  {
   printf (" %d",ma[i][j]);
  }
  printf ("\n");
 }

 printf ("After arranging the columns in descending order \n");
 for (j=0;j<n;++j)
 {
  for (i=0;i<m;++i)
  {
   for (k=i+1;k<m;++k)
   {
    if (mb[i][j] < mb[k][j])
    {
     a = mb[i][j];
     mb[i][j] = mb[k][j];
     mb[k][j] = a;
    }
   }
  }
 }       /* End of outer for loop*/

 for (i=0;i<m;++i)
 {
  for (j=0;j<n;++j)
  {
   printf (" %d",mb[i][j]);
  }
  printf ("\n");
 }

}  /*End of main() */
Read more Similar C Programs
C Basic.

C Matrix.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!