在C中交换多维阵列
我正在为基本蜂窝自动机制作代码,并试图使其更有效。我的目标之一是将其与同一数组中的生命蜂窝自动机游戏结合起来,根据基本Wolfram规则计算一半的值,而另一个值则是Conway规则。我已经用两个不同的逻辑分别完成了它。因此,为了实现我的最终目标,我正在执行2D数组,首先将其设置为生成底部的新的基本CA,将其他所有行移动并将顶行发送到游戏中Life CA到目前为止,这是我的代码:
#define Y 7
#define X 15
int main()
{
int grid[Y][X]={0}, newgrid[Y][X], wr=30, rules[8];
grid [0][X/2]=1;
int (*g)[X] = grid,(*newg)[X] = newgrid;
to_binary(&wr,rules); //Transform rule integer into binary number array
for(int k=0;k<10s;++k)
{
for(int i = Y-1;i>=0;--i)
{
for(int j = 0;j<X;++j)
{
printf("%d",*(g[i]+j));
if(!i){*(newg[i]+j)=check_cell(j,g[i],rules);} //check cell neighborhood and rules to evolve it
else{*(newg[i]+j)=*(g[i-1]+j);} //here is my question
}
printf("\n");
}
//swapping for efficiency (instead of iterating again)
int (*swap)[X] = g;
g = newg;
newg = swap;
printf("\n");
}
return 0;
}
我要做的是,而不是将每个元素复制到NewGrid,而是按行复制,这是我尝试的(仅修改了部分):
for(int k=0;k<Y;++k)
{
for(int i = Y-1;i>=0;--i)
{
for(int j = 0;j<X;++j)
{
printf("%d",*(g[i]+j));
if(!i){*(newg[i]+j)=check_cell(j,g[i],rules);}
}
printf("\n");
if(i>0){*newg[i]=*g[i-1];} //trying to reassign row by row, instead of cell by cell
}
int (*swap)[X] = g;
g = newg;
newg = swap;
printf("\n");
}
但是,这得到了错误的内存访问,无法正确完成工作。有没有办法正确地进行操作,像newg [i] = g [i-1]这样的行重新分配行?
此外,还有更有效的编写此代码的方法吗?最后,我将使用SDL2进行图形。提前致谢
I'm doing a code for Elementary Cellular Automata and trying to get it more efficient. One of my goals is to combine it with the Game of Life Cellular Automata in the same array, computing one half values by the Elementary Wolfram rules, and the other by the Conway rules. I've already done it separately with two different logic. Therefore, to accomplish my final goal, I'm doing a 2D array and, first, setting it to generate the new row of Elementary C.A. on the bottom of it, moving everything else one row up and sending the top row to the Game of Life C.A. This is my code so far:
#define Y 7
#define X 15
int main()
{
int grid[Y][X]={0}, newgrid[Y][X], wr=30, rules[8];
grid [0][X/2]=1;
int (*g)[X] = grid,(*newg)[X] = newgrid;
to_binary(&wr,rules); //Transform rule integer into binary number array
for(int k=0;k<10s;++k)
{
for(int i = Y-1;i>=0;--i)
{
for(int j = 0;j<X;++j)
{
printf("%d",*(g[i]+j));
if(!i){*(newg[i]+j)=check_cell(j,g[i],rules);} //check cell neighborhood and rules to evolve it
else{*(newg[i]+j)=*(g[i-1]+j);} //here is my question
}
printf("\n");
}
//swapping for efficiency (instead of iterating again)
int (*swap)[X] = g;
g = newg;
newg = swap;
printf("\n");
}
return 0;
}
What I was trying to do was, instead of copying every element to the newgrid, copy line by line, and this is what I've tried (only the modified part):
for(int k=0;k<Y;++k)
{
for(int i = Y-1;i>=0;--i)
{
for(int j = 0;j<X;++j)
{
printf("%d",*(g[i]+j));
if(!i){*(newg[i]+j)=check_cell(j,g[i],rules);}
}
printf("\n");
if(i>0){*newg[i]=*g[i-1];} //trying to reassign row by row, instead of cell by cell
}
int (*swap)[X] = g;
g = newg;
newg = swap;
printf("\n");
}
But this gets the wrong memory access and does not finish the job properly. Is there a way to do it correctly, reassigning row by row like newg[i]=g[i-1]?
Furthermore, is there any more efficient way to write this code? In the end, I'm going to get it graphic with SDL2. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论