How to compare multiple intergers?

If you have multiple intergers, or something, how can you put the in a list and compare them greatest to least? Say, 4 intergers?


K.V.

Something like this?

x = 1
y = 2
z = 3
yy = 4
game.zz = o
if (y > x) {
  msg ("y is greater than x")
}
if (z > x) {
  msg ("z is greater than x")
}
if ((z > x) and x < yy) {
  game.zz = z + yy
  msg ("You have {game.zz} whatever{if game.zz<>1:s}")
}
msg ("x is " + x + "<br/>y is " + y + "<br/>z is " + z + "<br/>yy is " + yy + "<br/>zz is " + game.zz + "")

(filler for getting edited post, updated/posted)


this is a common intro programming program/assignment given in programming classes, which then leads into searching designs/algorithms (linear search, binary search, etc):

finding/getting the 'min' and 'max' Values of a bunch of data

(you can do a lot more stuff too, but this is just the skeleton basics of this type of stuff)

game.stringlist_attribute = split ("4;44;55;0;66;7;8;9;5;99;6;7;1;2;3;12", ";")
game.max_integer_attribute = 0
game.min_integer_attribute = 0

// linear search (this is actually extremely fast, as you most likely won't have a data quantity size where binary then becomes more efficient, and people don't realize that it actually comes down to the physics of the hardware, aka memory is contigious, and thus makes linear searches even faster than they should be when just looking at it mathematically with Big O notation / quantity of operations):

// max_min_function (game.stringlist_attribute)

<function name="max_min_function"parameters="stringlist_parameter">

  <![CDATA[

    // setting these both to the first data value in the list:

    min_integer_variable = ToInt (StringListItem (stringlist_parameter, 0)) // for this example, the first value in the list is: 4 // min_integer_variable = 4
    max_integer_variable = min_integer_variable // for this example, the first value in the list is: 4 // max_integer_variable = 4

    // iterating through the list, check to see if each data value is greater or lower than the current min/max value, if so, then that new value replaces the old value, which will end with you having found the max and min values from the list:

    foreach (string_variable, stringlist_parameter) {
      integer_variable = ToInt (string_variable)
      if (integer_variable > max_integer_variable) {
        max_integer_variable = integer_variable
      }
      if (integer_variable < min_integer_variable) {
        min_integer_variable = integer_variable
      }
    }

   // storing the min/max values into Attributes, so you can use them. (Using a Function's 'return' only returns a single Data Value, so it's easier to just store the Values into Attributes, than having to return a Stringlist VARIABLE, for holding both values, and then splitting/getting those 2 values from the returned stringlist, lol. But, if you only got a single data Value, than if it fits your design, you can use the Function's 'return' instead):

    game.max_integer_attribute = max_integer_variable
    game.min_integer_attribute = min_integer_variable

  ]]>

</function>

if interested in this stuff, google search:

programming finding min max value algorithms
programming searching algorithms
linear search
binary search
programming sorting algorithms
big o notation


if you want to actually sort the values (min to max or max to min), it's pretty much the exact same algorithm, except you'll be placing them into a new list, though you'll have to do a bit of creative thinking on how to do it.


@KV Yeah.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums