'lrcvs 01.11.17
'Program to eliminate repeated elements in an array.
'1) we sort the initial array, using quicksort.
'2) we remove the repeated elements.
'3) skipping the ones that are the same and reducing the time of "cleaning the repeated elements".
DECLARE SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
DIM AS INTEGER c,f,k,n,x
CLS
x = 3000000 '<<< elements
DIM s (x)AS INTEGER
DIM t (x)AS INTEGER
RANDOMIZE, 3
FOR n = 1 TO x
s(n) = INT(RND * x) + 1
NEXT n
PRINT "Init array of "; x; " elements, some repeated"
ordenar2 (s(),1,x) '<<< quicksort
'******************************************************************************
'Here remove elements repeted
f = 1
c = 1
WHILE f <= x
k = 0
IF s(f) = s(f + 1) THEN
k = 1
f = f + 1
ELSE
t(c) = s(f)
f = f + 1
c = c + 1
END IF
WEND
'******************************************************************************
PRINT
PRINT "here are ";c-1;" unique elements"
PRINT
PRINT "end"
SLEEP
END
SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
'quick sort
DIM AS INTEGER i,j,y
i = inicio
j = final
y = s((INT(i + j) / 2))
WHILE i < j
WHILE s(i) < y
i = i + 1
WEND
WHILE s(j) > y
j = j - 1
WEND
IF i <= j THEN
SWAP s(i), s(j)
i = i + 1
j = j - 1
END IF
WEND
IF j > inicio THEN ordenar2(s(),inicio,j)
IF i < final THEN ordenar2(s(),i,final)
END SUB
'lrcvs 01.10.17
'Program to eliminate repeated elements in an array.
'1) we sort the initial array, using quicksort.
'2) we remove the repeated elements, skipping the ones that are the same
'and reducing the time of "cleaning the repeated elements".
DECLARE SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
DIM AS INTEGER c,f,k,n,x
CLS
x = 20 '<<< elements 'x = number of array elements
DIM s (x)AS INTEGER '<<< This is the main array
DIM t (x)AS INTEGER ''<<< This is the auxiliary array
RANDOMIZE, 3
FOR n = 1 TO x
s(n) = INT(RND * x) + 1 'Here we write the values in the main array
print s(n);" "; 'show elements of auxiliary array
NEXT n
print
PRINT "Init array of "; x; " elements, some repeated"
print
ordenar2 (s(),1,x) 'sort the elements
'******************************************************************************
'Remove elements repeted
f = 1 'f = counter for the main array
c = 0 'c = counter for the auxiliary array
WHILE f <= x
k = 0 'k = control flag
'Here we compare an element of the main array with the following of the main array
'comparing them, if they are equal, the counter (f) of the main array advances one step, and so on
'else comparing them, if they are different, we add the element of the main array to the auxiliary array and increase "c" and 'advance the main counter (f)
'the process is repeated until the end of the main array
IF s(f) = s(f + 1) THEN
k = 1
f = f + 1
ELSE
c = c + 1
t(c) = s(f)
f = f + 1
END IF
WEND
'******************************************************************************
'show elements of auxiliary array
for n = 1 to c
print t(n);" ";
next n
PRINT
PRINT "There are ";c;" unique elements"
PRINT
PRINT "end"
SLEEP
END
'******************************************************************************
SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
'quick sort
DIM AS INTEGER i,j,y
i = inicio
j = final
y = s((INT(i + j) / 2))
WHILE i < j
WHILE s(i) < y
i = i + 1
WEND
WHILE s(j) > y
j = j - 1
WEND
IF i <= j THEN
SWAP s(i), s(j)
i = i + 1
j = j - 1
END IF
WEND
IF j > inicio THEN ordenar2(s(),inicio,j)
IF i < final THEN ordenar2(s(),i,final)
END SUB
That is why FXM always maintains that you compile with -exx.
Yes it does report an error. Out of bounds.
And it is an obvious one. s() is an array with upper bound of x. You loop until f is less than or equal to x. When f = x you then check in the if statement f+1 which will be 30000001. That is out of bounds.
If you don't compile with -exx FB does not do bounds checking.
So test it like this and you will see that without -exx it still reports no errors despite being obviously out of bounds
But really. Just debug your code and you can see that there is an error there.
Again, when f gets to 20 it comes down to the if statement and in that if statement is "s(f + 1)". f + 1 is 21 and that is out of bounds.
Last edited by sancho3 on Nov 04, 2017 1:44, edited 1 time in total.
In addition to compile with the '-exx' option, and in order to can visualize the run-time error messages when running code from FBIde "Run/Quick run" command, you must configure in the "View/Settings/FreeBASIC" menu the "Run command" field as specified at:
To show the bug (I guess it would be hard to find as it occurs randomly)
Just add this line after the ordenar2(...) instruction : s(21)=s(20).
The last value will be lost.....
If by chance the ubound+1 value is equal to the greatest value of the array this value is removed.
In this example seems to be 0 (but could be any value)
So removing elements in an array containing values <=0 the risk is to loose the zero.