Hey, so this is embarrassing since this is an incredibly basic function I'm trying to write, but whenever I test it I don't get either true or false. I need the function to check whether an array is in order from smallest to highest int. Same values next to each other should still return true, which is why I have <=. I've included a test. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14
bool is_sorted(constint array[], int size) {
for (int i = 0; i < size; i++) {
if (array[i] <= array[i + 1]) {
returntrue;
}
}
returnfalse;
}
void test_is_sorted() {
constint array_a[5] = { 1, -2, 3, -4, 5 };
int size_1 = 5;
is_sorted(array_a, size_1);
}
whenever I test it I don't get either true or false
What do you mean by this exactly?
By looking at your tester function, I'd say your problem is that you assume that simply calling the function will print either "true" or "false". You'll have to store the result somewhere and print that, or print the returned value directly:
1 2 3 4 5 6
#include <iostream>
//...
bool result = is_sorted(array_a, size_1);
std::cout << std::boolalpha << result << std::endl;