Help With Converting C Pointer code To PB Code
Posted: Mon Apr 03, 2017 7:01 pm
I'm trying to convert a C pointer code subroutine into a PB pointer sub. Although I read the the help file material, I'm not familiar with pointer manipulation in PB .
Below is the C code followed by my PB code that compiles and runs but doesn't compute correctly. I'd be appreciative for any help with this.
Below is the C code followed by my PB code that compiles and runs but doesn't compute correctly. I'd be appreciative for any help with this.
Code: Select all
/*-------------------------------------------------------------------------*/
/* This function uses a simple O(N^2) implementation. It probably has a
* smaller constant and therefore is useful in the small N case, and is also
* useful for testing the relatively complex O(N log N) implementation.
*/
float kendallSmallN( float *arr1, float *arr2, int len )
{
int m1 = 0, m2 = 0, s = 0, nPair , i,j ;
float cor ;
/* printf("enter kendallSmallN\n") ; */
for(i = 0; i < len; i++) {
for(j = i + 1; j < len; j++) {
if(arr2[i] > arr2[j]) {
if (arr1[i] > arr1[j]) {
s++;
} else if(arr1[i] < arr1[j]) {
s--;
} else {
m1++;
}
} else if(arr2[i] < arr2[j]) {
if (arr1[i] > arr1[j]) {
s--;
} else if(arr1[i] < arr1[j]) {
s++;
} else {
m1++;
}
} else {
m2++;
if(arr1[i] == arr1[j]) {
m1++;
}
}
}
}
nPair = len * (len - 1) / 2;
if( m1 < nPair && m2 < nPair )
cor = s / ( sqrtf((float)(nPair-m1)) * sqrtf((float)(nPair-m2)) ) ;
else
cor = 0.0f ;
return cor ;
}
Code: Select all
;PB Code
EnableExplicit
;==============KendalTauSlow=======================================
Procedure.d kendallSmallN( *arr1.double, *arr2.double, len.l )
Protected m1.l = 0, m2.l = 0, s.l = 0, nPair.l , i.l,j.l ,qSOD.q
Protected cor.d
qSOD=SizeOf(double)
For i = 0 To len-1
For j = i + 1 To len-1
If (*arr2+i*qSOD) > (*arr2+j*qSOD)
If (*arr1+i*qSOD) > (*arr1+j*qSOD)
s=s +1
ElseIf (*arr1+i*qSOD) < (*arr1+j*qSOD)
s=s-1
Else
m1=m1+1
EndIf
ElseIf (*arr2+i*qSOD) < (*arr2+j*qSOD)
If (*arr1+i*qSOD) > (*arr1+j*qSOD)
s=s-1
ElseIf (*arr1+i*qSOD) < (*arr1+j*qSOD)
s=s+1;
Else
m1=m1+1
EndIf
Else
m2=m2+1
If (*arr1+i*qSOD) = (*arr1+j*qSOD)
m1=m1+1;
EndIf
EndIf
Next j
Next i
nPair = len * (len - 1) / 2
If( m1 < nPair And m2 < nPair )
cor = s / ( Sqr(nPair-m1) * Sqr(nPair-m2) )
Else
cor = 0
EndIf
ProcedureReturn cor
EndProcedure
;========================Run======================================
OpenConsole()
ConsoleTitle ("PureBasic - Console Example:")
EnableGraphicalConsole(1)
Define i.l, n.l, tau.d, taufast.d
Dim a.d(100)
Dim b.d(100)
a(0)=1 :a(1)=1: a(2)=1: a(3)=2: a(4)=3: a(5)=3: a(6)=4: a(7)=4 :
b(0)=1 :b(1)=2: b(2)=1: b(3)=3: b(4)=3: b(5)=5: b(6)=5: b(7)=5
tau=KendallSmallN(a(),b(),8)
PrintN("KendallSmallN="+StrD(tau)+ " Should be =0.869565")
Input()
CloseConsole()
End