Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(Solved).. Ranking Problem
#1
if i have a lot of double value:
double a = 90.12
double b = 124.03
double c = 43.32
double d = 39.24
double e = 78.31
and more
can anyone help me, what syntac that i need to know which one will be the 1st highest, 2nd highest, 3rd highest ...........until ranking 10.?
Thank you for the help..

Regards.
Wiewie
#2
Code:
Copy      Help
out

;create array for testing

ARRAY(double) a.create(20)
int i
srand GetTickCount ;;initialize random number generator
for(i 0 a.len) a[i]=rand/32767.0*200 ;;generate random numbers between 0 and 200

;see what we have
out "--- Array ---"
for(i 0 a.len) out a[i]
;______________________________

;find 10 highest numbers

ARRAY(int) ai ;;will contain indices of max 10 highest numbers in a

ai.create(iif(a.len>=10 10 a.len))
ARRAY(byte) at.create(a.len)
int j k
for(j 0 ai.len)
,double mx=-1; int imx=0
,for(k 0 a.len) if(!at[k] and a[k]>mx) mx=a[k]; imx=k
,ai[j]=imx
,at[imx]=1
;______________________________

;results

out "--- Top %i ---" ai.len
for(i 0 ai.len) out "%i, value=%f" ai[i] a[ai[i]]

___________________________________________________________

Or maybe you want to sort the array.

To sort numbers or strings or other types, can be used qsort. Example:

Code:
Copy      Help
ARRAY(double) a.create(4)
a[0]=7.35
a[1]=3.57
a[2]=3.24
a[3]=7.12
qsort &a[0] a.len sizeof(double) &doublesortproc
int i
for i 0 a.len
,out a[i]

The example also uses this function:
Function doublesortproc
Code:
Copy      Help
function[c] double&i1 double&i2

if(i1>i2) ret 1
if(i1<i2) ret -1
#3
wow...Thank you.. you are very helpfull.

wiewie


Forum Jump:


Users browsing this thread: 1 Guest(s)