1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
void checkIndDecr(int *arr, const int &index);
int main()
{
int *arr = new int[20]{ 1, 3, 5, 7, 9, 8, 6, 4, 3, 2, 1, 12, 15, 4, 23, 3, 6, 87, 12, 0 };
for (int i = 0; i < 20; i++)
checkIndDecr(arr, i);
delete[] arr;
}
void checkIndDecr(int *arr, const int &index)
{
static bool increasing = false;
static int lastValue = INT_MIN;
//If we were previously increasing and the new value is less than the previous value
if (increasing && arr[index] < lastValue) {
std::cout << "Decreasing (starting at index = " << index << ", value = " << arr[index] << ", previousValue = " << lastValue << ")" << std::endl;
increasing = false;
}
//If we were previously decreasing and the new value is greater than the previous
else if (!increasing && arr[index] > lastValue) {
std::cout << "Increasing (starting at index = " << index << ", value = " << arr[index] << ", previousValue = " << lastValue << ")" << std::endl;
increasing = true;
}
lastValue = arr[index];
}
|