Array related question

Started by markbilly, Thu 09/06/2011 22:15:59

Previous topic - Next topic

markbilly

Is there a way of checking if all array elements are the same?

i.e. ask the condition "Is every element in the String array questions the same value?" but in code.

I had no idea where to ask this question, as it's basic programming more than scripting, so apologies if I've broken the rules.

Cheers,
Mark
 

Calin Leafshade

Use a pen and paper.

no actually like this:

Code: ags

String array[ARRAYSIZE];

String test = array[0];
bool areSame = true;
int i = 0;
while (i < ARRAYSIZE){
  if (test != array[i]) {
       areSame = false;
       i = ARRAYSIZE + 1;
  }
  i ++;

}

// check areSame here


markbilly

Yay, thanks Mr. Leafshade!

And sorry about the pen and paper comment - caught me on a bad day! ;)
 

Ryan Timothy B

It (i) should start at 1 since you already know the 0 index is the same as the 0 index. :P

monkey0506

#4
Well since we're analyzing Calin's code, it's also not necessary to set i to "ARRAYSIZE + 1" if the test fails and breaking out of the loop..given that ARRAYSIZE is never < ARRAYSIZE, which is the condition for the loop, and i is already incremented once more anyway at the end of the final iteration. So with Calin's code there if every element is the same then after the loop i will always be equal to ARRAYSIZE, otherwise i will always be equal to ARRAYSIZE+2, essentially meaning that the areSame variable is unnecessary fluff in itself. But hey, who's counting? :P

Being the proponent of dynamic arrays that I am, I'd also like to point out that if you were to use dynamic arrays instead of static arrays you could write a generic function for this, in case you need it again.

Code: ags
String questions[];

// game_start, or wherever you need it
  questions = new String[ARRAYSIZE];

bool AreStringElementsSame(String array[], int arraySize)
{
  if ((array == null) || (arraySize <= 0)) return false;
  if (arraySize > 1000000) arraySize = 1000000;
  String buffer = array[0];
  int i = 0;
  while (i < arraySize)
  {
    if (array[i] != buffer) return false;
    i++;
  }
  return true;
}

// check elements of questions array
  if (AreStringElementsSame(questions, ARRAYSIZE))
  {
    // ...
  }


Of course a single while loop would be more efficient if you're only ever using this for a single array.

Calin Leafshade

Setting i to 0 was more out of habit than anything else and having a bool to check is more easily readable for an example.

markbilly

I only need to use this check once in the whole script and it is almost certain that it will also only run once in any given game session, so Calin's little bit of code is perfect.

Also, areSame may be pointless, granted. But it makes the code easily understandable, should I take a break from development and forget what I was doing...

@monkey_05_06:

Yeah, I tried writing a function for it and got a couple of compiler errors. Reading your post, I now know this is because I needed a dynamic array. Very useful, so thanks :)
 

SMF spam blocked by CleanTalk