Check which is highest
#1

Let's say I have 5 distances:

Код:
A = 51.0;
B = 52.0
C = 10.0
D = 12.0
E = 1024.0
I need to find the highest, or the lowest, of these 5. How would I do that? I mean, I can "if(A > B)" but I don't know how to do it with 3 or more values.
Reply
#2

pawn Код:
new Float:distances[] = { 51.0, 52.0, 10.0, 12.0, 1024.0 }; // Set an array with distances
pawn Код:
new highest;
for(new i; i < sizeof(distances); i++) // Loop thru the distances
{
    if(distances[highest] < distances[i]) // Check if the distance is higher than the previous highest distance
    {
        highest = i; // If it is, set this slot to the currently highest distance and continue the loop
    }
}

// Once the loop has ended, 'highest' variable contains the slot that has the highest distance

printf("highest distance is %d (%f)", highest, distances[highest]);
Might help.
Reply
#3

Aaah thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)