【问题标题】:How to combine numbers from 2 different arrays into just one array?如何将 2 个不同数组中的数字组合成一个数组?
【发布时间】:2019-04-15 17:33:29
【问题描述】:

我有一个代码,我在其中生成随机数列 = numCols 和随机数行数 = numRows。 nomRows 和 numCols 的倍数 = numCells。我想用不同的颜色为每个单元格着色,我知道我拥有的单元格总数,因为那是 numCells。因此我有一个数组“颜色”,它有 6 个值。该数组中的每个数字代表我在该数组中有多少次该颜色。我有一个循环,它为每种颜色生成一个随机数,但始终确保数组中的数字总和永远不会大于 NumCells。您不能总共有 23 个颜色值,并且只有 10 个单元格适合它。现在这工作正常,数组中的总数永远不会大于 numCells。

所以我有一个包含 6 个数字的数组 colors[],我想将该数组推入 mysound2[] 并在推入它之后创建另一个循环,该循环将在该数组中添加另外 2 个数字以最终得到一个数组mysound2[] 包含 8 个数字的总数。

但我无法让它工作,要么我得到一个错误:数组下标的无效类型'int [8] [int [6]]'我猜arduino不喜欢8个数组我只是尝试添加 6 个数字。

或者,如果我尝试其他方法,代码会生成 0,0,0。

请问如何将颜色[] 推入 mysoun2[] 并添加 2 个随机数?

// CODE FOR COLORS
``` int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0
``` int AnalogPin1 = A1; //Declare an integer variable, hooked up to analog pin 1

void setup() {
  Serial.begin(9600); //Begin Serial Communication with a baud rate of 9600
  randomSeed(analogRead(0));

}

void loop() {
// 1)Gray, 2)White, 3)Yellow, 4)Blue, 5)Black, 6)Red

int numCols1  = random(1,4);
int numCols2  = random(2,5);
int numCols   = numCols1 + numCols2;

int numRows1  = random(2,5);
int numRows2  = random(2,6);
int numRows   = numRows1 + numRows2;

int numCells  = numCols * numRows;
int colors[]  = {5,3,2,1,0,0};
int numAlloc  = colors[0] + colors[1] + colors[2] + colors[3] + colors[4] + colors[5];


for (int i=0; i<numCells - numAlloc; i++)
{
  int color = random(0,7);
  color[colors]++;
}



  /*The Serial.print() function does not execute a "return" or a space
      Also, the "," character is essential for parsing the values,
      The comma is not necessary after the last variable.*/
Serial.print(colors [0]);     
Serial.print(",");
Serial.print(numCols);
Serial.print(",");
Serial.print(numRows);
Serial.print(",");
//Serial.print(numCells);
//Serial.print(",");
Serial.println();
delay(5000); // For illustration purposes only. This will slow down your program if not removed 
}


//CODE FOR ADDING 8 NUMBERS INTO ONE ARRAY WITH 2 LOOPS
# define Parameters 8
int mysound2[Parameters];
int randNumber =0;

void setup() {
   Serial.begin(9600);
     randomSeed(analogRead(0));
    for (int thisPin = 0; thisPin < Parameters-2; thisPin++)  {

    randNumber = random(1, 100); // generates between 0 and 127

    mysound2[thisPin]= randNumber;   

  }
    for (int thisPin = Parameters-2; thisPin < Parameters; thisPin++)  {

    randNumber = random(100, 200); // generates between 0 and 127

    mysound2[thisPin]= randNumber;   

  }
}

void loop() {
    for (int thisPin = 0; thisPin < Parameters; thisPin++)  {
     Serial.println(mysound2[thisPin]);  
     delay(5000);
}
}


//ME TRYING TO COMBINE THE TWO CODES
// 1)Gray, 2)White, 3)Yellow, 4)Blue, 5)Black, 6)Red

# define Parameters 8

int numCols1  = random(1,4);
int numCols2  = random(2,5);
int numCols   = numCols1 + numCols2;

int numRows1  = random(2,5);
int numRows2  = random(2,6);
int numRows   = numRows1 + numRows2;

int numCells  = numCols * numRows;
int colors[]  = {5,3,2,1,0,0};
int numAlloc  = colors[0] + colors[1] + colors[2] + colors[3] + colors[4] + colors[5];
int color     = 0;

int mysound1[Parameters];
int randNumber1 =0;

void setup() {
   Serial.begin(9600);
     randomSeed(analogRead(0));
    for (int thisPin = 0; thisPin < Parameters-2; thisPin++)  
    {
      for (int i=0; i<numCells - numAlloc; i++)
      {
        int color = random(0,6);
        color[colors]++;
        }
       mysound1[color];
      }

  for (int thisPin = Parameters-2; thisPin < Parameters; thisPin++)  {

    randNumber1 = random(100, 200); // generates between 0 and 127

    mysound1[thisPin]= randNumber1;   
  }
}

void loop() {
    for (int thisPin = 0; thisPin < Parameters; thisPin++)  {
     Serial.println(mysound1[color]);  
     delay(2000);
    }
}

【问题讨论】:

  • 我发现的唯一明显问题是,在几个地方,您使用color[colors] 而不是colors[color]color 被声明为 intcolors 被声明为 ints 的数组。
  • 哦,很好,但是即使我改变了它。我希望状态mysound1[colors]; 会将6 个数字推入该数组。我仍然收到此错误:数组下标的无效类型'int [8] [int [6]]'。我知道 Arduino 不喜欢我只是想把 6 个数字推到应该是 8 个数字的地方。但后来我想我在第一个 for 循环中通过声明这个来处理这个问题:for (int thisPin = 0; thisPin &lt; Parameters-2; thisPin++)

标签: arrays for-loop random arduino arduino-c++


【解决方案1】:

我了解到您想将一个数组复制到另一个数组中。你可以用这样的函数来做到这一点:

void copyArray(int* source, int* destination, int length) {
    for (int i = 0; i < length; i++) {
        *destination++ = *source++;
    }
}

所以,如果你想将colors中的6个元素复制到mysound2中, copyArray(colors, mysound2, 6); 会这样做。

如果您想再添加 2 个元素,只需执行以下操作。

mysound2[6] = randNumber1;
mysound2[7] = randNumber2;

编辑:在评论部分,您提到要将两个数组合并为一个。像这样的函数可以做到这一点。

void copyArray(int* source, int* destination, int destStartIndex, int len) {
  for (int i = 0; i < len; i++) {
        destination[destStartIndex + i] = *source++;
    }
}

下面的代码会将colors复制到mysound2的前6个字节,并将thisPin复制到mysound2的后2个字节。

copyArray(colors, mysound2, 0, 6);
copyArray(thisPin, mysound2, 6, 2);

【讨论】:

  • 我没有正确描述我的问题。我有一个数组mysound2[],总共应该有 8 个数字。我有一个数组colors[],它有 6 个数字。在我将这 6 个推入 mysound2[] 之后,我必须再创建 2 个随机数来添加到这个数组中,所以它总共有 8 个。我尝试使用 for 循环,或者它可以是第二个数组,它产生这两个数字,不在乎。只是不知道如何组合它们。
  • 非常感谢您的帮助。我设法让它工作了!
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 2016-07-20
  • 2021-04-15
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多