Write the definition of a function named isSorted that receives three arguments: an array of int, an int that indicates the number of elements of interest in the array, and a bool. If the bool argument is true then the function returns true if and only if the array is sorted in ascending order. If the bool argument is false then the function returns true if and only if the array is sorted in descending order. In all other cases the function returns false. You may assume that the array has at least two elements.

LANGUAGE: C++

CHALLENGE:

Write the definition of a function named isSorted that receives three arguments: an array of int, an int that indicates the number of elements of interest in the array, and a bool.

If the bool argument is true then the function returns true if and only if the array is sorted in ascending order. If the bool argument is false then the function returns true if and only if the array is sorted in descending order. In all other cases the function returns false.

You may assume that the array has at least two elements.

SOLUTION:


bool isSorted(int a[], int n, bool status){
    bool flag = true;
    if(status == true){
        for (int i=0; i<n-1; i++)
            if (a[i] > a[i+1])
                flag = false;
    }else{
        for (int i=n-1; i > 0; i--)
            if (a[i] > a[i-1])
                flag = false;
    }
    return flag;
}