Check if array is missing values

Hi all, I need a bit of a pointer on some code.

I have a 2D array like so:

int digits[10][29] = {
{24,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23},
{8,8,9,10,11,12,13,14,15}, // 1
{20,4,5,6,7,8,9,10,11,16,17,18,19,20,21,22,23,24,25,26,27}, // 2
{20,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24,25,26,27}, // 3
{16,0,1,2,3,8,9,10,11,12,13,14,15,24,25,26,27}, // 4
{20,0,1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,24,25,26,27}, // 5
{24,0,1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}, // 6
{12,4,5,6,7,8,9,10,11,12,13,14,15}, // 7
{28,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}, // 8
{24,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24,25,26,27}// 9
};

The first number in each array defines how many elements that array has, followed by some other values.

What I need to do is look through each of those arrays, list any values <28 that don’t exist.

Eg look at the last array:

{24,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24,25,26,27}// 9

As you can see, it contains all values from 0-27 except 20,21,22 and 23, so I’d like it to tell me those numbers. Possible?

Cheers

I have a 2D array like so:

An array whose values are all bytes, stored in an int array. What a waste of space.

Possible?

Of course. A for loop running from 0 to 28, where the index is the value to look for in the array. It either exists or it doesn't. You'll need, of course, an inner for loop from 0 to the number of elements in the array.

PaulS:
Of course. A for loop running from 0 to 28, where the index is the value to look for in the array. It either exists or it doesn’t. You’ll need, of course, an inner for loop from 0 to the number of elements in the array.

This works fine until it gets to a point where numbers are missing in the middle of the array, as it then offsets the value?

  for (int i = 0; i<28; i++){
    for (int k = 1; k<digits[num][0]; k++){
      if (digits[num][k] != i) {
        Serial.print("Array is missing ");
        Serial.print(i);
        Serial.println();
    }
  }
  }

Doesn’t seem to work.

Things:
What I need to do is look through each of those arrays, list any values <28 that don’t exist.

That can be done with a FOR loop to iterate through all the values you want to check, enclosing another FOR loop which iterates through each element of the array testing whether it contains the value being checked.

ETA: In other words, just like PaulS said.

You need to scan the whole array, looking for i, unless you assume that the array is in order. When you find i, you can note that, and quit looking.

Boolean variable usage is in your (near) future.

there are a number of assumptions

  • sorted
  • unique values
  • distance 1
  • first and last element is always OK

If you do not want to see the answer do not scroll the code box

int digits[10][29] = {
{24,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23},
{8,8,9,10,11,12,13,14,15}, // 1
{20,4,5,6,7,8,9,10,11,16,17,18,19,20,21,22,23,24,25,26,27}, // 2
{20,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24,25,26,27}, // 3
{16,0,1,2,3,8,9,10,11,12,13,14,15,24,25,26,27}, // 4
{20,0,1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,24,25,26,27}, // 5
{24,0,1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}, // 6
{12,4,5,6,7,8,9,10,11,12,13,14,15}, // 7
{28,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}, // 8
{24,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24,25,26,27}// 9
};

void printMissing(int n)
{
  Serial.print("row: ");
  Serial.print(n);
  
  int len = digits[n][0];
  int expected = digits[n][1];
  for (int idx = 2; idx < len;)
  {
    expected++;
    if (digits[n][idx] == expected) idx++;  // take next element from array
    else 
    {
      Serial.print(" ");
      Serial.print(expected);
    }
  }
  Serial.println();
}
void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");

  for (int row = 0; row < 10; row++)
  {
    printMissing(row);
  }
}

void loop() 
{
}