Question

# Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:Display the original matrix.Sort each row of the matrix in ascending order using any standard sorting technique.Display the changed matrix after sorting each row.

```				```
Test your program for the following data and some random data:
Example 1:
INPUT:
m = 4
n = 3
Enter elements of matrix:
11  -2  3
5   16  7
9   0   4
3   1   8

OUTPUT:

ORIGINAL MATRIX:

11  -2  3
5   16  7
9   0   4
3   1   8

MATRIX AFTER SORTING:

-2  3  11
5   7  16
0   4   9
1   3   8
Example 2:
INPUT:
m = 3
n = 3
Enter elements of matrix:

22  5   19
7   36  12
9   13  6

OUTPUT:

ORIGINAL MATRIX:

22  5   19
7   36  12
9   13  6

MATRIX AFTER SORTING:

5   19  22
7   12  36
6   9   13

INPUT:
m = 11
n = 5
OUTPUT:
Matrix size out of range.
```
```

# Share code with your friends

Code

```				```
import java.util.Scanner;
public class SortMatrix
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m = 0,n=0,i=0,j=0,k=0,temp=0;
int a[][];

System.out.print("M = ");
m = sc.nextInt();
System.out.print("N = ");
n = sc.nextInt();
if(m <= 2 || m >= 10 || n <= 2 || n >= 10)
{
System.out.println("Matrix size out of range.");
return;
}
a = new int[m][n];

System.out.println("Enter elements of matrix:");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}

System.out.println("Original Matrix:");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
/*sorting  array by row */
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
for(k=0;k< n;k++)
{
if(a[i][j] < a[i][k])
{
temp=a[i][j];
a[i][j] =a[i][k];
a[i][k]=temp;
}
}
}
}

System.out.println("Matrix after sorting rows:");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");

}
System.out.println();
}
}
}

```
```

Sale

50
Sale

150
Sale

200
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50