Возврат 2D-массива из функции в основную функцию - PullRequest
1 голос
/ 06 июня 2019

Я пишу код для Game of Life, и, как это предусмотрено для моего задания, я не должен использовать указатели.

Для подсчета количества соседних ячеек в каждой ячейке, ячейка в этом контексте является координатой моей2D массив, я написал функцию, которая проходит через все строки и столбцы и подсчитывает, сколько 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поле, в котором у меня есть несколько ЖИВЫХ ячеек.

Тогда я бы ожидал, после распечатки 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

Ответы [ 2 ]

2 голосов
/ 07 июня 2019

Вы не можете возвращать массивы в C. И поскольку вам не разрешено использовать указатели, вам придется обернуть массив в структуру. Пример:

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

MyStruct func()
{
    MyStruct my_struct;

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

    return my_struct;
}
0 голосов
/ 07 июня 2019

Как я уже упоминал в своем комментарии, вы не можете возвращать типы массивов из функции. правильный способ сделать это - передать целевой массив в качестве параметра:

const int rows = 20;
const int cols = 20;
cell field[rows][cols];
int counting[rows][cols];
...
CellNeighbor( rows, cols, field, counting );

Затем вы определите свою функцию CellNeighbor как:

void CellNeighbour(const int rows, const int cols, cell field[rows][cols], int counts[rows][cols])
{
  int i,j;
  int count =0;

  // you could probably replace the following with memset( counts, 0, rows * cols * sizeof counts[0][0]
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      counts[i][i] =0;
    }
  }

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

Теперь вы не явно нигде используете указатели, поэтому должен удовлетворять условиям назначения.

Но ...

За исключением случаев, когда он является операндом операторов sizeof или унарных & или является строковым литералом, используемым для инициализации массива символов в объявлении, выражение типа "массив N-элементовиз T "будет преобразовано (" распад ") в выражение типа" указатель на T ", а значением выражения будет адрес первого элемента массива.

Поэтому при вызове CellNeighbor( rows, cols, field, counting ) выражение field преобразуется из типа "rows -элементный массив cols -элементный массив cell" (cell[rows][cols]), чтобы набрать «указатель на cols -элемент массива cell» (cell (*)[cols]).Аналогично, выражение counting преобразуется из типа "rows -элементный массив cols -элементный массив int" (int [rows][cols]) в "указатель на cols -элементный массив int"(int (*)[cols]).

Это означает, что CellNeighbor получает указатель значения для field и counting, а не фактические массивы.

В контексте объявления параметра функции, T a[N] и T a[] «настроены» на T *a, поэтому объявления cell field[row][cols] и int counts[row][cols] интерпретируются как cell field (*)[cols] и int counts (*)[cols] в определении функции CellNeighbor.

Если ваше назначение серьезно относится к «нет указателей», как, например, к «нет указателей ВСЕ, даже неявных указателей в результате распада выражения массива», то вы не можете передавать выражения массива в качестве аргументов функции, точка.У меня действительно трудное время поверить, что именно это и задумал ваш инструктор.

...