Help me to understand a sorted array program
Please help me to understand this code. Correct me if I am wrong, does
SIZE - 1 mean the last element in the array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 4;
int arr[SIZE] = { 1,2,3,4 };
int i = 0;
while (i < (SIZE - 1) && arr[i] < arr[i + 1])
i++;
if (i == (SIZE - 1))
cout << "Sorted" << endl;
else
cout << "Not sorted" << endl;
}
|
Well what it is really doing is preventing you from accessing the array out of bounds on line 10 when you add 1 to i.
But yes it is also the last element in the array.
Topic archived. No new replies allowed.