This is a homework, so I will not ask for the full code, I need a part of it. I need a working code for an algorithm, that checks if an even number follows an odd number in an array and continues to do that until all elements of the array are checked.
If element #1 is an odd number then x1 is 1, if it's an even number x1 is 2.
If element #2 is an odd number then x2 is 1, if it's an even number x2 is 2.
If element #1 is an odd number and element #2 is an even number, then break.
Then it changes up the indexes of the array, so that in the next step elements #2 and #3 are checked.
I have no knowledge of boolean usage in programming, so, if explaining using that, please, be basic.
You're always checking the last value in the array and the one after that (obviously there is none, so you're accessing invalid memory). When those match the criteria, you're stopping the loop.
1 2 3 4 5 6 7 8
for (int i=1; i<n; i++)
{
if (array[i-1]%2!=0 && array[i]%2==0)
{
//...
i++; //optional, but we can skip the next iteration, since we already know the first value won't be odd
}
}