How to check if a sequence of numbers has a increasing/decreasing trend in C++
c++, int, trend, vector
Solution
I would accumulate the number of increases vs number of decreases, which should give you an idea of whether there's an overall trend to increase or decrease.
Problem
What's the best way to check if a sequence of numbers has an increasing or decreasing trend? I know that I could pick the first and last value of the sequence, and check their difference, but I'd like a somewhat more robust check. This means that I want to be able to tolerate a minority of increasing values within a mostly decreasing sequence, and viceversa. More specifically, the numbers are stored as ``` vector<int> mySequence; ``` A few more details about the number sequences that I am dealing with: - All the numbers within the sequence have the same order of magnitude. This means that no sequence like the following can appear: [45 38 320 22 12 6]. - By descending trend I mean that most or all the numbers within the sequence are lesser than the previous one. (The opposite applies for ascending trend). As a consequence, the following sequence is to be considered as descending: [45 42 38 32 28 34 26 20 12 8 48]