0

seeing which number in an array is larger

Normally I could sort arrays easily, but today it's giving me some weird results with the location number with i. Yet it works when using numbers rather than symbols. #include <iostream> using namespace std; int main() { int i = 0; int size = 0; int list[10]; for (i; i<10; i++) list[i] = i; for (i=0; i < 10; i++) { if (list[i] > list[i+1]) { cout << list[i] << " " << list[i + 1] << endl; cout << "The array is not sorted" << endl; break; } else if (i != 9) continue; else cout << "The array is sorted" << endl; } for (int i = 0; i < 10; i++) { cout << list[i+1] << endl; } // cout << "The array is sorted" << endl; system("pause"); return 0; } It should be checking the first number with the second, but it's checking the last number and a random 3. If you flip the greater than symbol to lesser than, you get the right numbers. I have no idea why this is happening today. I have done this before, but the compiler is making up it's own rules. I'm using a sort function from the algorithm library, but I am not including it in this question.

1st Apr 2018, 6:52 AM
Tyler Riola
1 Respuesta
+ 1
your code seems to check the array that is sorted or not your array has 10 elements and in c++ your array indexes start from 0 to 9 so the line else if (i!=9) must be else if (i!=8) and in 3rd for in your code cout << list[i+1] must be cout << list[i]
1st Apr 2018, 7:26 AM
🇮🇷 M N