0% found this document useful (0 votes)
11 views

Program 22

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
11 views

Program 22

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
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/ 9

Program 22

Write a program to declare a matrix a[ ] [] of order (M x m) where ‘M’ is the


number of rows and the number of columns such that M must be greater than
2 and less than 20. Allow the user to input integers into the matrix.
(a) Display the input matrix

(b) Find the maximum and minimum value in the matrix and display them.

(c) Sort the elements of the matrix in ascending order using any standard
sorting technique and rearrange them in the matrix.

(d) Output the rearranged matrix.


Test your program with the sample data and some random data :
Example 1
Input :M=3
N=4
8 7 9 3
-2 0 4 5
1 3 6 -4
Output : Original Matrix
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest number :9
Row =0
Column =2
Smallest number : -4
Row =2
Column =3
Rearranged matrix
-4 -2 0 1
3 3 4 5
6 7 8 9
Example 2
Input :M=3
N = 22
Output : Size out of Range
Method 1
import java.util.*;
class Sorting
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,k=0,big,small,pos,temp,row=0,col=0,row1=0,col1=0;;
System.out.print(“Enter the M :”);
int M=sc.nextInt();
int x[][]=new int[M][M];
int y[][]=new int[M][M];
int size=M*M;
int a[]=new int[size];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(“Enter any number:”);
x[i][j]=sc.nextInt();
a[k++]=x[i][j];
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(“Original matrix”);
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(x[i][j]+”\t”);
}
System.out.println();
}
big=x[0][0];
small=x[0][0];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(x[i][j]>big)
{
big=x[i][j];
row=i;
col=j;
}
if(x[i][j]<small)
{
small=x[i][j];
row1=i;
col1=j;
}
}
}
System.out.println(“Largest number”+big);
System.out.println(“Row=”+row);
System.out.println(“Column=”+col);
System.out.println(“Smallest number”+small);
System.out.println(“Row=”+row1);
System.out.println(“Column=”+col1);
System.out.println(“Matrix after sorting”);
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
y[i][j]=a[k++];
System.out.print(y[i][j]+”\t”);
}
System.out.println();
}
}
}

Method 2
import java.util.*;
class Sorting
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,k=0,big,small,pos,temp,row=0,col=0,row1=0,col1=0;;
System.out.print("Enter the M :");
int M=sc.nextInt();
if(M>2 && M<20)
{
int x[][]=new int[M][M];
int y[][]=new int[M][M];
int size=M*M;
int a[]=new int[size];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print("Enter any number:");
x[i][j]=sc.nextInt();
a[k++]=x[i][j];
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Original matrix");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(x[i][j]+"\t");
}
System.out.println();
}
big=x[0][0];
small=x[0][0];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(x[i][j]>big)
{
big=x[i][j];
row=i;
col=j;
}
if(x[i][j]<small)
{
small=x[i][j];
row1=i;
col1=j;
}
}
}
System.out.println("Largest number"+big);
System.out.println("Row="+row);
System.out.println("Column="+col);
System.out.println("Smallest number"+small);
System.out.println("Row="+row1);
System.out.println("Column="+col1);
System.out.println("Matrix after sorting");
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
y[i][j]=a[k++];
System.out.print(y[i][j]+"\t");
}
System.out.println();
}
}
else
{
System.out.println("Size out of Range");
}
}
}

You might also like