I am trying to find the minimum and maximum values for the continuous while loop given below, but somehow I am unable to get the logic right. Kindly 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 that I am facing with the above algorithm is that the values of RawX, RawY and RawZ are continuously changing. For eg: at one point, I have values ranging from -46 to -35. I want my program to display MinRawX as -46 and MaxRawX as -35. At some other point, I might have values in between 201 to 215, where I want it to display MaxRawX as 215 and MinRawX as 201. Its basically some sensor angle data I receive from my hardware. I am sure I am doing something wrong here considering this being very basic but can’t figure it out. Any suggestions?
If I understand correctly, you want to have the minimum and maximum for a given time frame. The solution you use keep the minimum and maximum since the beginning of the program.
Depending on your needs, you have several solutions: for instance, you can simply reset the min and max from time to time, like @dirkgently suggested. If you want a moving range, so that at any point in time you have the min and max of the
nlast measurements, then you will have to use a more complex solution. The only one I can think of is keeping the measurements in a FIFO container:Edit: Here is an alternative (better) solution using a circular buffer: