Answer:
The method definition to this question can be given as:
Method Definition:
bool isSorted(int a[], int n, bool status) //function definition
{
//function body
bool flag = true; //define Bool(Boolean) variable.
if(status == true) //check condition.
{
for (int i=0; i<n-1; i++) //loop for search
{
if (a[i] > a[i+1]) //check array elements
{
flag = false; //change bool variable value.
}
}
}
else //else block
{
for (int i=n-1; i > 0; i--) //loop
{
if (a[i] > a[i-1]) //if block
{
flag = false; //change bool variable value.
}
}
}
return flag; //return value.
}
Explanation:
In the above method definition firstly we define the method that is isSorted(). This method returns true or false value because its return type is bool. In the method, we define the bool variable that is flag. Then we use the condition by using conditional statements. in the if block we check that if status equal to true then we use the for loop in the for loop we define variable i that is used for search array elements it will go to n-1 that is the total number of an array is decrement by 1. In this loop, we use the if condition that is if elements of a[i] is greater then a[i+1] flag will return false. In the else block we use the for loop for search array elements in this loop we use the if block that checks that elements of a[i] is greater then a[i-1]. if it is true it returns false. end of loop end of else block the function will return value.