Coderstuck404
Coderstuck404

Reputation: 11

Check whether elements in 2d array is ascending or not in java

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

Upvotes: 1

Views: 295

Answers (1)

user8234870
user8234870

Reputation:

Just two for loops are enough to check weather the 2d array is in ascending order or not.

import  java.util.Scanner;
class Q3{
    public static void main(String[] args) {
        int rows, columns;
        var out = System.out;

        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 in ascending order or not
        boolean ascendingOrder = true;
        for(int i=0;i<rows && ascendingOrder;i++){
            for(int j=0;j<columns-1 && ascendingOrder;j++)
                if(arr1[i][j] > arr1[i][j+1])
                    ascendingOrder = false;
        }
        //displaying the 2d array is its in ascendingOrder
        if(ascendingOrder){
            for(int i=0; i<rows;i++){
                for(int j=0;j<columns; j++){
                    out.print(arr1[i][j]+" ");
                }
                out.println();
            }
        }

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

output:

$ java Q3 
Enter size of 2D Array : 
2
2
Enter array elements: 
arr1 [0][0] = 1
arr1 [0][1] = 2
arr1 [1][0] = 3
arr1 [1][1] = 4
1 2 
3 4 

$ java Q3 
Enter size of 2D Array : 
2
2
Enter array elements: 
arr1 [0][0] = 3
arr1 [0][1] = 2
arr1 [1][0] = 1  
arr1 [1][1] = 2

Upvotes: 1

Related Questions