I have this code in python and it works correctly. The idea of this code is compute the repetition of each value in the array depending on the threshold value. The output for the python code is [value,repetition]
[1.2, 3, 2.4, 1, 2.5, 1, 2.3, 1, 2.4, 1, 8.5, 2, 8.9, 1, 9.11, 1]
def dif(x, y, ma1):
res=0
if(math.fabs(x-y) <= ma1):
res=1
return res
def enc(text,th):
coded=[]
coded.clear()
index=0
unt=1
while index<=(len(text)-1):
if index==(len(text)-1) or dif(text[index],text[(index+1)],th) !=1:
coded.append(text[index])
coded.append(unt)
unt=1
else:
unt=unt+1
index=index+1
return coded
SenList=[1.1,1.1,1.2,2.4,2.5,2.3,2.4,8.6,8.5,8.9,9.11]
th = 0.1
comm= enc(SenList,th)
print(comm)
And this is the C code and the output for the C cod is :
1.100000 2 1.200000 1 2.500000 2 2.300000 1 2.400000 1
8.600000 1 8.500000 1 8.900000 1 9.110000 1
int dif(float x,float y,float sigma1){
int res=0;
if(fabsf(x-y) <= sigma1)
res=1;
return res;
}
void RL(){
float text [] = {1.1,1.1,1.2,2.4,2.5,2.3,2.4,8.6,8.5,8.9,9.11} ;
int n = sizeof(text)/sizeof(text[0]);
float th =0.1;
float num[30]; int nc = 0;
int cou[30]; int nc1= 0;
int index=0;
int unt=1;
while (index<(n)){
if ( (index==(n-1)) || (dif(text[index],text[index+1],th)!=1) ) {
cou[nc] = unt; nc=nc+1;
num[nc1] = text[index]; nc1=nc1+1;
unt=1;
}
else{
unt=unt+1;
}
index=index+1 ;
}
for(int i=0; i<nc;i++){
printf(" %3f %d \n ",num[i],cou[i]);
}
}
Why the C code deal with the values as int not float(compute the repetition as int)? how can fix the problem in this code please? Note: the C code work correctly if I use int array.