#include <iostream>
#include <fstream>
using namespace std;

int underPopulation;  // Any cell with less than <underPopulation> liviving neighbors dies.
int overCrowding;    // Any cell with more than <overCrowding> living neighbors dies.
int minNeighborsToLive;   // Any cell with <minNeighborsToLive> living neighbors lives.
int reproduction;  // Any dead cell with <reproduction> living neighbors becomes a live cell.
const int numRows = 22;  // Number of rows in the board
const int numColumns = 80; // Number of columns in the board
char Community[numRows][numColumns]; // environment representing all the cells and their state.


//Function declarations
void initData();
void toggleState(int nRow, int nCol) ;
bool getCurrentCellState(int nRow, int nCol) ;
int getNumberAliveNeighbors(char myArray[][numRows][numColumns]);
int getNumberAliveNeighborsInRow(char myArray[numRows][numColumns]);
void generateNextCycleState(char Community[numRows][numColumns]);
void printArrayState();


int main()
{
	char s, str[256];
	ifstream input;
	

	cout << "Enter the name of a text file containing an initial colony configuration" << endl;
	cin >> str; 
	input.open(str);

	if(input.fail())
	{
		cout << "Input file not found" << endl;
		exit(1);
	}

	while(input.good())
	{
	    if (input.good())
			for(int x=0; x < numRows; x++)
			{
				for (int y=0; y < numColumns; y++)
				{
					  s = input.get();
                      Community[x][y] = s;
				}
			}
	}

    input.close();
	system("cls");

    for(int x=0; x < 22; x++)
			{
				for (int y=0; y < 80; y++)
                {
                      cout << Community[x][y];
				}
			}
    cout << "Generation 1" << endl;
	
	system("pause");

    while (true)
	{
		printArrayState();
		generateNextCycleState(Community[22][80]);  // FIRST ERROR: LINE 70
		system("pause");
		system("CLS");
	}
	printArrayState();
	return 0;
}



// Get the number of neighbors who are alive
int getNumberAliveNeighbors(char myArray[][numRows][numColumns]) {
	int numAliveNeighbors = 0;
	int col, row;
	if (numRows > 0)                                                                // Check for top neighbors
    {     
		row = numRows - 1;
		numAliveNeighbors += getNumberAliveNeighborsInRow(myArray[row][numColumns]);  // SECOND ERROR: LINE 87
	}
	if (numRows < (numRows - 1))                                                    // Check for bottom neighbors
    {
		row = numRows + 1;
		numAliveNeighbors += getNumberAliveNeighborsInRow(myArray[row][numColumns]);  // THIRD ERROR: LINE 92
	}
	
	                                                                               // Check for side neighbors
	col = numColumns - 1;
	if (col >= 0) {
		if (getCurrentCellState(numRows, col)) {
			numAliveNeighbors += 1;
		}
	}
	col = numColumns + 1;
	if (col < numColumns) {
		if (getCurrentCellState(numRows, col)) {
			numAliveNeighbors += 1;
		}
	}
	
	return numAliveNeighbors;
}









// Get number of alive neighbors in a row
int getNumberAliveNeighborsInRow(char myArray[][numRows][numColumns]) {
	int numAliveNeighbors = 0;
	int col = numColumns - 1;
	if (col >= 0) {                                      // Check for left edge.
		if (getCurrentCellState(numRows, col)) {
			numAliveNeighbors += 1;
		}
	}
	if (getCurrentCellState(numRows, numColumns)) {               // Current position.
		numAliveNeighbors += 1;
	}
	col = numColumns + 1;
	if (col < numColumns) {                             // Check for right edge.
		if (getCurrentCellState(numRows, col)) {
			numAliveNeighbors += 1;
		}
	}
	return numAliveNeighbors;
}






void generateNextCycleState(char Community[numRows][numColumns]) 
{
	char nextGen[numRows][numColumns];  // Create a model for next generation
	int nRow = 0;
	
	// Copy data to next generation model.
	for (nRow = 0; nRow < numRows; nRow++) {
		for (int nCol = 0; nCol < numColumns; nCol++) {
			nextGen[nRow][nCol] = Community[nRow][nCol];
		}
	}


	// Check neighbors.
	int numAliveNeighbors = 0;
	for (nRow = 0; nRow < numRows; nRow++) {
		for (int nCol = 0; nCol < numColumns; nCol++) {
			numAliveNeighbors = getNumberAliveNeighbors(nextGen[nRow][nCol]);  // FOURTH ERROR: LINE 163
			if (getCurrentCellState(nRow, nCol)) {              // Current cell is alive.
				if (numAliveNeighbors < underPopulation) {      // Current cell dead due to lonliness.
					nextGen[nRow][nCol] = false;
				}
				if (numAliveNeighbors > overCrowding) {         // Current cell dead due to over crowding.
					nextGen[nRow][nCol] = false;
				}
			} else {                                            // Current cell is dead.
				
				if (numAliveNeighbors == reproduction) {        // dead cell comes to life.                       
					nextGen[nRow][nCol] = true;
				}
			}
		}
	}

	// Copy the next gen data to original model.
	for (nRow = 0; nRow < numRows; nRow++) {
		for (int nCol = 0; nCol < numColumns; nCol++) {
			Community[nRow][nCol] = nextGen[nRow][nCol];
		}
	}
}



void printArrayState(char myArray[][numRows][numColumns])
{
	for (int i = 0; i < numRows; i++)
	{
		for (int j = 0; j < numColumns; j++)
		{
			cout << myArray[i][j];
		}
		cout << endl;
	}
}

the errors are:
line 70 - cannot convert parameter 1 from 'char' to 'char[][80]'
line 87 and 92 - cannot convert parameter 1 from 'char[80]' to 'char[][80]'
line 163- cannot convert parameter 1 from 'char' to 'char[][22][80]'

i tried searching google and these threads but i didn't find anything. if you guys find a link or something explaining how to fix the errors that would be great too. thanks!

These issues are all about you passing single char (or a pointer to char) into functions that expect other things. Your code looks a little confused to me.

int getNumberAliveNeighbors(char myArray[][numRows][numColumns]);

What do you actually mean to pass into this function?

Edited 5 Years Ago by Moschops: n/a

ideally, i want the array Community that i constructed in the main program to be passed to all of my functions. i'm not even 100% on the entire code, but i think that's what i want.

If you're not sure what you want, how can we even begin to be?

RE: Error on Line 70:
From what I can see, the function is intended to receive a 2-dimensional array. However, when you pass Community[22][80] you are attempting to pass the single char that is found at the 81st position of the 23rd string contained in the char array; which, I might add, is non-existent because it's about 80-bytes outside the limits of the array. If you want to pass the entire array, you'll only want to use "Community" as the argument to the function call.

Your other 2 errors are similar in nature.

Edited 5 Years Ago by Fbody: n/a

if i was sure of the code i wouldn't need help. i know what i want my program to do but i'm not sure if all the code is correct and will do what i want it to.

your advice to shorten it down to just Community did help though, so i thank you.

>>i'm not sure if all the code is correct and will do what i want it to
I was alluding to the fact that you haven't told us what you want it to do. If you tell us what you want it to do, we should be able to provide you with some better suggestions.

This article has been dead for over six months. Start a new discussion instead.