c - 从函数返回一个二维数组到主函数

标签 c multidimensional-array conways-game-of-life

我正在为 Game of Life 编写代码,根据我的作业规定,我不应该使用指针。

为了计算每个单元格的邻居数量,在此上下文中的单元格是我的二维数组上的坐标,我编写了一个函数,它遍历所有行和列并计算每个单元格有多少个 ALIVE 邻居。最大值为 8。

但是我不知道如何返回我的数组,该数组将相邻单元格的数量存储在 20x20 数组中。

下面是完整的代码。请注意,有些部分未完成,因为我正在补充给我的模板。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'

/* Declaration of data structure */
typedef struct{
  char current;
  char next;
} cell;

/* Declaration of functions */
void initField(const int rows, const int cols, cell field[rows][cols]);
void loadGlider(const int rows, const int cols, cell field[rows][cols]);
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
void loadRandom(const int rows, const int cols, cell field[rows][cols]);
void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void printWorld(const int rows, const int cols, cell field[rows][cols]);
int CellNeighbour(const int rows, const int cols, cell field[rows][cols]);



/* Function:    main
* Description: Start and run games, interact with the user.
* Input:       About what initial structure and whether to step or exit.
* Output:      Information to the user, and the game field in each step.
*/

int main(void) {

  const int rows = 20;
  const int cols = 20;
  cell field[rows][cols];
  int counting[rows][cols];

  initField(rows,cols, field);
  printWorld(rows,cols,field);
  CellNeighbour(rows,cols,field);//test



    for (int i = 0; i < rows; i++){
        for (int j = 0; j < cols; j++){
            printf("%d ", counting[i][j]);
        }
        printf("\n");
    }




  return 0;
}


/* Function:    initField
* Description: Initialize all the cells to dead, then asks the user about
*              which structure to load, and finally load the structure.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void initField(const int rows, const int cols, cell field[rows][cols]) {

  for (int r = 0 ; r < rows ; r++) {
    for (int c = 0 ; c < cols ; c++) {
      field[r][c].current = DEAD;
    }
  }

  printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
  printf("or [C]ustom): ");

  int ch = getchar();

  /* Ignore following newline */
  if (ch != '\n') {
    getchar();
  }

  switch (ch) {
    case 'g':
    case 'G':
    loadGlider(rows, cols, field);
    break;
    case 's':
    case 'S':
    loadSemaphore(rows, cols, field);
    break;
    case 'r':
    case 'R':
    loadRandom(rows, cols, field);
    break;
    case 'c':
    case 'C':
    default:
    loadCustom(rows, cols, field);
    break;
  }
}


/* Function:    loadGlider
* Description: Inserts a glider into the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadGlider(const int rows, const int cols, cell field[rows][cols]) {

  field[0][1].current = ALIVE;
  field[1][2].current = ALIVE;
  field[2][0].current = ALIVE;
  field[2][1].current = ALIVE;
  field[2][2].current = ALIVE;
}


/* Function:    loadSemaphore
* Description: Inserts a semaphore into the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {

  field[8][1].current = ALIVE;
  field[8][2].current = ALIVE;
  field[8][3].current = ALIVE;
}


/* Function:    loadRandom
* Description: Inserts a random structure into the field.
* Input:       The field array and its size.
* Output:      The field array is updated. There is a 50 % chance that a cell
*              is alive.
*/

void loadRandom(const int rows, const int cols, cell field[rows][cols]) {

}


/* Function:    loadCustom
* Description: Lets the user specify a structure that then is inserted into
*              the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadCustom(const int rows, const int cols, cell field[rows][cols]) {

  printf("Give custom format string: ");
  do {
    int r, c;
    scanf("%d,%d", &r, &c);
    field[r][c].current = ALIVE;
  } while (getchar() != '\n');
}
/* Function:    printWorld
* Description: Prints the current field
* Input:       The field array and its size.
* Output:      The field array is updated.
*/


void printWorld(const int rows, const int cols, cell field[rows][cols]){

  char c = '\n';

  while(c == '\n'){
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        printf("%c ", field[i][j].current);
      }
      printf("\n");
    }
    c = getchar();
    if(c != '\n'){
      break; // hoppa ut ur loopen till main funktionen
    }

  }
}

void evolve(const int rows,const int cols,cell field[rows][cols]){

  for(int i = 0;i<rows;i++){
    for(int j =0;j<cols;j++){
      if(field[rows][cols].current == ALIVE  && ArrayDatCorresponds2NmbofNeighb[rows][cols]<2){
      }
      if(field[rows][cols].current == ALIVE && ArrayDatCorresponds2NmbofNeighb[rows][cols] ==3 ||ArrayDatCorresponds2NmbofNeighb[rows][cols] ==2 ){
        field[rows][cols].next = ALIVE;
      }
      if(field[rows][cols].current == ALIVE && ArrayDatCorresponds2NmbofNeighb[rows][cols] >= 4 ){
        field[rows][cols].next = DEAD;
      }
      if(field[rows][cols].current == DEAD && ArrayDatCorresponds2NmbofNeighb[rows][cols] ==3){
        field[rows][cols].next = ALIVE;
      }
    }
  }



int CellNeighbour(const int rows, const int cols, cell field[rows][cols]){

  int i,j;
  int count =0;
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
  int StoreArray[rows][cols] =0;
 }
}

  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      if(field[rows-1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
    }
  }


  return StoreArray; 
}

以下是我遇到问题的功能:

int CellNeighbour(const int rows, const int cols, cell field[rows][cols]){

  int i,j;
  int count =0;
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
  int StoreArray[rows][cols] =0;
 }
}

  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      if(field[rows-1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
    }
  }


  return StoreArray; 
}

如果我初始化一个 20x20 的区域,其中有一些 ALIVE 细胞。

然后我希望,在打印出一个 5x5(只是为了简单起见)计算每个单元格有多少个邻居之后,一个看起来像这样的网格:

2 2 0 0 0
2 3 0 0 0
0 0 0 0 0
0 1 1 0 0
0 0 0 0 0

最佳答案

您不能在 C 中返回数组。并且由于不允许使用指针,因此您必须将数组包装在结构中。示例:

typedef struct {
    int data[rows][cols];
} MyStruct;

MyStruct func()
{
    MyStruct my_struct;

    // Fill my_struct.data with what you need.
    // ...

    return my_struct;
}

关于c - 从函数返回一个二维数组到主函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484979/

相关文章:

Javascript 未定义函数错误

embedded - 对于低内存使用,康威生命游戏的有效实现是什么?

C - 如何在只读取单个字符时检测管道中的 EOF?

c - 指向结构成员的指针,无需初始化

java - C/Java 中的堆栈与堆

c# - 使用 int[][] 和 int[,] 有什么区别?

javascript - 有没有一种优雅的方法可以从 JavaScript 或 angularjs 的二维数组中只获取一维?

c - C 中 ")"标记之前的错误预期表达式

JavaScript - 操作对象的嵌套多维数组

c - 在生命游戏矩阵中搜索人口最多的区域