Question

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

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++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Write a function named out_of_order that takes in an array of doubles and tests for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT