Write an algorithm for a method (say is Descending) that accepts an integer arrays (say A) as input parameter, and returns a boolean value true if the elements in the array are in descending order. Example 1: Formal Parameters: A = { 1, 2 , 3, 4 } Return : False Example 2: Give Formal Parameters: A = { 4, 3, 2, 1 } Return : True Example 3: Formal Parameters: A = { 4, 3 , 1, 2} Return : False

Respuesta :

Answer:

Check the explanation

Explanation:

Algorithm for determining if the array passed as argument is in descending order or not.

1. Start

2. A = {1, 2, 3, 4};

3. Call function by passing array and it's size.

   isDescending(A, A.length);

4. Loop from 1 to size - 1 (inclusive)

5.     check if element at (i-1)th index is greater than element at (i)th index or not

6.     if at any point the above expression is executed to true value then return false

7.     if the above if statement is not executed then loop will terminate it self

8. Now return true.

// Java Implementation:

public static boolean isDescending(int[] arr, int size) {

   for (int i = 1; i < size; i++) {

       if (arr[i-1] > arr[i]) {

           return false;

       }

   }

   return true;

}