将值从 2D 数组传递到函数中
Passing values from 2D array into a function
我有一个包含 255 个四倍的数组,如下所示。
在 i 的每次迭代中,我想将每个四元组的前三个值传递到一个函数中(下面的 getColorDistance 中的三个值(,以便返回计算结果。
在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 可以被认为是一个二维数组,因此 SAMPLES[0][0] ,将给出 SAMPLES 的第一维数组的第一个元素,SAMPLES[
0][1],将给出 SAMPLES 的第一个一维数组的第 2 个元素,依此类推,考虑到这个术语我们可以做,
#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);
}
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中