Array content: fastest means to verify whether all variables contain an identical value?

The contents of an array is to be used to execute an action on condition that all variables included do contain an identical value, ie for example int = 0.
What is the fastest way to do this?

Thanks!
Erik

Drop out of the loop as soon as you find any value that isn't the same.

If you said more about why you need to do this, where the contents are coming from and so forth it is quite possible there is a better way to solve what is really the problem.

Unless it’s just a weird homework assignment. :expressionless:

a7

1 Like

Worded like a school question ….

...and may not even be an Arduino question.

The issue is simply nothing else but a case of determining, at one instance in the program, whether the values in an array all are equal to 0.
Apart from using a "for" to extract these values, is there any other way?

Can you do it as you populate the array?

1 Like

So now it's only for zero? That's not what you originally said...

Hi
multiply the value of the first cell by the number of cells,
then subtract the value of each cell from the total.
If the final result is zero, all cells are of equal value.

0 25
1 25
2 25
3 25
4 25
5 25

6 x 25 = 150
150 [0] - [1] - [2] - [3] - [4] - [5] = 0

or

0 25
1 25
2 30
3 25
4 25
5 25

6 x 25 = 150
150 [0] - [1] - [2] - [3] - [4] - [5] = 5 -5

Multiplication may slow things down on some machines, so an iterative solution may be best.

It's also extremely wasteful if the first two values are different. :slight_smile:

Most programming questions on this board could equally easily be handled on a generic C/C++ StackOverflow site.

Yes, but they aren't as forgiving on that site. :slight_smile:

You ever want to get your eyebrows singed off, go ask a question on avrfreaks.net that they don't think lives up to their standards. Man, there are some toxic personalities over there.

2 Likes

But why? Or is it just something you're curious about?

I often have pondered sending some of the more annoying posters over there, but I am fundamentally opposed to blood-sports.

1 Like

I'm probably going to screw something up, but I want this thread to end soon!

int array[] = {1,2,3,4,5,66,}; // whatever
const int length = sizeof(array)/sizeof(array[0]));

bool equal = true;
for (int i = 1; i < length; i++)
{
  if (array[0] != array[i])
  {
    equal = false;
    break;
  }
}
// if equal == true, then all values are the same
1 Like

Maintain an array of flags equal in size to your original array.

Whenever you change a value in the original array, if it is zero, clear the corresponding flag in the associate array, similarly if it isn’t zero, set the flag.

To see if all the original array values are zero, scan the flag array and count how many flags are set.

If the count is zero, then all the original array values are zero.

a7

Nope.

0 25
1 25
2 30
3 25
4 20
5 25

6 x 25 = 150
150 [0] - [1] - [2] - [3] - [4] - [5] = 0
... but the values aren't all equal, so that doesn't work.

1 Like

If you have access to the standard library:

#include <algorithm>
#include <iterator>

int main() {
    int values[] {0, 0, 0, 0, 42};
    auto is_zero = [&](int value) { return value == 0; };
    bool all_zero = std::all_of(std::begin(values), std::end(values), is_zero);
    return all_zero; // false
}

is_zero is a predicate that returns true if the given value is zero.

The call to std::all_of(...) returns whether all elements in the array (from the beginning of the array to the end) match the predicate, i.e. are equal to zero.
It will short-circuit once it finds an element that doesn't equal zero.


But the fastest way is to not scan the array at all, just change the logic of your program so you don't have to. In most cases you are the one writing to the array, so you should already know whether all entries are zero or not.

Why?
If you're going to subtract all values, you're already doing at least as much work as just comparing each value to the target.

1 Like
{
  boolean all_equal = true;
  for (size_t i=1; i < sizeof array / sizeof array[0]; i++)
  {
    if (array[i] != array[0])
    {
      all_equal = false;
      break;
    }
  }

  if (all_equal)
    do_something();
}
1 Like