【发布时间】:2019-07-11 12:30:41
【问题描述】:
我有一个包含 255 个四元组的数组,如下所示。
对于 i 的每次迭代,我想将(正确的术语?)每个四元组的前三个值传递给一个函数(下面 getColourDistance 中的三个 ?),以便返回计算结果。
如何在 Arduino 的 C++ 变体中完成此操作?
谢谢!
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));
}
【问题讨论】:
-
提示:
SAMPLES[n][0]为您提供第 n 行的第一个元素。 -
啊,谢谢,所以,在 for 循环中,我声明了三个局部变量,并在嵌套的 for 循环中迭代 3 次以到达
SAMPLES[i][0], SAMPLES[i][1], SAMPLES[i][2],如果我正确理解您的提示?跨度> -
是的,除非您不需要局部变量或嵌套的 for 循环。你可以使用
getColourDistance(r, g, b, SAMPLES[i][0], SAMPLES[i][1], SAMPLES[i][2]) -
谢谢,内森,我不知道这是“允许的”。我认为这涉及指针等领域。
-
非常好,一直在使用。听起来不粗鲁,但听起来你可以使用good C++ book。
标签: c++ function multidimensional-array arduino