Hi Charles,
sometimes I am unhappy about my faulty logic. This is a small bubblesort example which uses overloading:
include "$/inc/console.inc"
macro swap(a,b) {tmpval=a : a=b : b=tmpval}
macro print_array(a,c)
typeof a *arr
@arr = @a
for x=1 to c
print arr[x] " "
next
printl
end macro
'applying indexbase 1
sub bubble_sort(int a[],int c, d) 'd=1 ascending, else descending
u=1 'unsorted
while u
u=false
for i=1 to c-1
if d=1 then 'ascending
if a[i]>a[i+1] then
swap a[i],a[i+1] : u=true
end if
else 'descending
if a[i]<a[i+1] then
swap a[i],a[i+1] : u=true
end if
end if
next
wend
end sub
'applying indexbase 1
sub bubble_sort(double a[],int c, d) 'd=1 ascending, else descending
u=1 'unsorted
while u
u=false
for i=1 to c-1
if d=1 then 'ascending
if a[i]>a[i+1] then
swap a[i],a[i+1] : u=true
end if
else 'descending
if a[i]<a[i+1] then
swap a[i],a[i+1] : u=true
end if
end if
next
wend
end sub
'applying indexbase 1
sub bubble_sort(string a[],int c, d) 'd=1 ascending, else descending
u=1 'unsorted
while u
u=false
for i=1 to c-1
if d=1 then 'ascending
if a[i]>a[i+1] then
swap a[i],a[i+1] : u=true
end if
else 'descending
if a[i]<a[i+1] then
swap a[i],a[i+1] : u=true
end if
end if
next
wend
end sub
==========================================
int array1[] = {4,65,2,-31,0,99,2,83,782,1}
double array2[] = {4.3,65.7,2,-31.4,0,99,2.1,83,782,1}
string array3[] = {lcase("Oxygen"),"is","a","nice","and","powerful","programming","language"}
print_array(array1[], countof array1[])
bubble_sort(array1[], countof array1[], 0)
print_array(array1[], countof array1[],)
bubble_sort(array1[], countof array1[], 1)
print_array(array1[], countof array1[],)
printl
print_array(array2[], countof array2[])
bubble_sort(array2[], countof array2[], 0)
print_array(array2[], countof array2[])
bubble_sort(array2[], countof array2[], 1)
print_array(array2[], countof array2[])
printl
print_array(array3[], countof array3[])
bubble_sort(array3[], countof array3[], 0)
print_array(array3[], countof array3[])
bubble_sort(array3[], countof array3[], 1)
print_array(array3[], countof array3[])
printl
printl "Enter ..." : waitkey
Output:
4 65 2 -31 0 99 2 83 782 1
782 99 83 65 4 2 2 1 0 -31
-31 0 1 2 2 4 65 83 99 782
4.2999999999999998 65.700000000000003 2 -31.399999999999999 0 99 2.1000000000000001 83 782 1
782 99 83 65.700000000000003 4.2999999999999998 2.1000000000000001 2 1 0 -31.399999999999999
-31.399999999999999 0 1 2 2.1000000000000001 4.2999999999999998 65.700000000000003 83 99 782
oxygen is a nice and powerful programming language
programming powerful oxygen nice language is and a
a and is language nice oxygen powerful programming
If I try to use a macro for the sorting routine this will fail, only the results for the first type are correct:
include "$/inc/console.inc"
macro swap(a,b) {tmpval=a : a=b : b=tmpval}
macro print_array(a,c)
typeof a *arr
@arr = @a
for x=1 to c
print arr[x] " "
next
printl
end macro
'applying indexbase 1
macro bubble_sort(a,c,d) 'd=1 ascending, else descending
typeof a *bsa
@bsa = @a
u=1 'unsorted
while u
u=false
for i=1 to c-1
if d=1 then 'ascending
if bsa[i]>bsa[i+1] then
swap bsa[i],bsa[i+1] : u=true
end if
else 'descending
if bsa[i]<bsa[i+1] then
swap bsa[i],bsa[i+1] : u=true
end if
end if
next
wend
end macro
==========================================
int array1[] = {4,65,2,-31,0,99,2,83,782,1}
double array2[] = {4.3,65.7,2,-31.4,0,99,2.1,83,782,1}
string array3[] = {lcase("Oxygen"),"is","a","nice","and","powerful","programming","language"}
print_array(array1[], countof array1[])
bubble_sort(array1[], countof array1[], 0)
print_array(array1[], countof array1[],)
bubble_sort(array1[], countof array1[], 1)
print_array(array1[], countof array1[],)
printl
print_array(array2[], countof array2[])
bubble_sort(array2[], countof array2[], 0)
print_array(array2[], countof array2[])
bubble_sort(array2[], countof array2[], 1)
print_array(array2[], countof array2[])
printl
print_array(array3[], countof array3[])
bubble_sort(array3[], countof array3[], 0)
print_array(array3[], countof array3[])
bubble_sort(array3[], countof array3[], 1)
print_array(array3[], countof array3[])
printl
printl "Enter ..." : waitkey
Output:
4 65 2 -31 0 99 2 83 782 1
782 99 83 65 4 2 2 1 0 -31
-31 0 1 2 2 4 65 83 99 782
4.2999999999999998 65.700000000000003 2 -31.399999999999999 0 99 2.1000000000000001 83 782 1
782 99 83 66 4 2 2 1 0 -31
-31 0 1 2 2 4 66 83 99 782
oxygen is a nice and powerful programming language
programming language 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Is there a mistake in the sorting macro or do I only exaggerate and overstrain the use of a macro? I also tried to use type 'any' in the bubble_sort routine, but probably I overlooked something in this case too.
Roland