10 Integers, Highest Value, Siemens

userxyz

Member
Join Date
May 2002
Location
any
Posts
2,768
I have a Fifo buffer, values are shifting every 0.2 seconds.

Fifo buffer is 10 integers long.

So every 2 seconds there came 10 new values in the buffer.

What I need to do is taking the 3 highest values.

How can I know wich values to take, how can I know wich values are the highest in other words ?
 
hmmmm

j [url="http://nl.wikipedia.org/wiki/Quicksort#De_basis" said:
http://nl.wikipedia.org/wiki/Quicksort#De_basis[/url]
acekd]http://en.wikipedia.org/wiki/Sorting_algorithm Choose one, implement in S7, use against your data... And that's all :whistle:

I'd suggest selection sort method.

All these examples are in Java or C

I understand SCL, but Java and C not well.

But I now know that I need s SORT ALGORITM.

Anyone who can translate an example program (QUICKSORT for example) of that site in SCL or STL ?

Thanks in forward
 
Quicksort is one of faster methods but only when you have enough of data to be sorted. It is more complicated and not worth implementing for such small quantity. Personally I'd just use modified bubble sort (3-passes are enough : find largest of 10, you have 9 left, find largest in those 9 and you have 8 ints left, find biggest in 8 and you are done)
 
thanks

thanks

Found code of the bubble in the pascal language, allmost the same as SCL


edit] Pascal
Procedure BubbleSort( Var Data : IntArray; Count : integer; First : integer; Last : integer; Acend : boolean; Var Succ : integer );var i, temp, s_begin, s_end, numsort : integer; sorted : boolean;Begin { initialize for full array sort } s_begin := STARTINTARRAY; s_end := STARTINTARRAY + count - 1 ; numsort := Count; Succ := 0; { assume success } { check for a subset sort; check parameters for correctness } if (Count = 0) then Begin If (First < STARTINTARRAY) then Begin { error: sort start index too low } Succ := 1; Exit; End; If (Last > MAXINTARRAY) then Begin { error: sort end index too high } Succ := 2; Exit; End; if (Last < STARTINTARRAY) then Begin { error: sort end index too low } Succ := 3; Exit; End; s_begin := First; s_end := Last; numsort := Last - First + 1; End; If numsort <= 1 then Exit; { only one element, so exit } If Acend then Begin { do the ascending sort } Repeat sorted := true; { flag default is true } For i := s_begin to s_end -1 do if (Data < Data[i+1]) then Begin { swap contents of Data and Data[i+1] } temp := Data; Data := Data[i+1]; Data[i+1] := temp; { set flag to indicate a swap occured; i.e., sort may not be completed } sorted := false; End; Until sorted; End Else Begin { do the descending sort } Repeat sorted := true; { flag default is true } For i := s_begin to s_end -1 do if (Data < Data[i+1]) then Begin { swap contents of Data and Data[i+1] } temp := Data; Data := Data[i+1]; Data[i+1] := temp; { set flag to indicate a swap occured; i.e., sort may not be completed } sorted := false; End; Until sorted; End;End;
 
Hi,

Seeing this is a fifo buffer and only one new value is added, I'd take different approach:

Take new value, compare it against the 3 highest. If higher then one of them, then if neccesary shift other(s) down (only needed if highest2) and then replace with new value (first shift then replace to avoid loss of data..:D)

Would be easy to do in STL and alot less complicated. You can do this cyclicly or on every new addition to the FIFO (every 0.2sec)

Just my 2cts.

Jeebs
 
This is what I have yntil now in SCL

Some SCL problems


codee.JPG
 
Jeebs said:
Hi,

Seeing this is a fifo buffer and only one new value is added, I'd take different approach:

Take new value, compare it against the 3 highest. If higher then one of them, then if neccesary shift other(s) down (only needed if highest2) and then replace with new value (first shift then replace to avoid loss of data..:D)

Would be easy to do in STL and alot less complicated. You can do this cyclicly or on every new addition to the FIFO (every 0.2sec)

Just my 2cts.

Jeebs


The problem here is, how do you do this when one of those highest may move out of the FIFO, you would have to compare the number being moved out with the three you have and then what do you replace it with if it comes out?

Maybe search the FIFO!!
 
PeterW said:
The problem here is, how do you do this when one of those highest may move out of the FIFO, you would have to compare the number being moved out with the three you have and then what do you replace it with if it comes out?

Maybe search the FIFO!!

Somehow I fail to see the problem you are trying to describe.
 
Jeebs said:
Somehow I fail to see the problem you are trying to describe.

Maybe I haven't grasped what you were saying, but its a FIFO a length of 10, which means on the 11th pulse, the first value leaves the FIFO.

What if that 1st value was one of the top 3, then the top 3 would have to be re-adjusted and the next highest brought into the top 3.

In your explanation I cannot see where you handle this.
 
I assume the listed routine is called on each entry of data into the FIFO. It doesn't modify the original (unless the calling routine sets 'Data_In' and 'Data_Out' to point to the same array.) It just seems a waste to sort the entire array when it will only require the first three passes.
 

Similar Topics

Hi, how do I convert 2x Integer registers to a Real? The two integers are from Modbus registers that contain a floating point value, I need to...
Replies
2
Views
102
Is there a way in CX-Programmer to ad UINTs together with an instruction without a compiler warning. Ladder Rung: Warning: Instructions...
Replies
2
Views
1,252
So I'm looking at replacing some obsolete Prosoft boxes that currently convert DH+ to Ethernet. I have a logix chassis that already has a...
Replies
10
Views
2,906
I'm looking at program for a air compressor (SLC 04). I'm trying to figure out why I can't change a setpoint that's located in a N: address. The...
Replies
10
Views
4,033
I have to take a 16 bit N register and split some of the bits into separate integers. example: Bits 0-1 = integer with a value from 0-3 Bit 2 is...
Replies
22
Views
5,865
Back
Top Bottom