C 动态二维矩阵实现
在我的静态矩阵中,我试图得到2d动态矩阵 我想做的就是更改init函数,而不是使用定义的高度和宽度 这将是动态的-请告诉我怎么做C 动态二维矩阵实现,c,C,在我的静态矩阵中,我试图得到2d动态矩阵 我想做的就是更改init函数,而不是使用定义的高度和宽度 这将是动态的-请告诉我怎么做 void init(int board[][WIDTH], int rows) { int x, y; for (y = 0; y < rows; y++) for (x = 0; x < WIDTH; x++) board[y][x] = 0; /* Scatter some live ce
void init(int board[][WIDTH], int rows) {
int x, y;
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
board[y][x] = 0;
/* Scatter some live cells: */
board[10][25] = 1;
board[10][26] = 1;
board[10][27] = 1;
board[11][25] = 1;
board[12][26] = 1;
}
int main(void) {
int board[HEIGHT][WIDTH];
init(board, HEIGHT);
..
..
}
这是我的全部代码:
#include <stdio.h>
#define WIDTH 50
#define HEIGHT 20
void init(int board[][WIDTH], int rows) {
int x, y;
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
board[y][x] = 0;
/* Scatter some live cells: */
board[10][25] = 1;
board[10][26] = 1;
board[10][27] = 1;
board[11][25] = 1;
board[12][26] = 1;
}
void print(int board[][WIDTH], int rows, int cols)
{
int x, y;
char c;
for (y = 0; y < rows; y++) {
for (x = 0; x < cols; x++) {
if (board[y][x] == 1)
printf("X");
else
printf(" ");
}
printf("\n");
}
printf("Press any key to continue:\n");
getchar();
}
int count_neighbors(int board[][WIDTH], int rows,
int y, int x)
{
int i, j;
int result = 0;
for (i = -1; i <= 1; i++)
if ((y+i >= 0) && (y+i < rows))
for (j = -1; j <= 1; j++)
if ((x+j >= 0) && (x+j < WIDTH))
if ((i != 0) || (j != 0))
result += board[y+i][x+j];
return result;
}
int step(int board[][WIDTH], int rows) { // now returns a bool
int x, y;
int neighbors[HEIGHT][WIDTH];
int changed = 0; // save changes
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
neighbors[y][x] = count_neighbors(board, rows, y, x);
for (y = 0; y < rows; y++)
for (x = 0; x < WIDTH; x++)
if (board[y][x] == 1) { /* Currently alive */
if (neighbors[y][x] < 2)
{
board[y][x] = 0; /* Death by boredom */
changed = 1; // change happened
}
else if (neighbors[y][x] > 3)
{
board[y][x] = 0; /* Death by overcrowding */
changed = 1; // change happened
}
}
else { /* Currently empty */
if (neighbors[y][x] == 3)
{
board[y][x] = 1;
changed = 1; // change happened
}
}
return changed; // return the status (changed yes/no?)
}
int main(void) {
int board[HEIGHT][WIDTH];
init(board, HEIGHT);
while (1) {
print(board, HEIGHT, WIDTH);
if(step(board, HEIGHT) == 0) // no change
break; // leave the loop
}
return 0;
}
#包括
#定义宽度50
#定义高度20
void init(整块板[][宽度],整块行){
int x,y;
对于(y=0;y3)
{
董事会[y][x]=0;/*因过度拥挤而死亡*/
changed=1;//发生了更改
}
}
else{/*当前为空*/
if(邻域[y][x]==3)
{
董事会[y][x]=1;
changed=1;//发生了更改
}
}
return changed;//返回状态(changed yes/no?)
}
int main(void){
内板[高度][宽度];
初始(板,高度);
而第(1)款{
打印(板、高度、宽度);
如果(台阶(板,高度)=0)//无变化
break;//离开循环
}
返回0;
}
尝试使用struct,下面是我编写的一个简单实现(使用GCC编译,以支持构造函数
属性)
// IntGrid.h
typedef struct intGrid_t {
int **data;
int rows;
int cols;
} *IntGridRef;
struct {
IntGridRef(* create)(int, int);
void (* print)(IntGridRef);
void (* free)(IntGridRef);
int **(* data)(IntGridRef);
int (* rows)(IntGridRef);
int (* cols)(IntGridRef);
} IntGrid;
// IntGrid.c
IntGridRef _intGrid_create(int rows, int cols);
void _intGrid_print(IntGridRef this);
void _intGrid_free(IntGridRef this);
int **_intGrid_data(IntGridRef this);
int _intGrid_rows(IntGridRef this);
int _intGrid_cols(IntGridRef this);
__attribute__((constructor))
static void intGrid_setup()
{
IntGrid.create = _intGrid_create;
IntGrid.print = _intGrid_print;
IntGrid.free = _intGrid_free;
IntGrid.data = _intGrid_data;
IntGrid.rows = _intGrid_rows;
IntGrid.cols = _intGrid_cols;
}
IntGridRef _intGrid_create(int rows, int cols)
{
IntGridRef this = calloc(1, sizeof(struct intGrid_t));
this->rows = rows;
this->cols = cols;
this->data = calloc(rows, sizeof(int *));
for (int i = 0; i < rows; i++) {
this->data[i] = calloc(cols, sizeof(int *));
}
return this;
}
void _intGrid_print(IntGridRef this)
{
printf("{\n");
for (int i = 0; i < this->rows; i++) {
printf(" { ");
for (int j = 0; j < this->cols; j++) {
printf("%i", this->data[i][j]);
if (j != this->cols - 1)
{
printf(", ");
}
}
printf(" }\n");
}
printf("}\n");
}
void _intGrid_free(IntGridRef this)
{
for (int i = 0; i < this->rows; i++) {
free(this->data[i]);
}
free(this->data);
free(this);
}
int **_intGrid_data(IntGridRef this)
{
return this->data;
}
int _intGrid_rows(IntGridRef this)
{
return this->rows;
}
int _intGrid_cols(IntGridRef this)
{
return this->cols;
}
//IntGrid.h
typedef结构intGrid\t{
int**数据;
int行;
int cols;
}*IntGridRef;
结构{
IntGridRef(*创建)(int,int);
无效(*打印)(IntGridRef);
无效(*自由)(IntGridRef);
int**(*数据)(IntGridRef);
int(*行)(IntGridRef);
int(*cols)(IntGridRef);
}IntGrid;
//IntGrid.c
IntGridRef\u intGrid\u create(int行,int列);
void _intGrid _print(IntGridRef this);
void _intGrid _free(IntGridRef this);
int**u intGrid\u数据(IntGridRef this);
int _intGrid_行(IntGridRef this);
int _intGrid _cols(IntGridRef this);
__属性(构造函数)
静态无效intGrid_设置()
{
IntGrid.create=\u IntGrid\u create;
IntGrid.print=\u IntGrid\u print;
IntGrid.free=\u IntGrid\u free;
IntGrid.data=\u IntGrid\u data;
IntGrid.rows=\u IntGrid\u rows;
IntGrid.cols=\u IntGrid\u cols;
}
IntGridRef\u intGrid\u创建(int行,int列)
{
IntGridRef this=calloc(1,sizeof(struct intGrid_t));
这->行=行;
这->cols=cols;
这个->数据=calloc(行,sizeof(int*);
对于(int i=0;i数据[i]=calloc(cols,sizeof(int*);
}
归还这个;
}
void\u intGrid\u打印(IntGridRef this)
{
printf(“{\n”);
对于(inti=0;irows;i++){
printf(“{”);
对于(int j=0;jcols;j++){
printf(“%i”,此->数据[i][j]);
如果(j!=this->cols-1)
{
printf(“,”);
}
}
printf(“}\n”);
}
printf(“}\n”);
}
void\u intGrid\u free(IntGridRef this)
{
对于(inti=0;irows;i++){
免费(此->数据[i]);
}
免费(此->数据);
免费(这个);
}
int**u intGrid\u数据(IntGridRef this)
{
返回此->数据;
}
int\u intGrid\u行(IntGridRef this)
{
返回此->行;
}
int\u intGrid\u cols(IntGridRef this)
{
返回此->cols;
}
用法示例:
int main(int argc, char *argv[])
{
IntGridRef grid = IntGrid.create(10, 10);
for (int i = 0; i < IntGrid.rows(grid); i++) {
for (int j = 0; j < IntGrid.cols(grid); j++) {
IntGrid.data(grid)[i][j] = arc4random_uniform(10);
}
}
IntGrid.print(grid);
IntGrid.free(grid);
return 0;
}
intmain(intargc,char*argv[])
{
IntGridRef grid=IntGrid.create(10,10);
for(inti=0;i
基本上你已经做到了,但与其让编译器生成代码将[x][y]映射到分配给board的内存中的特定元素,还不如自己进行映射:board[x*h+y]或board[y*w+x](其中w是宽度,h是高度);选择哪一个并不重要,只要保持一致(函数或宏在这里会有所帮助).像这样声明和分配董事会:
int *board = malloc( n * m * sizeof(int) );
然后,无论何时您希望访问board[x][y],请使用以下表达式:
board[y*n+x]
你正在用同样的代码发布一个又一个关于同样问题的问题。你应该花点时间关闭网站,阅读一篇好的C bool to und
board[y*n+x]