Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the definition of a function named isSorted that receives three arguments

ID: 3546632 • Letter: W

Question

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 boolargument  is true  then the function returns true  if and only if the array  is sorted in ascending order. If the boolargument  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 .

Explanation / Answer

bool isSorted (int *arr,int num,bool b){
    bool temp=true;
    if(b){
        for(int i=0;i<num-1;i++){
            if(arr[i]>arr[i+1]){
                temp=false;
                break;
            }
        }
    }else{
        for(int i=0;i<num-1;i++){
            if(arr[i]<arr[i+1]){
                temp=false;
                break;
            }
        }
    }
    return temp;
}