How to compare array?
#1

-- DELETED --
Reply
#2

PHP код:
new
    
highest 0// Variable to store the current highest number of votes.
    
topscore = -// Variable to store the option with the highest number of votes.
;
for(new 
0!= sizeof(votes); i++) // Start a loop to iterate through all elements of the array 'votes'.
{
    if(
votes[i] > highest// If the number of votes of this option is higher than the current highest...
    
{
        
highest votes[i]; // Replace the current highest number of votes with the amount of votes that this option has.
        
topscore i// Option 'i' now has the highest number of votes.
    
}
}
return 
topscore
If 'topscore' is equal to -1, it means nobody voted. Note that this code does not account for options with tied numbers of votes. This code will account for an array of any size, but if you're only going to be using 3 options, you could simply just check each option against the other.

Eg.
PHP код:
if(votes[0] > votes[1] && votes[0] > votes[2]) // Option '0' has the highest number of votes.
else if(votes[1] > votes[0] && votes[1] > votes[2]) // Option '1' has the highest number of votes.
else if(votes[2] > votes[0] && votes[2] > votes[1]) // Option '2' has the highest number of votes. 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)