

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to sort all columns and rows of matrix
Problem
Write a code to sort all the rows of matrix in an ascending order and all columns in the descending order. The size of matrix and elements of matrix are given by user at runtime.
Solution
The solution to sort all the rows of matrix in an ascending order and all columns in the descending order in the C programming language is explained below −
The logic used to sort the rows in an ascending order is as follows −
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; } } } }
The logic used to sort the columns in the descending order is as follows −
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; } } } }
Program
Following is the C program to sort all the rows of matrix in an ascending order and all columns in the descending order −
#include <stdio.h> void main(){ int i,j,k,a,m,n; static int ma[10][10],mb[10][10]; 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; } } } } 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; } } } } for (i=0;i<m;++i){ for (j=0;j<n;++j){ printf (" %d",mb[i][j]); } printf ("\n"); } }
Output
When the above program is executed, it produces the following result −
Enter the order of the matrix 3 4 Enter co-efficient of the matrix 1 2 3 4 1 2 3 4 5 1 2 3 The given matrix is 1 2 3 4 1 2 3 4 5 1 2 3 After arranging rows in ascending order 1 2 3 4 1 2 3 4 1 2 3 5 After arranging the columns in descending order 5 2 3 4 1 2 3 4 1 1 2 3
- Related Questions & Answers
- Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- Python program to sort matrix based upon sum of rows
- Python Program to sort rows of a matrix by custom element count
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- How to shuffle columns or rows of matrix in PyTorch?
- Find distinct elements common to all rows of a Matrix in C++
- How to find the sum of rows and columns of a given matrix using Numpy?
- How to divide matrix rows by number of columns in R?
- Count all sorted rows in a matrix in C++
- Finding the number of rows and columns in a given matrix using Numpy
- How to find the sum of rows, columns, and total in a matrix in R?
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- How to create a subset of rows or columns of a matrix in R?
- Find distinct elements common to all rows of a matrix in Python
Advertisements