We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Ques: - Write a program to input a 2d matrix of size m by n first Arrange row in
ascending order. Then arrange column in ascending order.
Example: - ENTER THE VALUE OF M: 3 ENTER THE VALUE OF N: 3 ENTER ELEMENTS OF MATRIX: ENTER ELEMENTS OF ROW 1: 1,2,3 ENTER ELEMENTS OF ROW 2: 4,5,6 ENTER ELEMENTS OF ROW 3: 7,8,9 ORIGINAL MATRIX 123 456 789 MATRIX AFTER SORTING ROWS 123 456 789 MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN DESCENDING 789 456 123 ALGORITHM 1. Start 2. Prompt the user to enter the values of M and N 3. Read the values of M and N 4. Create a 2D array 'a' of size MxN 5. Prompt the user to enter the elements of the matrix 6. Read the elements of the matrix into the array 'a' 7. Display "Original Matrix" 8. Loop through each row 'i' from 0 to M-1 a. Loop through each column 'j' from 0 to N-1 i. Print the value of a[i][j] b. Print a new line 9. Sort each row of the matrix 'a' in ascending order 10. Display "Matrix After Sorting Rows" 11. Loop through each row 'i' from 0 to M-1 a. Loop through each column 'j' from 0 to N-1 i. Print the value of a[i][j] b. Print a new line 12. Sort each column of the matrix 'a' in descending order 13. Display "Matrix After Sorting Rows in Ascending and Columns in Descending" 14. Loop through each row 'i' from 0 to M-1 a. Loop through each column 'j' from 0 to N-1 i. Print the value of a[i][j] b. Print a new line 15. End JAVA PROGRAM import java.util.Scanner;
public class ArraySort
{ public static void main() { 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(); 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(); }
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 1; j++) { for (int k = 0; k < m - j - 1; k++) { if (a[k][i] < a[k + 1][i]) { int temp = a[k][i]; a[k][i] = a[k + 1][i]; a[k + 1][i] = temp; } } } }
System.out.println("MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN
DESCENDING"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } }
Write a program to declare a Square Matrixx, a [][] order m*n where m is the number of rows and n is the number of columns such that m and n both must be greater than 2 and less than 10 . Accept the value of ‘m’ and ‘n’ as user input and perform the following task. Display the original Matrixx. Sort each column of the matrix in ascending order using selection sort technique. Play the changed Matrixx after sorting each row.
Write a program to declare a Square, Matrixx a[][] of order m*n where ‘m’ is the number of rows and ‘n’ is the number of columns, such that m and n both must be greater than 2 and less than 10 . accept the value of m and n as user input. Allow the user to input the integers into Matrixx and perform the following task.