0% found this document useful (0 votes)
42 views8 pages

Enter Elements of Matrix

The document describes a program that allows a user to input values into a two-dimensional matrix. It then sorts each row of the matrix in ascending order using a bubble sort algorithm. The program displays the original matrix, sorts each row, and displays the sorted matrix. It includes sample input/output and describes declaring variables, inputting values, sorting rows, and displaying the results.

Uploaded by

A.K. Y.
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)
42 views8 pages

Enter Elements of Matrix

The document describes a program that allows a user to input values into a two-dimensional matrix. It then sorts each row of the matrix in ascending order using a bubble sort algorithm. The program displays the original matrix, sorts each row, and displays the sorted matrix. It includes sample input/output and describes declaring variables, inputting values, sorting rows, and displaying the results.

Uploaded by

A.K. Y.
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/ 8

QUESTION 26:

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;
}
}
}
}

void display(int a[][]) //Taking 2D array as parameter


{
int i, j;
for(i=0;i<a.length;i++) //a.length here gives the number of rows
{
for(j=0;j<a[i].length;j++) //a[i] gives one row at a time. a[i].length gives the
number of values in each row
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

void sort2D(int a[][])


{ //As arrays are reference data types any changes made to the formal parameter
in this function.
//Will be reflected in the original parameter.
int i, j;
for(i=0;i<a.length;i++)
{
sort(a[i]); //Taking each row and passing each to sort function as a single
dimension array
}
}

public static void main()


{
Scanner sc = new Scanner(System.in);
TwoDArr obj = new TwoDArr();
int a[][], m, n, i, j;
System.out.println("Enter the number of rows: ");
m = sc.nextInt(); //Input the size
System.out.println("Enter the number of columns: ");
n = sc.nextInt();
if(m<3||m>9||n<3||n>9)
{
System.out.println("Matrix out of range");
System.exit(0); //Terminating the program
}
a = new int[m][n]; //Allocating memory
System.out.println("Enter the values for the matrix: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Original Matrix: ");
obj.display(a); //Displaying array before sorting
obj.sort2D(a); //Calling sort function
System.out.println("Matrix after sorting rows: ");
obj.display(a); //Display after sort function has executed
}
}
-----------------------------------*-----------------------------------
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
Variable Description:
Variable Data Type Description

A[] int Parameter single dimensional arrray


n int Length
i int Loop variable for row

j int Loop variable for column


A[][] int Double dimensional array
m int size

You might also like