write a function issorted() that accepts an array of real numbers arr and an integer n as arguments, where n is the size of arr. The function properly reads n integers into the array arr,then finds and returns whether the items of the array are sorted in ascending order.

Answer :

Answer:

Here is code in java.

import java.util.*;

class Main

{

//main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

       int m;

        // scanner object to read input from user

       Scanner ss=new Scanner(System.in);

       System.out.println(" size  of the array:");

       //read the size of the array

        m=ss.nextInt();

        // create array of the given size

        float arr[]=new float[m];

        /* call function to check whether input are in the ascending sorted order or not */

        Boolean res= issorted(arr,m);

       

      // print the result

       System.out.println("array in the ascending sorted order  : "+res);

     

       

   }catch(Exception ex){

       return;}

}

 // function to check whether input are in the ascending sorted order or not

public static boolean issorted(float[] arr,int n)

{

    int i,j;

    float d;

    Boolean flag=true;

    Scanner ss=new Scanner(System.in);

    System.out.println("enter the elements of the array:");

     // read elements of the array

    for(i=0;i<n;i++)

    {

        arr[i]=ss.nextFloat();

    }

   

    // check elements re in the ascending order or not

    for(j=0;j<n-1;j++)

    {

        if(arr[j]>arr[j+1])

        {

            flag=false;

            break;

        }

    }

     // return the result

    return flag;

}

}

Explanation:

With the help of scanner class object, read the size "m" of array.Create an array of  the given size. Pass array and integer into the function issorted(). Read the elements of the array and then check elements are in the ascending order or not.It will return "true" if elements are in the sorted order else it will return "false".

Output:

enter  size  of the array:                                                                                                                                    

5                                                                                                                                                              

enter the elements of the array:                                                                                                                              

1 2 3 4 7                                                                                                                                                      

array in the ascending sorted order  : true                                                                                                                              

enter  size  of the array:                                                  

5                                                                            

enter the elements of the array:                                              

2 4 9 3 5                                                                  

array in the ascending sorted order  : false

Other Questions