Finding the minimum and maximum values ​​of an unknown continuous loop

I am trying to find the minimum and maximum values ​​for the continuous while loop given below, but somehow I cannot correctly understand the logic. Please let me know where I am going wrong.

while (true)
   {
         Function(&RawX, &RawY, &RawZ);// Keeps generating new RawX,Y and Z values

         if(MaxRawX < RawX)
            MaxRawX = RawX;
         if(MinRawX > RawX)
            MinRawX = RawX;

         Output("MaxRawX:%0.2f",MaxRawX);
   }

The problem I encountered with the above algorithm is that the values ​​of RawX, RawY and RawZ are constantly changing. For example, at some point I have values ​​from -46 to -35. I want my program to display MinRawX as -46 and MaxRawX as -35. At some other point, I can have values ​​between 201 and 215, where I want it to display MaxRawX as 215 and MinRawX as 201. Its basically some sensor data that I get from my equipment. I am sure that I am doing something wrong here, considering it very simple, but I can not understand. Any suggestions?

+3
source share
3 answers

, . , , .

: , reset , , @dirkgently. , min max n, . , , FIFO:

std::deque<int> lastRawXs;
const int frameSize = 100; // only keep the last 100 measures    

while (true)
{
    // Keeps generating new RawX,Y and Z values
    Function(&RawX, &RawY, &RawZ);// 

    if (lastRawXs.size() >= frameSize)
    {
        lastRawXs.pop_front();
    }
    lastRawXs.push_back(RawX);

    typedef std::deque<int>::const_iterator iterator;
    std::pair<iterator, iterator> minMaxRawX =
        boost::minmax_element(lastRawXs.begin(), lastRawXs.end());

    Output("MinRawX:%0.2f", *minMaxRawX.first);
    Output("MaxRawX:%0.2f", *minMaxRawX.second);
}

. () :

const int frameSize = 100;
std::circular_buffer<int> lastRawXs(frameSize);

while (true)
{
    Function(&RawX, &RawY, &RawZ); // keeps generating new RawX,Y and Z values

    lastRawXs.push_back(RawX); // overwrites old measures if buffer is full

    typedef std::circular_buffer<int>::const_iterator iterator;
    std::pair<iterator, iterator> minMaxRawX =
        boost::minmax_element(lastRawXs.begin(), lastRawXs.end());

    Output("MinRawX:%0.2f", *minMaxRawX.first);
    Output("MaxRawX:%0.2f", *minMaxRawX.second);
}
+5

, max/min, - :

const int MAX_AGE = 1000; // number of samples before min/max expires
int MinRawXAge = INT_MAX;
int MaxRawXAge = INT_MAX;

while (true)
{
    if (MinRawXAge > MAX_AGE) // test to see whether MinRawX has expired
    {
        MinRawX = INT_MAX;
        MinRawXAge = 0;
    }

    if (MaxRawXAge > MAX_AGE) // test to see whether MaxRawX has expired
    {
        MaxRawX = INT_MIN;
        MaxRawXAge = 0;
    }

    Function(&RawX, &RawY, &RawZ); // Keeps generating new RawX, Y and Z values

    if (MaxRawX < RawX) // test for new MaxRawX
    {
        MaxRawX = RawX;
        MaxRawXAge = 0;
    }
    else
    {
        MaxRawAge++;
    }

    if (MinRawX > RawX) // test for new MinRawX
    {
        MinRawX = RawX;
        MinRawXAge = 0;
    }
    else
    {
        MinRawAge++;
    }

    Output("MinRawX: %0.2f, MaxRawX: %0.2f", MinRawX, MaxRawX);
}

( ) , (1000 ), "" min/max.

+2

You probably have a problem with your specification.

Your code will continuously print the current maximum values. But from your description, it seems like you really don't want maximum values. So what do you need?

Before you start writing code, you need the right specification.

+1
source

All Articles