How can I write a MAX and MIN sorting program?

I am using Pic Basic Pro and need to be able to find the amplitude of a sinusoidal wave. I Figure this might be a good way to do it.

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:
  • Im building an electric go cart. Can I use an alternator to extend my range, by adding charge to my battery?
  • Why won't my homemade capacitor work!?
  • Why was it not possible to construct a bridge between britan and france instead of channel tunnel?
  • What size genset do I need?
  • What is the type of bridge that can support the most weight?
  • Why Doesnt the back pressure of a positive displacement compressor/pump not affect its delivery pressure?
  • What is the differance Between a cantilever bridge and a suspension bridge?
  • What are the functions and misconceptions of functions of an isolated patient "ground" used in DIGITAL EEG?
  • When was expanded metal mesh first invented?
  • What is a bipolar water electrolyser?