0

I have an array with 255 quadruples as shown below.

With each iteration of i I want to pass (correct terminology?) the first three values of each quadruple into a function (the three ? in getColourDistance below) in order to return the result of a calculation.

How must this be done in the C++ variant for Arduino?

Thanks!

const int SAMPLES[][4] ={{2223, 1612,  930,  10}, {1855,  814,  530,  20}, {1225,  463,  438,  30}, {1306,  504,  552,  40}, ...};

byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]);

for (byte i = 0; i < samplesCount; i++)
{
  tcs.getRawData(&r, &g, &b, &c);
  colourDistance = getColourDistance(r, g, b, ?, ?, ?);
  // do something based on the value of colourDistance
}

int getColourDistance(int sensorR, int sensorG, int sensorB, int sampleR, int sampleG, int sampleB)
{
  return sqrt(pow(sensorR - sampleR, 2) + pow(sensorG - sampleG, 2) + pow(sensorB - sampleB, 2));
}
6
  • 5
    Hint: SAMPLES[n][0] gives you the first element of the nth row. Commented Jul 11, 2019 at 12:32
  • Ah, thanks, so, inside the for-loop, I declare three local variables and in a nested for-loop iterate three times to get at SAMPLES[i][0], SAMPLES[i][1], SAMPLES[i][2] if I understand your hint correctly? Commented Jul 11, 2019 at 12:43
  • 1
    Yes, except you don't need the local variables or a nested for loop. You can just use getColourDistance(r, g, b, SAMPLES[i][0], SAMPLES[i][1], SAMPLES[i][2]) Commented Jul 11, 2019 at 12:49
  • Thanks, Nathan, I did not know that this was "allowed". I thought this gets into the area of pointers, etc. Commented Jul 11, 2019 at 12:53
  • 1
    It's perfectly fine and used all of the time. Not to sound rude but it sounds like you could use a good C++ book. Commented Jul 11, 2019 at 12:56

1 Answer 1

2

In this case array SAMPLES can be considered as a 2-dimensional array , thus SAMPLES[0][0] , will give the 1st element of 1st 1-dimensional array of SAMPLES , SAMPLES[0][1], will give the 2nd element of 1st 1-d array of SAMPLES , and so on , considering this terminology in mind we can do ,

#include <iostream>

const int SAMPLES[][4] = {{2223, 1612, 930, 10}, {1855, 814, 530, 20}, {1225, 463, 438, 30}, {1306, 504, 552, 40}, ...};

byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]);

for (byte i = 0; i < samplesCount; i++)
{
    //taking values of r,g,b as before    
    a=SAMPLES[i][0];//getting values of r,g,b 
    b=SAMPLES[i][1];//using the knowledge that SAMPLES[i][j]
    c=SAMPLES[i][2];//denotes jth element of ith 1-d array of SAMPLES
    colourDistance = getColourDistance(r, g, b, a, b, c);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, yes, that's akin to what user Nathan pointed me to above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.