This may seem like kind of a tall order but I've been running into problems with a program I've been working on and I've been unable to figure out why some of the things go wrong... particularly when all the conditions/code look right.

To understand this code properly, I feel I need to explain where the idea came from.

I saw in a computer lab once about a problem called Eden. How it works is that there a set of boxes/matrix full of zeros with only one 1. It works through the idea that if there's room for that 1 to expand to then those boxes next to it will become a 1, and if it can't expand it dies (becomes 0).

Being who I am, I decided lets try to develop a way to do this without using if statements, that way this problem can be done easier with larger matrixes.

Long while later, I figured it out and set it as a function. However, I still had to develop a method for a variable matrix.

So without further ado, here is my code with comments:

The compilier I'm using is Dev-C++, running on Windows.Code://Models binary growth and decay given that a population dies when it can't expand //and will expand to any areas immediately next to it that are empty. #include <stdlib.h> #include <math.h> #include <time.h> #include <iostream> using namespace std; int twovar(int s, int v1, int v2, int f) { f = (s - (v1 + v2)/2 - ((s - 1)*((v1 + v2)%2))); //"abs" caused a problem, why? } int threevar(int s, int v1, int v2, int v3, int f) { f = (s - (v1 + v2 + v3)/3 - ((s - 1)*((v1 + v2 + v3)%3 - ((v1 + v2 + v3)%3)/2))); } int fourvar(int s, int v1, int v2, int v3, int v4, int f) { f = (s - (v1 + v2 + v3 + v4)/4 - ((s - 1)*((v1 + v2 + v3 + v4)%4 - ((v1 + v2 + v3 + v4)%4)/3 - ((v1 + v2 + v3 + v4)%4)/2))); } main() { int r, c, row, col; int a[4][4]; int sa[4][4]; int refresh, time = 0, stime; char any; a[1][1] = 1; a[1][2] = 0; a[1][3] = 0; a[1][4] = 0; a[2][1] = 0; a[2][2] = 0; a[2][3] = 0; a[2][4] = 0; //error, sets [4][1] and [3][4] a[3][1] = 0; a[3][2] = 0; a[3][3] = 0; a[3][4] = 0; //to 4 despite these settings. a[4][1] = 0; a[4][2] = 0; a[4][3] = 0; a[4][4] = 0; cin >> any; stime = clock()/CLOCKS_PER_SEC; /*for (row = 1; row <= 4; ++row) //my idea as to how the above area { //could be made more generic. for (col = 1; col <=4; ++col) //fails to initialize though.... { a[row][col] = 0; } } a[1][1] = 1;*/ do{ system("cls"); for (row = 1; row <= 4; ++row) { for(col = 1; col <= 4; ++col) //used to output each number of the matrix { //such that they are seperated by a space cout << a[row][col] << "\t"; } cout << "\n"; } for (row = 1; row <= 4; ++row) { for(col = 1; col <= 4; ++col) { if ((row == 1) && (col == 1)) { sa[row][col] = abs(twovar(a[row][col], a[row + 1][col], a[row][col + 1], sa[row][col])); } if ((row == 1) && (col != 1) && (col != 4)) { sa[row][col] = abs(threevar(a[row][col], a[row + 1][col], a[row][col - 1], a[row][col + 1], sa[row][col])); } if ((row == 1) && (col == 4)) { sa[row][col] = abs(twovar(a[row][col], a[row + 1][col], a[row][col - 1], sa[row][col])); } if ((row != 1) && (row != 4) && (col == 1)) { sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col + 1], sa[row][col])); } if ((row != 1) && (row != 4) && (col != 1) && (col != 4)) { sa[row][col] = abs(fourvar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col - 1], a[row][col + 1], sa[row][col])); } if ((row != 1) && (row != 4) && (col == 4)) { sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col - 1], sa[row][col])); } if ((row == 4) && (col == 1)) { sa[row][col] = abs(twovar(a[row][col], a[row - 1][col], a[row][col + 1], sa[row][col])); } if ((row == 4) && (col != 1) && (col != 4)) { sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row][col - 1], a[row][col + 1], sa[row][col])); } if ((row == 4) && (col == 4)) { sa[row][col] = abs(twovar(a[row][col], a[row - 1][col], a[row][col - 1], sa[row][col])); } } } for (row = 1; row <= 4; ++row) { for(col = 1; col <= 4; ++col) { a[row][col] = sa[row][col]; } } time = clock()/CLOCKS_PER_SEC - stime; cout << "\n \n" << time; do{ refresh = clock()/CLOCKS_PER_SEC - stime - time; } while (refresh < 3); } while (1 != 0); } //program fails after first loop. //certain values fail to calculate properly, namely [4][2] and [2][4].

I'm unsure whether it's something wrong with the code or something else so if someone else could give it a try it would be much appreciated.

Any questions or wish to see previous code known to work, please post or send a message.