Passing a 2D boolean array in .C

I am making a "Game of Life" program in .C and need to set my entire 2D array, for if the organism in that spot is alive (represented by a boolean), to a 2D array of the calculated next generation. I realize there may be an easier way to make organism[][] = nextGeneration[][], but there are several different functions where I need to be able to pass the 2D array and alter it. A few other functions where I need to be able to access the array are: resetting all variables, translating an input file to my array, calculating the next generation, and printing the array out to the console to display. I found some information online about needing to pass a separate integer to hold the size of my array, but I don't understand how I'm supposed to pass the entire array so I can edit it and return the whole change. (Note: I don't actually need to return anything, because the array should already be passed as a pointer. There may be flaws in my logic here..)
I would be grateful if anyone could point me in the right direction!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//In driver or main()
const int WIDTH = 60;
const int HEIGHT = 30;
bool organism[HEIGHT][WIDTH];
bool nextGeneration[HEIGHT][WIDTH];
char organismSymbol = '*';
...

//only one []? Cause I get errors with organism[][]
spanGeneration(organism[], nextGeneration[], HEIGHT * WIDTH);
displayOrganisms(organism[], nextGeneration[], HEIGHT * WIDTH);
...

//Function for setting one array to the other
void spanGeneration(bool organism[], bool nextGeneration[], int arraySize)
{
	int i = 0;
	int j = 0;

	for(i = 0; i < HEIGHT; i++)
	{
		for(j = 0; j < WIDTH; j++)
		{
			organism[i][j] = nextGeneration[i][j];
		}
	}
}

//function for displaying the array
void displayOrganisms(char organismSymbol, bool organism[], int arraySize)
{
	int i = 0;
	int j = 0;

	system("cls");//NOTE: Anyone know a more portable way to clear the console?

	for(i = 0; i < HEIGHT; i++)
	{
		for(j = 0; j < WIDTH; j++)
		{
			if(organism[i][j] == true)
				printf("%c", organismSymbol);
			else
				printf(" ");
		}
		printf("\n");
	}
}
Last edited on
Would it be easier for me to code my nested loop inside my main() then call organism[j][m] as a single boolean value? Then call the function for every iteration? Or would this cause unnecessary memory usage? (For all I know it could use less memory..)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Something kinda like...
//main()
for(i = 0; i < HEIGHT; i++)
	{
		for(j = 0; j < WIDTH; j++)
		{
			organism[i][j] = spanGeneration(nextGeneration[i][j]);
		}
	}

//function in question
void spanGeneration(bool organism, bool nextGeneration)
{
	organism = nextGeneration;
        return organism;
}


The reason I am trying to leave it in a function is because it is a group project and other people are working on the functions. I just need to figure out how to pass the appropriate parameters before they can start.
Arrays are always passed by reference, so whatever you do in the function will affect the original data.

In C and C++, all dimensions except the outermost must have a compile-time size.

1
2
3
void foo( bool a[][ COLS ], int rows )
{
}

You can use templates to help:

1
2
3
4
template <int Rows, int Cols>
void foo( bool (&a)[ Rows ][ Cols ] )
{
}

All this presumes that you will be using arrays that are at some place in your code declared with specific dimensions:

 
bool my_array[ 25 ][ 80 ];

If, however, you plan to be able to handle any dynamic size array, you must do all the indexing yourself.

See this for more:
http://www.cplusplus.com/forum/beginner/118058/#msg643956

Hope this helps.
My array is declared using constants, so the array will not need to be dynamic. Thank you though!

So all I need to worry about is passing the second array size(columns), then passing the first(rows) as an int?
 
void spanGeneration(bool organism[][width], int height)

And this calls my same exact array from my earlier code in main? Seems strange cause it should either need to know the entire size, or it should be ambiguous so it can refer to the array it's trying to access.

can I not do something like:
 
void spanGeneration(bool organism[height][width])

Or will this only call one exact point in that array?
Last edited on
I think I also have something wrong when I'm trying to call the function.
I should need something like:
 
spanGeneration(organism[][], nextGeneration[][]);

but that doesn't exactly work. How do I pass the array to the function in the first place?
If your array is defined with constants, then you don't need to bother passing anything except the array itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int ROWS = 4;
const int COLS = 3;

typedef bool BoolArray[ ROWS ][ COLS ];

void clear( BoolArray a, bool value = false )
{
  for (int row = 0; row < ROWS; row++)
  for (int col = 0; col < COLS; col++)
    a[ row ][ col ] = value;
}

int main()
{
  BoolArray bs;

  clear( bs );

  ...
}

Hope this helps.
So.. I believe what I need is:
1
2
3
4
//header
const int WIDTH = 60;
const int HEIGHT = 30;
void spanGeneration(bool organism[HEIGHT][WIDTH], bool nextGeneration[HEIGHT][WIDTH]);

1
2
3
4
5
//main()
bool organism[HEIGHT][WIDTH];
bool nextGeneration[HEIGHT][WIDTH];
...
spanGeneration(organism, nextGeneration);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//library
void spanGeneration(bool organism[HEIGHT][WIDTH], bool nextGeneration[HEIGHT][WIDTH])
{
	int i = 0;
	int j = 0;

	for(i = 0; i < HEIGHT; i++)
	{
		for(j = 0; j < WIDTH; j++)
		{
			organism[i][j] = nextGeneration[i][j];
		}
	}
}

I have no errors showing so far. Thank you!
Yes, exactly right.

Glad to help. :O)
I'm still having an issue...
It says that there is a syntax error: ')'
It says there is this error for every line in my header defining functions with arguments using my two arrays, but does not say where this fabled ')' goes. I keep trying to put it random places and seeing if it works, but I have no idea why it thinks it needs an ending parentheses, but it also say's the ending parentheses need to be in my library several times at the beginning of each function.
 
void function(array[X][Y])
When you call the function: function(array); is all you need.
Yeah it's defining the function that seems to be the problem.
I figured out my problem with defining the function.

I'm using Microsoft Visual Studio 2010, which doesn't have .c compatibility with bool. I fixed this by creating one my own Boolean handler:
1
2
3
#define BOOL int
#define FALSE 0
#define TRUE (!FALSE) 
Last edited on
Topic archived. No new replies allowed.