Write a function named out_of_order that takes in an array of doubles and tests for the condition a[0] <= a[1] <= a[2] <= ...
The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order.
Don't forget to also:
- Explain what you do to avoid out of bounds array access.
- Create a driver program that test your function.
-Write the code in C++
CODE IN C++:
#include <iostream>
using namespace std;
int out_of_order(double arr[]){
int len =*(&arr + 1) - arr - 1;
double curVal = arr[0];
int i ;
for(i = 1 ; i < len ; i++){
if(arr[i] >= curVal){
curVal = arr[i];
}
else{
return i ;
}
}
return -1 ;
}
int main()
{
int n ;
cout << "Enter the size of the array : ";
cin >> n ;
double arr[n];
cout << "Enter the values of the array : ";
for(int i = 0 ; i < n ; i++){
cin >> arr[i] ;
}
int ind = out_of_order(arr);
if(ind == -1)
cout << "the elements are not out of order" << endl
;
else
cout << "the elements are out of order at the index "
<< ind << endl ;
return 0;
}
OUTPUT:


Write a function named out_of_order that takes in an array of doubles and tests for the...