簡體   English   中英

將2D數組中的值傳遞給函數

[英]Passing values from 2D array into a function

我有一個具有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視為二維數組,因此SAMPLES [0] [0]將給出第1個1維數組的第1個元素,SAMPLES [0] [1]將給出第2個第一個1維樣本數組的元素,以此類推,考慮到我們可以做到的這一術語,

#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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM