我有一个代码,我生成随机数Colums = numCols和随机数Rows = numRows。 nomRows和numCols = numCells的倍数。我想用不同的颜色为每个Cell着色,我知道我拥有的Cell的总数,因为那是numCells。因此我有一个数组“颜色”,它有6个值。该数组中的每个数字代表该数组中该颜色的次数和次数。我有一个循环,为每种颜色生成一个随机数,但始终确保数组中的数字总数不会大于NumCells。你不能有23个颜色值,只有10个单元格适合它。现在这个工作正常,数组中的总数大小永远不会大于numCells。
所以我有一个数组颜色[],其中包含6个数字,我想将该数组推入mysound2 []并在我推动之后创建一个nother循环,它将在该数组中再添加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);
}
}
我知道你想将一个数组复制到另一个数组中。你可以用这样的函数做到这一点:
void copyArray(int* source, int* destination, int length) {
for (int i = 0; i < length; i++) {
*destination++ = *source++;
}
}
因此,如果你想将qazxsw poi中的6个元素复制到qazxsw poi中,qazxsw poi会这样做。
如果你想再添加2个元素,可以这样做。
colors
编辑:在评论部分,您提到要将两个数组合并为一个。像这样的函数会这样做。
mysound2
下面的代码将copyArray(colors, mysound2, 6);
复制到mysound2[6] = randNumber1;
mysound2[7] = randNumber2;
的前6个字节,并将void copyArray(int* source, int* destination, int destStartIndex, int len) {
for (int i = 0; i < len; i++) {
destination[destStartIndex + i] = *source++;
}
}
复制到colors
的以下2个字节。
mysound2