So I got a problem Write a program in java to take an 2D array from user and display all elements if array in ascending order.
Here I took the input from user but am not able to check whether elements in 2d array are ascending or not
import java.util.Scanner;
class Q3{
public static void main(String[] args) {
int rows, columns;
Scanner input= new Scanner (System.in);
System.out.println("Enter size of 2D Array : ");
rows = input.nextInt();
columns = input.nextInt();
System.out.println("Enter array elements: ");
int [][] arr1 = new int [rows][columns];
for(int i=0; i<rows;i++)
{
for(int j=0;j<columns; j++)
{
System.out.print("arr1 ["+ i+"]["+j+"] = " );
arr1[i][j] = input.nextInt();
}
}
//Checking whether the array is of ascending order or not
// for(int i=0; i<rows;i++)
// {
// for(int j=0;j<columns; j++)
// {
// for(int k=0; k<rows;k++)
// {
// for(int l=0 ;l<columns; l++)
// {
// if(arr1[i][j] < arr1[k][l])
// {
// break;
// }
// }
// }
// }
// }
}
}
Here to check for ascending order what should I do ?? I have tried a couple of things but didn't work