a for loop with multiple conditions

Is there a way to condense this code into 1 or 2 nested for loops? The array converted sometimes contains repetitions that I have to discount before adding the array elements to obtain a single ID for their source. Thank you in advance.

if (converted[0] != converted [1] &&
converted[0] != converted [2] &&
converted[0] != converted [3] &&
converted[0] != converted [4] &&
converted[0] != converted [5])
{currentChord +=converted[0];}

if (converted[1] != converted [2] &&
converted[1] != converted [3] &&
converted[1] != converted [4] &&
converted[1] != converted [5])
{currentChord +=converted[1];}

if (converted[2] != converted [3] &&
converted[2] != converted [4] &&
converted[2] != converted [5])
{currentChord +=converted[2];}

if (converted[3] != converted [4] &&
converted[3] != converted [5])
{currentChord +=converted[3];}

if (converted[4] != converted [5])
{currentChord +=converted[4];}

current Chord=+converted [5];

something like this:

  bool matched = false;
  for (int i = 1, i < 6; i++)
  {
    if (converted[0] == converted[i])
    {
      matched = true;
      break;
    }
  }
  if (matched) currentChord +=converted[0];

BulldogLowell, I see a double loop. I can't make a shortcut for a smart solution with a single loop, it is a double loop.

// For: http://forum.arduino.cc/index.php?topic=373368.0

#define NUM 6
int converted[NUM] = { 10, 23, 48, 102, 183, 201 };

void setup()
{
  Serial.begin( 9600);
  Serial.println(F( "Started"));

  int currentChord = 0;
  for( int i=0; i<NUM; i++)
  {
    boolean same_number = false;
    for( int j=i+1; j<NUM; j++)
    {
      if( converted[i] == converted[j])
      {
        same_number = true;
        break;
      }
    }
    if( !same_number)
    {
      currentChord += converted[i];
    }
  }
  Serial.println( currentChord);
}

void loop()
{
}

The last one "currentChord += converted [5]" is also executed, because 'j' will be '6'. For number '6' the 'j'-loop is not run and the converted [5] is added to currentChord.

I would code it like this (assuming bytes for the table and int for the sum)

int sumChord(byte* toSum, byte elements)
{
  int currentChord = 0;
  for (byte toAdd = 0; toAdd < elements; toAdd++)
  {
    byte thisElm = toSum[toAdd];
    for (byte toTest = toAdd + 1; toTest < elements; toTest++)
    {
      if (toSum[toTest] == toSum[toAdd])
      {
        thisElm = 0;
        break;
      }
    }
    currentChord += thisElm;
  }
  return currentChord;
}

byte converted[] { 0x10, 0x20, 0x40, 0x80, 0x20, 0x80, 0x01, 0x01, 0x02, 0x80, 0x04, 0x04, 0x80, 0x40, 0x08 };

void setup()
{
  Serial.begin(115200);
  Serial.print(F("Sum is "));
  Serial.print(sumChord(converted, sizeof(converted)), HEX);
}
void loop() {}

Koepel:
BulldogLowell, I see a double loop. I can't make a shortcut for a smart solution with a single loop, it is a double loop.

mine was an approach to get OP started... I'm too lazy to take it over the goal line today

:wink:

@peterbradley

Are the numbers in converted[] array in monotone non decreasing order a.k.a. sorted?

if so, the code would become a single loop

for (int i=0; i< 6; i++)
{
  if (converted[i] < converted [i+1]) currentChord +=converted[i];
}

Think again. The [ i + 1 ] for index 5 will test against something outside the array.
You would have to add an extra seventh number with a very high value to the array to make this work.

@robtillaart,

No, but I might be able to rewrite other sections of the program to make that work. The elements of converted are always 2 raised to a power between 1 & 12.

A typical example would be {2^5, 2^8, 2^1, 2^5, 0} or {2^4, 2^8, 2^1, 2^4, 2^8). Data type is float.

Thanks for your help everyone!

A typical example would be {2^5, 2^8, 2^1, 2^5, 0} or {2^4, 2^8, 2^1, 2^4, 2^8). Data type is float.

An integer value raised to an integer value and then stored in a float. Why?

Probably no good reason. I haven't really looked into how to cast.

converted is generated by consulting an array of floats that looks like {1, 1.067, 1.125, 1.2, 1.25, 1.33, 1.5, 1.4, 1.6, 1.7, 1.8, 1.875}. The index value of a float in play becomes the exponent.

When I declared converted as an array of ints, I got a weird error from the compiler about being out of registers.

I got a weird error from the compiler about being out of registers.

So, you fooled it by creating a bunch of additional work for it to do (and for the Arduino to do each and every time it access the array), instead of fixing the problem. I see...

Are you trying to do musical intervals? In that case the exponent is divided by 12. So converted seems to have values of 2n/12. Are your arrays intended to contain that kind of number?