动态2d矩阵实现

时间:2012-01-22 19:29:10

标签: c

我的静态矩阵的insdead我尝试获得2d动态matrics 我想要做的就是更改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 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);  
..
..
}  

这是我想要使用的代码 - 请告诉我正确的分辨 不使用#define WIDTH 50 #define HEIGHT 20

    int **matrix_dyn(int n, int m)
    {
        int i = 0;
        int j = 0;

            printf ("please enter the horizontal size of the board \n");
        scanf ("%d", &n);

        printf ("please enter the vertical size of the board \n");
        scanf ("%d", &m);


        int **board = (int**)malloc(n * sizeof(int*));

        printf("please enter the 0's or 1's to fill the matrix \n");
        for (i = 0; i <= n; i++)
        board[i] = (int*)malloc(m*sizeof(int));

        for(i = 0; i <= n; i++)
        {
            for(j = 0; j <= m; j++)
            scanf ("%d", &board[i][j]);
        }
        return board;
    }

这是我的全部代码:

    #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;  
    }  

3 个答案:

答案 0 :(得分:1)

声明&amp;像这样分配董事会:

int *board = malloc( n * m * sizeof(int) );

然后,只要您想访问board [x] [y],请使用以下表达式:

board[y*n+x]

答案 1 :(得分:0)

尝试使用结构,这是我编写的一个简单实现(使用GCC编译,以支持constructor属性。

// 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;
}

示例用法:

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;
}

答案 2 :(得分:0)

你基本上就在那里,但不是让编译器生成代码来将[x] [y]映射到为board分配的内存中的特定元素,你必须自己进行映射:board [x * h + y]或板[y * w + x](其中w是宽度,h是高度);你选择哪个并不重要,只需要保持一致(函数或宏在这里会有所帮助)。