i am trying to write a function that tests whether or not all elements of a given array are ordered from least to greatest, i.e., a[0] <= a[1] <= a[2] ... is true.
i am testing this using a for loop with the condition a[i] > a[i + 1]. the function is supposed to return the index value of the element for which this condition is not true. otherwise, it's supposed to return -1.
when i run this function on test2[], it should return 5 because 4 is smaller than 5, and the index value of 4 in the test2[] array is 5. but i keep getting -1, which incorrectly indicates that the array's elements are ordered from least to greatest.
any advice on how to fix this? i've been trying for a long time.
thank you in advance for any insight. my program is pasted below.
#include <iostream>
using namespace std;
int outOfOrder(double a[], int size);
int main() {
double test2[] = {1, 2, 3, 4, 5, 4, 8};
int size = 7;
outOfOrder(test2, size);
return 0;
}
int outOfOrder(double a[], int size){
for (int i = 0; i < size - 1; i++){
if (a[i] > a[i + 1]){
cout << i + 1;
return (i + 1);
} else {
cout << -1;
return -1;
}
}
return 0;
}
test2[]tooutOfOrder. It only sendsdata.