SA-MP Forums Archive
find the biggest variable by looping - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: find the biggest variable by looping (/showthread.php?tid=296742)



find the biggest variable by looping - CounterTerroristUnit - 12.11.2011

hi,

i searched first in order to solve my prob but it didnt help me tho'.
I hope someone can help me.

I have a voting system using variables:
pawn Код:
new VoteOptions[40];

//then everytime someone votes for a certain option the specific variable is increased:

    if(listitem == 0)
    {
        VoteOptions[0]++;
    }
    else if(listitem == 1)
    {
                VoteOptions[1]++;
    }
    else if(listitem == 2)
    {
        VoteOptions[2]++;
    }
        //etc. up to 39...
So how can i find the biggest variable at the end of the vote?
I tried it with looping:

pawn Код:
new biggest;
for(new i = 0; i < VoteOptions[i]; i++)
for(VoteOptions[biggest] < VoteOptions[i])//i dunno what exactly to do here...
biggest = i;

            if(biggest == VoteOptions[0])
            {
                //Do smth. when variable 1 is the biggest
            }
            else if(biggest == VoteOptions[1]
            {
                       //Do smth. when variable 2 is the biggest
            }
            else if(biggest == VoteOptions[2])
            {
                //Do smth. when variable 3 is the biggest
            }
                        //etc...
I hope you can help me (also givin reps+)


Re: find the biggest variable by looping - Sascha - 12.11.2011

pawn Код:
new highest[2] = {VoteOptions[0], 0};
for(new n=0; n<40; n++) //change the 40 to your highest number + 1 (highest number = amount)
{
    if(VoteOptions[n] > highest[0])
        highest[1] = n;
}
new string[100];
format(string, sizeof(string), "Highest value for option %d - value: %d", highest[1], highest[0]);
SendClientMessageToAll(0x999999AA, string);
something like this should work
(didn't test it, just wrote it up)


Re: find the biggest variable by looping - CounterTerroristUnit - 12.11.2011

thx alot!

So can i just do it like:

pawn Код:
new highest[2] = {0, -1};
            for(new n=0; n<42; n++)
            {
                if(MapsToVote[n] > highest[0])
                highest[1] = n;
            }
            if(highest[1] == MapsToVote[0])
            {
                //change to map 1
            }
            else if(highest[1] == MapsToVote[1])
            {
                //change to map 2
            }
            else if(highest[1] == MapsToVote[2])
            {
                //change to map 3
            }
            else if(highest[1] == MapsToVote[3])
            {
                //change to map 4
            }
//...
Can i use it like this?


Re: find the biggest variable by looping - Sascha - 12.11.2011

yeap

oh wait..
there was a little mistake
it should be:
pawn Код:
if(MapsToVote[n] > highest[0])
{
                highest[1] = n;
                highest[0] = MapsToVote[n];
}



Re: find the biggest variable by looping - CounterTerroristUnit - 12.11.2011

k, thx.

can u pls explain to me what this does mean?

pawn Код:
new highest[2] = {0, -1};//the 0 and the -1 here??
im testing it right now thx


Re: find the biggest variable by looping - MadeMan - 12.11.2011

pawn Код:
new biggest;
for(new i = 0; i < sizeof(VoteOptions); i++)
    if(VoteOptions[i] > biggest)
        biggest = VoteOptions[i];

if(biggest == VoteOptions[0])
{
    //Do smth. when variable 1 is the biggest
}
else if(biggest == VoteOptions[1]
{
           //Do smth. when variable 2 is the biggest
}
else if(biggest == VoteOptions[2])
{
    //Do smth. when variable 3 is the biggest
}
            //etc...
Should work too.


Re: find the biggest variable by looping - Sascha - 12.11.2011

Quote:
Originally Posted by CounterTerroristUnit
Посмотреть сообщение
k, thx.

can u pls explain to me what this does mean?

pawn Код:
new highest[2] = {0, -1};//the 0 and the -1 here??
im testing it right now thx
should be better this
pawn Код:
new highest[2] = {VoteOptions[0], 0};
well highest[0] and highest[1] should have 2 different values.
I could now write: highest[0] = value1; and highest[1] = value2;
or I can just write: highest[2] = {value1, value2};
meaning: arrayvariable[amountofdifferentvaluesstored] = {valuefornumberX, valuefornumberX+1, valuefornumberX+2, valuefornumberN};


Re: find the biggest variable by looping - CounterTerroristUnit - 12.11.2011

ok, thx alot for ur help guys

Sascha: Your solution didnt work for me somehow... it might be cause i didnt change the specific part to?:
pawn Код:
new highest[2] = {VoteOptions[0], 0};
MadeMan:No probs with ur code...

Thx alot guys, ofc i give u both reps+

thx!