How can I write a MAX and MIN sorting program?
Answer:
Not familiar with PIC basic, but rather than sorting, you just need to find the min and max from all of your values. The person at the top didn't take into account the possibility that your sine wave had a DC offset (not centered at zero).
Assume wave is array of 100 points called Wave[100]
We'll store the min and max values as SinMin and SinMax
Your program would look something like this, although it could be done differently depending on what your Basic interpreter will tolerate. Ignore the leading periods -- this silly system won't tolerate leading spaces or tabs. I'll assume that you may have positive and negative values.
SinMin = Wave[1]
SinMax = Wave[1]
FOR x = 2 to 100
..if Wave[x] < SinMin THEN SinMin = Wave[x]
..if Wave[x] > SinMax THEN SinMax = Wave[x]
NEXT x
Amplitude = ABS(SinMax - SinMin)
The "abs" takes care of the possibility that all of your values may be negative.
figure the MAX that matches the MIN. it should be in the middle
assign two variables, min_var and max_var.
initialise min_var and max_var with the value of the first number in the data set
then loop through each of the values in the data set. at each step...
if curr_var>max_var, max_var=curr_var;
if curr_var<min_var, min_var=curr_var;
at the end of the loop, you should find that max_var contains the maximum value, and min_var contains the minimum.
PS If the sinusoid repeats, you could maybe try saving on computation time by detecting this and avoid looping through the rest of the data
Hope this helps... good luck
The answers post by the user, for information only, FunQA.com does not guarantee the right.
More Questions and Answers: