0% found this document useful (0 votes)
343 views3 pages

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.

a java code

Uploaded by

thanushree77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views3 pages

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.

a java code

Uploaded by

thanushree77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 18:

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.
Test your program for the following data and some random data:
Example 1:
Input:
m=4
n=3
Enter the element 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:
3 -2 3
5 0 4
9 1 7
11 16 8
Example 2:
INPUT:
m=11
n=5
OUTPUT:
Metric size out of range.

Program 18:Code:

import java .util.*;


class r18
{
public static void main()
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of rows: ");


int m = sc.nextInt();
System.out.print("Enter the number of columns: ");
int n = sc.nextInt();

if (m < 3 || m > 9 || n < 3 || n > 9)


{
System.out.println("Matrixx out of range"); //Check if size is Less than 10 and greater
than 2
System.exit(0);
}

int[][] a = new int[m][n];

System.out.println("Enter the elements of the matrix:");


for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}

System.out.println("Original Matrixx:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t"); //Printing the original Matrixx
}
System.out.println();
}

// Sort each column using selection sort


for (int j = 0; j < n; j++)
{
for (int i = 0; i < m - 1; i++)
{
int e= i; //The minimum element of each column.
for (int k = i + 1; k < m; k++) {
if (a[k][j] < a[e][j])
{
e= k;
}
}
// Swap
int temp = a[i][j];
a[i][j] = a[e][j];
a[e][j] = temp;
}
}

System.out.println("Matrix after sorting each column:");


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t"); //Printing this sorted Matrixx.
}
System.out.println();
}}}

You might also like