Raised This Month: $51 Target: $400
 12% 

Comparing variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Jon
Veteran Member
Join Date: Dec 2007
Old 07-12-2008 , 20:06   Comparing variables
Reply With Quote #1

Lets say I want to compare 4 variables. Then only return the 2 largest of them. It doesn't matter which one of these two that are the largest.

Code:
Example:

var1 = 2
var2 = 5
var3 = 3
var4 = 9

func_compare()
{
    Somehow find out that var2 and var4 are the two largest ones.
    
    Since they were the two largest ones, enable stuff:
    bool_var2 = true;
    bool_var4 = true;
}

then later: 
if(bool_var2)
     yay, var2 was one of the two largest ones of the four.
How?

Last edited by Jon; 07-12-2008 at 20:08.
Jon is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 07-12-2008 , 20:51   Re: Comparing variables
Reply With Quote #2

look at SortIntegers
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Jon
Veteran Member
Join Date: Dec 2007
Old 07-13-2008 , 08:14   Re: Comparing variables
Reply With Quote #3

Quote:
Originally Posted by Emp` View Post
look at SortIntegers
Could you give me a quick example?
Jon is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 07-13-2008 , 08:21   Re: Comparing variables
Reply With Quote #4

Code:
new li_Test[ 4 ] = { 2, 5, 3, 9 }; SortIntegers ( li_Test, sizeof li_Test, Sort_Descending );

li_Test will become : { 9, 5, 3, 2 }

Then, you have just to retrieve the 2 first values ( li_Test[ 0 ] and li_Test[ 1 ] ) to get the 2 largest number.
__________________
Arkshine is offline
Jon
Veteran Member
Join Date: Dec 2007
Old 07-13-2008 , 08:28   Re: Comparing variables
Reply With Quote #5

Yeah, I tested with this:
Code:
#include <amxmodx> #include <amxmisc> #define PLUGIN "New Plug-In" #define VERSION "1.0" #define AUTHOR "Jon" new var1 new var2 new var3 new var4 public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)         register_clcmd("say /var1", "plusvar1")     register_clcmd("say /var2", "plusvar2")     register_clcmd("say /var3", "plusvar3")     register_clcmd("say /var4", "plusvar4")         register_clcmd("say /showvars", "showvars")     } public plusvar1(id) {     var1++ } public plusvar2(id) {     var2++ } public plusvar3(id) {     var3++ } public plusvar4(id) {     var4++ } public showvars(id) {     new store[4];         store[0] = var1     store[1] = var2     store[2] = var3     store[3] = var4         SortIntegers(store, 4, Sort_Descending)         client_print(id, print_chat, "%d is the largest, %d is the second largest", store[0], store[1]) }

But the important thing is, I need to know the names of the vars that became the two largest. The numbers are only for comparing them.

Last edited by Jon; 07-13-2008 at 08:32.
Jon is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 07-13-2008 , 08:43   Re: Comparing variables
Reply With Quote #6

I think amxx should provide a solution for your situation which I think is useful for some cases.
Just offer another array that store the index of the original array which has been sorted.
In this example the index array should be {3, 1, 2, 0}
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Jon
Veteran Member
Join Date: Dec 2007
Old 07-13-2008 , 09:01   Re: Comparing variables
Reply With Quote #7

There must be a better way, terrible:
Code:
public showvars(id) {     new store[4];         store[0] = var1     store[1] = var2     store[2] = var3     store[3] = var4         SortIntegers(store, 4, Sort_Descending)         if(store[0] == var1)     {         largest = "variable1"     }     else if(store[0] == var2)     {         largest = "variable2"     }     else if(store[0] == var3)     {         largest = "variable3"     }     else if(store[0] == var4)     {         largest = "variable4"     }     client_print(id, print_chat, "%s is the largest", largest)         if(store[1] == var1)     {         second_largest = "variable 1"     }     else if(store[1] == var2)     {         second_largest = "variable 2"     }     else if(store[1] == var3)     {         second_largest = "variable 3"     }     else if(store[1] == var4)     {         second_largest = "variable 4"     }         client_print(id, print_chat, "%s is the second largest", second_largest) }

Last edited by Jon; 07-13-2008 at 09:12.
Jon is offline
travo
Senior Member
Join Date: Aug 2006
Old 07-13-2008 , 09:39   Re: Comparing variables
Reply With Quote #8

i think this is what you are wanting

PHP Code:
     new store[8];
    
store[0] = 10;
    
store[1] = 100;
    
store[2] = 10240;
    
store[3] = 7611;
    
store[4] = -404;
    
store[5] = 2523;
    
store[6] = 66;
    
store[7] = -23;
    
    new 
varid[9], varval[9], tempabc;
    
arrayset(varval, -100007);
    for (
08a++)
    {
        
temp store[a];
        
80;
        while (
b-- > && !c)
            if (
temp varval[b])
            {
                
varval[1] = varval[b];    varid[1] = varid[b];
                
varval[b] = temp;            varid[b] = a;
            }
            else
                
1;
        
    }
    
    for (
08a++)
        
server_print("%d %d"varid[a], varval[a]); 
travo is offline
Jon
Veteran Member
Join Date: Dec 2007
Old 07-13-2008 , 14:11   Re: Comparing variables
Reply With Quote #9

Quote:
Originally Posted by travo View Post
i think this is what you are wanting

PHP Code:
     new store[8];
    
store[0] = 10;
    
store[1] = 100;
    
store[2] = 10240;
    
store[3] = 7611;
    
store[4] = -404;
    
store[5] = 2523;
    
store[6] = 66;
    
store[7] = -23;
    
    new 
varid[9], varval[9], tempabc;
    
arrayset(varval, -100007);
    for (
08a++)
    {
        
temp store[a];
        
80;
        while (
b-- > && !c)
            if (
temp varval[b])
            {
                
varval[1] = varval[b];    varid[1] = varid[b];
                
varval[b] = temp;            varid[b] = a;
            }
            else
                
1;
        
    }
    
    for (
08a++)
        
server_print("%d %d"varid[a], varval[a]); 
Could be, but I didn't understand much of that code.
Jon is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:23.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode