Computer Science
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 ROWS
-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 ROWS
5 19 22
7 12 36
6 9 13
Example 3
INPUT:
M = 11
N = 5
OUTPUT:
MATRIX SIZE OUT OF RANGE.
Java
Java Arrays
ICSE Prac 2018
33 Likes
Answer
import java.util.Scanner;
public class ArraySort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF M: ");
int m = in.nextInt();
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
if (m <= 2
|| m >= 10
|| n <= 2
|| n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE.");
return;
}
int a[][] = new int[m][n];
System.out.println("ENTER ELEMENTS OF MATRIX:");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS OF ROW " + (i+1) + ":");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - j - 1; k++) {
if (a[i][k] > a[i][k + 1]) {
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
}
}
}
}
System.out.println("MATRIX AFTER SORTING ROWS");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}Output
![BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE. BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE.](https://cdn1.knowledgeboat.com/img/abp12/1/2018-40-p2-1.jpg)
![BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE. BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE.](https://cdn1.knowledgeboat.com/img/abp12/1/2018-40-p2-2.jpg)
![BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE. BlueJ output of 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: (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 ROWS -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 ROWS 5 19 22 7 12 36 6 9 13 Example 3 INPUT: M = 11 N = 5 OUTPUT: MATRIX SIZE OUT OF RANGE.](https://cdn1.knowledgeboat.com/img/abp12/1/2018-40-p2-3.jpg)
Answered By
13 Likes
Related Questions
Consider the following two-dimensional array and answer the questions given below:
int x[ ][ ] = {{4,3,2}, {7,8,2}, {8, 3,10}, {1, 2, 9}};(a) What is the order of the array?
(b) What is the value of x[0][0]+x[2][2]?
Write a Java program to store n numbers in an one dimensional array. Pass this array to a function number(int a[]). Display only those numbers whose sum of digit is prime.
Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?