首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将两个不同数组中的数字组合成一个数组?

如何将两个不同数组中的数字组合成一个数组?
EN

Stack Overflow用户
提问于 2019-04-16 01:33:29
回答 1查看 60关注 0票数 0

我有一个生成随机列数= numCols和随机行数= numRows的代码。nomRows和numCols = numCells的倍数。我想用不同的颜色给每个单元格着色,我知道我有多少个单元格,因为那是numCells。因此,我有一个数组“颜色”,它有6个值。该数组中的每个数字都表示该颜色在该数组中出现的次数。我有一个循环,它为每种颜色生成一个随机数,但始终确保数组中数字的总和永远不会大于NumCells。你不能总共有23个颜色值,而只有10个单元格可以容纳它。现在这样做很好,数组中的总数永远不会大于numCells。

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

但是我不能让它工作,或者我得到一个错误:数组下标的类型'int 8int 6]‘无效,我猜arduino不会很高兴,因为我只想添加6个数字。

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

请问如何将colors[]推送到mysoun2[]中,并再添加2个随机数?

// CODE FOR COLORS
``` int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0
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);
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55694524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档