Comparing variables
#1

What is the best way to compare n integers and get the largest one?
Reply
#2

if(int1 > in2) //This is saying if int 1 is bigger...
if(int1 < int2) // this is saying if Int 2 is bigger

Its jst simple maths All you have to do is imagine Pac Man and Pac man wats to eat the biggest one xD
Reply
#3

Quote:
Originally Posted by IceCube!
Посмотреть сообщение
if(int1 > in2) //This is saying if int 1 is bigger...
if(int1 < int2) // this is saying if Int 2 is bigger

Its jst simple maths All you have to do is imagine Pac Man and Pac man wats to eat the biggest one xD
No offense, but I've seen you post on multiple threads in Scripting Discussion with info useless to the OP.
Please re-read. I said n variables. Say, 32 variables. And I want to get the largest one out of them.
Reply
#4

For example: there are 3 variables x, y, z. You need to make a variable and compare it with every integer.
If you have 32 integers then you need to compare the variable with 32 integers.
pawn Код:
new
    max_int = 0, // The variable will be the largest integer.
    x = 1, // 1st integer.
    y = 5, // 2nd integer.
    z = 10; // 3rd integer.
Now, we will compare them with the max_int the variable we made.
Note: You need to set that variable to very low number.
pawn Код:
// if x is higher than 0, then the max is the x's value
    if( x > max_int )
    {
        max_int = x; // max = x = 1
    }
    else if( y > max_int ) // the same happens with the y variable too. If y is higher than max then max = y's value, else it skip this and continue..
    {
        max_int = y; // max = y = 5
    }
    else if( z > max_int ) // It's check if the z value is higher than the max and if it is, max takes z's value..
    {
        max_int = z; // max = z = 10
    }
    printf( "The max is: %i", max_int ); // It will print in the console the higher number
Reply
#5

assuming you got all variables in an array, you can do with a loop.
in top of the script, create the array, like
pawn Код:
new Value[]={84,257,24,8,258,42,7,81,67,456,812,588,12,1,815,46};
a linear search will do fine:
pawn Код:
new MaxValueYet;
new MaxValueInCell;
for(new c=0;c<sizeof(Value);c++)
{
    if(Value[c]>MaxValueYet)
    {
        MaxValueInCell=c;
        //MaxValueYet=Value[c];//its the same as:
        MaxValueYet=Value[MaxValueInCell];
    }
}
Value[MaxValueInCell] is the amount (815), where MaxValueInCell is obvious: the "entry", or cell of the array Value[its 14 btw]
Reply
#6

Obviously, it's more easier way and less lines to script, but in case the integer isn't stable I mean the integer can be change every minute in-game, it's no too much efficient. I will recomment to compare every inetger with the max.
But it's your choice!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)