我試圖增加一些數組值:遞增數組值 - 的Arduino
int counter[] = {0,0,0,0,0,0,0,0};
如果數字的位置0的值達到25,然後在1位的值被加1,並且位置0重置爲0.依此類推 - 當索引位置2達到25時,它將位置3增加1,並將其自己的值重置爲0.
我正在做一些base26增量 - 生成所有字母組合給定數量的字母。理想情況下,我希望這可以無限工作(理論上) - 當最後一個值達到25時,會添加一個新的數組索引。
我正在處理上一個問題涉及的項目 - 可能會清理我「M試圖做:Every permutation of the alphabet up to 29 characters?
下面是我在一分鐘代碼:
// Set the variables.
String neologism;
int counter[] = {0,0,0,0,0,0,0,0};
String base26[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
void setup() {
// Initialize serial communication:
Serial.begin(9600);
}
void loop() {
int i = 0;
// Reset or increment the counter.
if (counter[i] == 25) {
counter[i] = 0;
counter[i+1]++;
}
else {
counter[i]++;
}
neologism = letters(counter[i]);
Serial.print(neologism+'\n');
delay(100);
i++;
if(i>7) {
i=0;
}
}
String letters(int counter) {
String newword;
for(int i=0; i <= 7; i++) {
newword += base26[counter];
}
return newword;
}
對不起,我不知道你這個問題就在這裏。 – 2011-05-08 20:10:23