Enter Elements of Matrix
Enter Elements of Matrix
Write a program to declare a matrix A[ ] [ ] of order (M xN) 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:
(a) Display the original matrix.
(b) Sort each row of the matrix in ascending order using any standard sorting technique.
(c) 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. 8. 13
Example 2:
INPUT:
M = 11
N=5
OUTPUT:
MATRIX SIZE OUT OF RANGE
Algorithm
1.Start
2.Create a class
3.Declare the required variables
4.input the numbers in an array
5.Apply bubble sort technique
6.Swapping
7.Displaying the parameters
8.End.
Programming code
import java.util.*;
public class TwoDArr
{
void sort(int a[]) //Taking single dimension array as parameter
{
int i, j, n = a.length, tmp; //a.length gives the number of values in the 1D array
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++) //Sorting using bubble sort technique
{
if(a[j]>a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}