我被一段 Arduino 代码困住了
基本上我有一个多维数组,其中每个索引将存储一个随机字母。但经过一小段时间(例如 900 毫秒)后,索引中的每个值都会向上移动到其上方的索引。当然,当指数达到例如[9][0] 它将转到最大的索引,例如 [9][9]
例如:myArray[9][8] 包含“K”,myArray[9][7] 包含“L”
myArray[9][8] 中的“K”将放入 myArray[9][7] 中,myArray[9][7] 中的“L”将放入 myArray[9][6] 等.等
但我不知道如何在 C 中做到这一点...... myArray[9][8] = myArray[9][7] 显然行不通
最佳答案
赋值没有问题,但必须先保存正在写入的元素。
在此示例中,所有重写的值都保留在每行的最后一个槽中,这只是一个示例 - 不是唯一的方法,如果您需要跨行移动字母,那么想法是相似的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void moveLettersInRow(char sArr[][3], int row)
{
int i = 0;
int j = 0;
char temp ;
for (i = 0; i < row; ++i)
{
for(j = 2 ;j > 0;--j)
{
temp = sArr[i][j-1]; /*save the value to be rewritten */
sArr[i][j-1]= sArr[i][2]; /* set the last val in its new place*/
sArr[i][2]= temp; /* set the last elem to the latest rewritten value*/
}
}
}
void printArr(char arr[][3], int size)
{
int i = 0, j= 0;
for(i = 0; i< size; ++i)
{
for(j = 0; j< 3; ++j)
{
printf("%c | ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
char arr[2][3]= {{'a','b','c'},{'a','b','c'}};
moveLettersInRow(arr, 2);
printArr(arr, 2);
return 0;
}
关于c - Arduino C 多维数组切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020958/